0

I am trying to create folders inside another folder using numbers. The code is below. Now I am checking with echo. But it is printing empty string.What is the mistake in it ? and how to achieve this?

    set folder=d:/delete_It
    setlocal ENABLEDELAYEDEXPANSION        
    FOR /L %%r IN (72,1,98) DO (
    REM mkdir %%r
    if not errorlevel 0 goto somethingbad
    set final_Dir=%folder%/%%r
    if not errorlevel 0 goto somethingbad
    echo final_Dir
    if not errorlevel 0 goto somethingbad
    )
    endlocal
    somethingbad:
        echo "Unexpected error" 

Note: I am using Windows 10 -pro, 64 bit

4
  • You have folder undefined, and perhaps echo %final_Dir%.
    – harrymc
    Commented Feb 5, 2021 at 10:56
  • @harrymc Sorry. I already added that line im my bat file. I forgot to paste it here. Commented Feb 5, 2021 at 13:37
  • I still see echo final_Dir.
    – harrymc
    Commented Feb 5, 2021 at 13:40
  • windows filesystem uses backslash as a path seperator, not forward slash. Aside from that, variables expanded in code blocks have their value expanded as it was at the start of the code block, unless: Delayed expansion is enabled and used, or, CALL is used and % expansion is doubled.
    – T3RR0R
    Commented Feb 5, 2021 at 14:31

1 Answer 1

0
@echo off 

pushd "D:\delete_it"
setlocal EnableDelayedExpansion 

for /l %%r in (72 1 98)do mkdir %%r && (
       set "_final_dir=!cd!\%%~r"
       <con: echo\!_final_dir!
    ) || (
      echo\Unexpected error^!! & popd
      endlocal & goto :eof
    )

popd & endlocal | <con: echo\Good bye^^!!

You don't need to set a variable for your folder, just go to D:\Delete_it or use pushd and popd

You can replace all your if errorlevel to operators && ==> return 0 and || ==> return non 0

You can replace goto (and :label) to || (commands in block)

2
  • Did you mean >con: (to the screen)? (echo doesn't take input)
    – Stephan
    Commented Feb 11, 2021 at 9:19
  • @Stephan Thank for your comment, This is a preference, just as I use set /p var=string <nul I like to use <con:
    – Io-oI
    Commented Feb 12, 2021 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .