In a batch file, I need to get a path from the user, remove the trailing backslash if they included it, and then check if any files with the .sql extension are in the folder that they specified. If not, I want to return them to the variable entry section.
Here's what I have:
REM Get the folder paths from the user
:setfolderpaths
set /p SCRIPTFOLDER="D:\Mark\Centriq Extraction Projects\"
set /p OUTPUTFOLDER="D:\Mark\Centriq Extraction Projects\"
REM View what the user entered (testing only)
echo %SCRIPTFOLDER%
echo %OUTPUTFOLDER%
REM pause here to check the user entry (testing only)
@pause
REM Remove trailing slashes from the folder path if they exist
if %SCRIPTFOLDER:~-1%==\ set SCRIPTFOLDER=%SCRIPTFOLDER:~0,-1%
if %OUTPUTFOLDER:~-1%==\ set OUTPUTFOLDER=%OUTPUTFOLDER:~0,-1%
REM If the folder the user entered doesn't exist, go back to the folder entry section
if not exist %SCRIPTFOLDER% + "\*.sql" (
goto setfolderpaths
)
However, when I run the file, after I press a key to continue at the pause, the cmd window closes immediately. I've checked that the lines removing the backslash work, so the problem seems to be in my if not exist
check.
EDIT Apparently, I had some code in my sample that was confusing what I am trying to figure out. I've added comments to explain what I'm doing in each part. I just need to figure out why my check for.sql files in the user-specified folder isn't working.
UPDATE
Okay, I cleaned up my quoting issues per @LotPings' suggestions.
So, when I put an echo right before the if
check:
echo %SCRIPTFOLDER%
@pause
That's displaying a valid path that I know does have sql files in it. So this code isn't working:
if not exist "%SCRIPTFOLDER%\*.sql" (
goto setfolderpaths
)
When I hit a key to continue at the pause the cmd window closes.