1

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.

marky
  • 4,878
  • 17
  • 59
  • 103
  • I fully understood your not working code and told the issues in my answer. `Set /p` gets typed user input, the part behind the `=` is the user prompt. –  May 31 '18 at 20:00

2 Answers2

0

This are all quoting issues.

If the double quote is the last char (as is the case above) and you remove it,
the string has only a leading d'quote and still a trailing backslash.

Better quote always like this:

:setfolderpaths
set "SCRIPTFOLDER=D:\Mark\Centriq Extraction Projects\"
set "OUTPUTFOLDER=D:\Mark\Centriq Extraction Projects\"

echo %SCRIPTFOLDER%
echo %OUTPUTFOLDER%

@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%"

if not exist "%SCRIPTFOLDER%\*.sql" (
    goto setfolderpaths
)

Also there is no string concatenation with + you used in your if.

0

Read the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? and you know what is wrong on first two SET command lines.

Original question: The folder paths are assigned to the environment variables with both double quotes and so the last character of both environment variable values is " and not the backslash as expected by you.

Edited question: The two folder paths are displayed as prompt text.

The wrong position of first " on SET command lines is responsible for syntax errors on remaining lines as well as + on IF condition line. Windows command processor does not support string concatenations using an operator +. The strings to concatenate must be just written concatenated in the batch file.

Here is a batch file which prompts the user for both paths and runs some checks as the user has the freedom to enter nothing or strings which could do something really bad without appropriate checks in right sequence without using delayed environment variable expansion. The command lines are modified by Windows command processor before execution by each %ScriptFolder% and %OutputFolder% reference. It never really matters what is written in batch file. It matters how each command line or an entire command block looks like after parsing by Windows command processor cmd.exe on execution of batch script.

@echo off

:GetScriptFolderPath
set "ScriptFolder="
set /P "ScriptFolder=Enter script folder path: "

rem Has the user not entered any string?
if not defined ScriptFolder goto GetScriptFolderPath

rem Remove all double quotes from entered string.
set "ScriptFolder=%ScriptFolder:"=%"

rem Has the user entered a string consisting only of double quotes?
if not defined ScriptFolder goto GetScriptFolderPath

rem Remove a backslash at end of user entered string.
if "%ScriptFolder:~-1%" == "\" set "ScriptFolder=%ScriptFolder:~0,-1%"

rem Has the user entered just a backslash (with one or more double quotes)?
if not defined ScriptFolder goto GetScriptFolderPath

rem Is the user entered string really a path to a directory
rem which really exists and contains at least one *.sql file?
if not exist "%ScriptFolder%\*.sql" goto GetScriptFolderPath

:GetOutputFolderPath
set "OutputFolder="
set /P "OutputFolder=Enter output folder path: "
if not defined OutputFolder goto GetOutputFolderPath
set "OutputFolder=%OutputFolder:"=%"
if not defined OutputFolder goto GetOutputFolderPath
if "%OutputFolder:~-1%" == "\" set "OutputFolder=%OutputFolder:~0,-1%"
if not defined OutputFolder goto GetOutputFolderPath

rem Is the user entered string really a path to an existing directory?
if not exist "%OutputFolder%\" goto GetOutputFolderPath

Note: The batch file user can also drag & drop a folder path for example from Windows Explorer into the console window to enter the path and needs to press next only RETURN or ENTER to complete the input prompt.

It is also possible to use set "ScriptFolder=D:\Mark\Centriq Extraction Project" instead of just set "ScriptFolder=" to define a non empty default on which the user has just to press RETURN or ENTER for using it. A default folder path can be also set for the output folder.

Mofi
  • 46,139
  • 17
  • 80
  • 143