I am trying to make a simple batch to search within a folder for a folder that contains a user-prompted name, and if the folder is found, it will ask if the folder should be copied to a pre-defined location. When I type the lines separately in cmd, they work fine, but when I try to save it in a batch file and run it, it somehow closes somewhere in the for do right after the SET, so I’m suspecting it has something to do with how my FOR is formatted in Notepad++?
:START
set /p nw=Please enter the search string:
FOR /f "tokens=*" %%i IN ('dir /a:d-s-h /b ^| findstr /i /m "%nw%"') do (
echo found %%i &&echo.
set /p cflag=Would you like to copy this folder over? Press 'y' and enter:
if %cflag%==y xcopy "%%i" "%sci%" /e
)
set /p rflag=Search for another folder? Press 'y' and enter:
if %rflag%==y goto START
This is going to be run on XP.
Thanks.
It has nothing to do with notepad++, the keyword is delayed expansion.
%cflag% will be expanded while the for block is parsed, before any of the lines in the block are executed.
Therefore it is still empty and it expands to nothing.
And then the if clause has an invalid syntax.
But you can solve it with delayed expansion.
Just put this to the beginning of your batch.
And then expand your variable with