Batch file unexpected behavior for prompt - batch-file

I have the following batch file:
echo off
CD \
:Begin
set /p UserInputPath= "What Directory would you like to make?"
if not exist C:\%UserInputPath% (
mkdir %UserInputPath%
) else (
set /p confirm= "Do you want choose another directory?"
echo %confirm%
if "%confirm%"=="y" goto Begin
)
OUTPUT:
C:\>echo off
What Directory would you like to make?ff
Do you want choose another directory?n
y
What Directory would you like to make?
Look at output, Directory ff already Exists, as you see If
I answer n to Do you want choose another directory? Variable
"%confirm% shows as y.
Any ideas?

Windows command processor substitutes all environment variable references using syntax %variable% within a command block starting with ( and ending with matching ) before executing the command which uses the command block.
This means here that %confirm% is replaced twice by nothing on first run of the batch file before the command IF is executed at all. This behavior can be seen on running the batch file without echo off from within a command prompt window, see debugging a batch file.
One solution is using delayed expansion as explained by the help of command SET output on running in a command prompt window set /? on an IF and a FOR example.
But much better is avoiding command blocks where not really necessary.
In this case usage of command CHOICE for the yes/no prompt is also better than set /P.
#echo off
cd \
goto Begin
:PromptUser
%SystemRoot%\System32\choice.exe /C YN /N /M "Do you want to choose another directory (Y/N)? "
if errorlevel 2 goto :EOF
:Begin
set "UserInputPath="
set /P "UserInputPath=What Directory would you like to make? "
rem Has the user not input any string?
if not defined UserInputPath goto Begin
rem Remove all double quotes from user path.
set "UserInputPath=%UserInputPath:"=%"
rem Is there no string left anymore?
if not defined UserInputPath goto Begin
rem Does the directory already exist?
if exist "%UserInputPath%" goto PromptUser
rem Create the directory and verify if that was really successful.
rem Otherwise the entered string was invalid for a folder path or
rem the user does not have the necessary permissions to create it.
rem An error message is output by command MKDIR on an error.
mkdir "%UserInputPath%"
if errorlevel 1 goto Begin
rem Other commands executed after creation of the directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
choice /?
echo /?
goto /?
if /?
mkdir /?
rem /?
set /?
See also:
Where does GOTO :EOF return to?
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

Related

How to save a variable to a batch file?

This is my code:
set /p name=user save name
if %name%==[""]
cd c:\users\student\desktop\login system\usersXD
echo set "name=%name%"> %name%.bat
My code is not working. and i will like to load them up to view
I suggest following code for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
goto PromptName
:ErrorInvalid
echo/
echo Error: The character !InvalidChar! is not allowed in name.
echo/
endlocal
:PromptName
set "Name="
set /P "Name=User save name: "
rem Has the user not input anything?
if not defined Name goto PromptName
rem Remove all double quotes from input string.
set "Name=%Name:"=%"
rem Has the user input just double quotes?
if not defined Name goto PromptName
rem Check if the input string contains any character not allowed in a file name.
setlocal EnableDelayedExpansion
for %%I in ("<" ">" ":" "/" "\" "|") do if not "!Name:%%~I=!" == "!Name!" set "InvalidChar=%%~I" & goto ErrorInvalid
if not "!Name:?=!" == "!Name!" set "InvalidChar=?" & goto ErrorInvalid
if not "!Name:**=!" == "!Name!" set "InvalidChar=*" & goto ErrorInvalid
endlocal
cd /D "%UserProfile%\Desktop\login system\usersXD"
echo set "Name=%Name%">"%Name%.bat"
endlocal
The file name of the batch file to create must be enclosed in double quotes because it could contain a space or one of these characters &()[]{}^=;!'+,`~ which require enclosing the file name in double quotes.
This batch file should not be stored in directory %UserProfile%\Desktop\login system\usersXD on having file extension .bat as it could be overwritten during execution if the user enters a name the name of the batch file. It would be safe to have this batch file in that directory with file extension .cmd.
The batch file is still not 100% fail-safe despite the checks already added, but the user input string itself cannot result anymore in an exit of batch file execution because of a syntax error.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
See also:
Wikipedia article listing the predefined Windows environment variables like UserProfile
Single line with multiple commands using Windows batch file explaining the operator &
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Microsoft documentation page Using command redirection operators explaining the redirection operator >
Microsoft documentation page Naming Files, Paths, and Namespaces listing which characters are not allowed in file/folder names
This should work to save a variable in a bat file
If I remember
(
Echo set test=%
Echo set test=%test%
Echo set test=%test%
)>Test.bat

Batch if file not exist create else rename

if not defined filename set filename=123.txt
if not defined folder set folder=January
set BASE_DIR=C:\Users\xxx\Desktop\
set file=%BASE_DIR%%folder%\%filename%
I am having difficult time coming with algorithma to accomplish what I am trying to do.
Every time this batch script runs filename and foldercan be different.
I am trying to check if file exist if it does no need to do
anything goto end.
If file does NOT exist and there is a another file under that folder. I need to update that filename with given.
However, there might be case where I wouldnt even have folder exist.
In that case I need to make folder and file inside of that folder.
Content inside file is always empty.
I dont need code, I just need help with logic to accomplish this.
This commented batch file should be helpful for you to finish your batch file coding task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
if not defined FileName set "FileName=123.txt"
if not defined Folder set "Folder=January"
set "BASE_DIR=%UserProfile%\Desktop"
set "FilePath=%BASE_DIR%\%Folder%"
set "FullFileName=%FilePath%\%FileName%"
rem Exit batch file execution if file already exists.
if exist "%FullFileName%" goto :EOF
rem Create the entire directory structure for the file if directory
rem does not already exist. Note the backslash at end which prevents
rem condition evaluating to true if a file with name "%Folder%"
rem exists in "%BASE_DIR%".
if not exist "%FilePath%\" md "%FilePath%" 2>nul
rem The creation of the directory tree could fail for various reasons.
if not exist "%FilePath%\" (
echo Error by %~f0:
echo/
echo Directory "%FilePath%" could not be created.
echo/
pause
goto :EOF
)
rem Add here more code to create/copy/move file "%FileName%" in/to "%FilePath%".
rem For example creating an empty file:
type NUL >"%FullFileName%"
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /? ... not explicitly used here. cmd.exe automatically restores previous environment on terminating batch file execution. See this answer for details about the commands SETLOCAL and ENDLOCAL.
goto /?
if /?
md /?
rem /?
set /?
setlocal /?
type /?
See also:
Where does GOTO :EOF return to?
How to create empty text file from a batch file?
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
Wikipedia article about Windows Environment Variables for environment variable UserProfile.

Why does Windows command interpreter output the error message: ) was unexpected

I'm making a Minecraft modding tool using a batch file. But on execution of the batch file the Windows command interpreter outputs the syntax error message:
) was unexpected
I can't figure out why.
This is my code:
#echo off
cd mods
setlocal enabledelayedexpansion
set "selected=1"
call:print 1
call:print 2
:menu
choice /c wse>nul
if "%errorlevel%"=="2" (
if not !selected! GEQ !a! (
set /a "selected+=1"
cls
call:print 1
call:print 2
)
)
if "%errorlevel%"=="1" (
if not !selected!==1 (
set /a "selected-=1"
cls
call:print 1
call:print 2
)
)
if "%errorlevel%"=="3" (
)
goto menu
:print
if "%1"=="1"set a=0
echo.
if "%1"=="1" (
echo Uninstalled:
) else (
echo Installed:
)
echo.
for %%f in (*.jar) do (
if "%1"=="1" (
if NOT EXIST
"C:/Users/Coornhert/AppData/Roaming/.minecraft/mods/%%~nf.jar" (
set /a "a+=1"
if "!a!"=="!selected!" (
echo -%%~nf
) else (
echo %%~nf
)
set "b=!a!"
)
) else (
if EXIST "C:/Users/Coornhert/AppData/Roaming/.minecraft/mods/%%~nf.jar" (
set /a "a+=1"
if "!a!"=="!selected!" (
echo -%%~nf
) else (
echo %%~nf
)
set "b=!a!"
)
)
)
goto :eof
And it works, but when I hit s, execution terminates with the error message.
Folder structure of folder containing the batch file:
mods
Foo.jar
Foo2.jar
Folder structure of target folder:
C:\Users\Coornhert\AppData\Roaming\.minecraft\mods
Foo.jar
I partly do not understand what this batch file should do, but here is the batch file rewritten with several improvements.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem cd /D "%~dp0mods"
pushd "%~dp0mods"
set "a=0"
set "selected=1"
call :PrintIt 1
call :PrintIt 2
:Menu
choice /C wse /N
if errorlevel 3 popd & endlocal & goto :EOF
if errorlevel 2 goto AddOne
if %selected% == 1 goto Menu
set /A selected-=1
cls
call :PrintIt 1
call :PrintIt 2
goto Menu
:AddOne
if %selected% GEQ %a% goto Menu
set /A selected+=1
cls
call :PrintIt 1
call :PrintIt 2
goto Menu
:PrintIt
if %1 == 1 set "a=0"
echo/
if %1 == 1 (echo Uninstalled:) else echo Installed:
echo/
for %%I in (*.jar) do (
if %1 == 1 (
if not exist "%APPDATA%\.minecraft\mods\%%~nI.jar" (
set /A a+=1
if !a! == %selected% (echo -%%~nI) else echo %%~nI
set "b=!a!"
)
) else (
if exist "%APPDATA%\.minecraft\mods\%%~nI.jar" (
set /A a+=1
if !a! == %selected% (echo -%%~nI) else echo %%~nI
set "b=!a!"
)
)
)
goto :EOF
It does nothing useful as is, but batch code in question is also not useful at all.
The applied improvements are:
The command SETLOCAL is moved to top of file. The reason is:
It pushes path of current directory on stack.
It pushes state of command extensions on stack.
It pushes state of delayed expansion on stack.
It pushes the memory address of the current environment variables table on stack.
It creates a copy of the current environment variables table in memory and makes this new environment variables table active.
It sets command extensions and delayed expansion according to the specified parameters if the command is called with parameters at all.
The command ENDLOCAL is executed before leaving batch file. The reason is:
It deletes the current environment table which means no environment variable defined in this batch file exists anymore after ENDLOCAL except it existed already before execution of command SETLOCAL.
It pops memory address of previous environment table from stack and uses this address resulting in restoring initial environment variables.
It pops state of delayed expansion from stack and disables/enables delayed expansion accordingly.
It pops state of command extensions from stack and disables/enables command extensions accordingly.
It pops previous current directory path from stack and sets current directory to this path to restore the current directory.
So the entire command process environment is restored on exit of this batch file to exactly the same environment as it was on starting the batch file.
This makes it possible to call this batch file from within another batch file or from within a command prompt window with no impact on calling batch file or command process.
The command CD could be extended to include drive and path of argument 0 which is the full path of the batch file ending with a backslash because the subdirectory mods is most likely always expected in directory of the batch file and it should not matter what is the current directory on running the batch file.
But cd /D "%~dp0mods" could fail if the batch file is located on a network share accessed using UNC path and therefore command PUSHD is used instead working with enabled command extensions also for UNC paths.
In all programming and scripting languages it is required that variables are defined and initialized with a value before being used the first time. For that reason the environment variables a and selected are defined at top of the batch file with default values. By the way: a is a very bad name for a variable. Why? Search for a in batch file. It is quite often found on not using special find features like whole word only, isn't it.
PRINT is a command as it can be seen on running in a command prompt window print /?. While it is possible to use command names as labels or as names for subroutines, it is not advisable to do so as it could be confusing for readers of the batch file.
The command CHOICE has the option /N to hide the list of choices in the prompt. It is better to use this option than redirecting the output of CHOICE to device NUL.
The very old but still valid Microsoft support article Testing for a Specific Error Level in Batch Files explains that if errorlevel X means that the condition is true if the exit code of previous command or application is greater or equal X. The command CHOICE with 3 choices exits always with 1, 2 or 3 as exit code. So it is best to use:
if errorlevel 3 rem Do something on third choice avoiding fall through to next line.
if errorlevel 2 rem Do something on second choice avoiding fall through to next line.
Do something on first choice.
The advantage of using this method is that it even works with CHOICE within a command block on which if %ERRORLEVEL% == X fails because of delayed expansion would be required and usage of if !ERRORLEVEL! == X.
The integer comparison if %selected% GEQ %a% would not work if the two arguments would be enclosed in double quotes as the double quotes are also interpreted as part of the arguments to compare. For that reason using if "%selected%" GEQ "%a%" would result in running a string comparison instead of an integer comparison. For more information about comparing values with IF look on answer on Exit a for loop in batch.
It is safe here to omit the double quotes also on the other IF conditions with == operator running string comparisons because the environment variables selected and a must be both defined before running this IF condition and therefore both variables are defined at top of the batch file.
The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains why set "variable=value" should be always used to assign a value to an environment variable or delete an environment variable on omitting the value. And this answer also explains why on set /A variable=expression the double quotes can be usually omitted as whitespace characters are interpreted completely different within an arithmetic expression. The exception is usage of set /A with 1 or more commands on same command line on which double quotes around variable=expression would be also needed.
The batch file should be exited when the batch file user enters e or E to take third choice. This could be done with just goto :EOF, or with exit /B which is an alias for goto :EOF, or with just exit which always exits entire command process independent on calling hierarchy which is not recommended. Windows command interpreter would implicitly restore the initial stack before finishing batch file execution. But it is nevertheless good coding practice to pop from stack with code which was pushed on stack before with code. For that reason there is used popd & endlocal & goto :EOF. See answer on Single line with multiple commands using Windows batch file for more information about usage of multiple commands on one command line.
The list of predefined environment variables of used user account is output on running in a command prompt window the command set. One predefined Windows environment variable is APPDATA with path to application data of current user account. This environment variable should be used instead of a fixed path to application data directory of user account.
And the directory separator on Windows is the backslash character \ and not slash character / as on Unix and Mac.
The usage of f as loop variable is not recommended as this is also a loop variable modifier. %%~f can be interpreted by Windows command interpreter as value of loop variable f without surrounding double quotes or as incomplete loop variable reference because of missing loop variable after %%~f which could be also interpreted as full file name of ?. So it is better to use # or $ as loop variable or upper case letters to avoid such a confusion on interpreting the loop variable reference. Loop variables are case-sensitive.
I prefer for IF conditions with an ELSE branch the coding style
if condition (
command
) else (
command
)
But here in this batch file with command being just a short ECHO command the code is better readable on being more compact with using:
if condition (echo short message) else echo other short message
Delayed expansion for an environment variable referenced within a command block started with ( and ending with matching ) is only needed if the environment variable is also modified in same command block. Therefore environment variable a must be referenced in body of FOR with usage of delayed expansion while environment variable selected can be referenced as usual because of not modified within this command block at all.
It is better to use echo/ to output an empty line instead of echo.. For the reason read the DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
For a basic understanding of the used commands, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
choice /?
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?
setlocal /?

How to ask user of batch file for a folder name/path?

I was able to create this batch file to move certain files from one folder to another. But I want to be able to use it also on different folders. For instance here I'm only moving files from UTS16. I want to use this batch file also for other folders like UTS15, UTS14, UTS13, UTS12, etc.
What do I need to change in code to ask the batch user on which folder to run? What am I missing?
#echo off
SET /P letter=Please give your drive letter and press ENTER:
ECHO %letter%
PAUSE
SET Datefolder="%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%%time:~3,2%"
MD "%Datefolder%"
mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.dbf" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.cdx" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~cd" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~db" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.fpt" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\RTBackup%Datefolder%\zipdata.dbf" "%letter%:\UTS16\Database\"
pause
start "" %letter%:\UTS16/dbrepair.exe
I suggest for your task following commented batch file:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
goto UserPrompt
rem Define environment variable BaseFolder with a double quote character as
rem value in case of the user enters nothing on prompt in which case the
rem variable BaseFolder is still defined with the double quote as value.
rem Then let the user enter the folder path or drag and drop
rem the folder over the console window to enter the path.
rem Next remove all double quotes from folder path and test
rem if the variable BaseFolder still exists with a value.
rem Last replace forward slashes by backslashes in case of user entered
rem the path with forward slashes, make sure the folder path does not
rem end with a backslash and test if the folder really exists in case of
rem user has made a typing mistake on entering manually the folder path.
rem Run the backup and repair operation if entered folder exists as expected.
:UserPrompt
cls
echo/
echo Please type the database base folder path and press ENTER.
echo/
echo Or alternatively drag ^& drop the folder from Windows
echo Explorer on this console window and press ENTER.
echo/
set "BaseFolder=""
set /P "BaseFolder=Path: "
set "BaseFolder=!BaseFolder:"=!"
if "!BaseFolder!" == "" goto UserPrompt
set "BaseFolder=!BaseFolder:/=\!"
if "!BaseFolder:~-1!" == "\" set "BaseFolder=!BaseFolder:~0,-1!"
if "!BaseFolder!" == "" goto UserPrompt
echo/
if not exist "!BaseFolder!\Database\*" (
echo There is no folder "!BaseFolder!\Database".
echo/
choice "Do you want to enter the path once again "
if errorlevel 2 goto ExitBatch
goto UserPrompt
)
set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%"
rem For German date/time format which is for DATE TIME: dd.mm.yyy hh:mm:ss,xx
rem set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"
if exist "%BackupFolder%\*" goto MakeBackup
md "%BackupFolder%"
if errorlevel 1 (
echo/
echo Error: Failed to create backup folder !BackupFolder!
echo/
choice "Repair without making a backup "
if errorlevel 2 goto ExitBatch
goto RunRepair
)
:MakeBackup
echo Making a backup to folder !BackupFolder! ...
move /-y "%BaseFolder%\Database\*.dbf" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.cdx" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~cd" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~db" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.fpt" "%BackupFolder%" 2>nul
move /-y "%BackupFolder%\zipdata.dbf" "%BaseFolder%\Database\" 2>nul
:RunRepair
echo/
echo Running database repair ...
"%BaseFolder%\dbrepair.exe"
:ExitBatch
endlocal
Please read first answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explaining the difference between set variable="value" and set "variable=value".
The environment variable Datefolder was created in batch file in question with double quotes included in environment variable value resulting in expanding
"%letter%:\UTS16\Database\RTBackup%Datefolder%"
for example to
"C:\UTS16\Database\RTBackup"2017_01_13_1250""
which of course is not good. Double quotes inside a double quoted string is in general not correct.
And the command line
mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"
expanded for example to
mkdir C:\UTS16\Database\"RTBackup"2017_01_13_1250""
Error correction of Windows must do overtime to fix the folder paths.
The date/time format of the environment variables DATE and TIME depends on Windows region and language settings of current user. I needed a different line to define the backup folder with date and time in name for my German Windows machine. There are region independent solutions posted for example at How to get current datetime on Windows command line, in a suitable format for using in a filename? However, if the faster command line using the environment variable DATE and TIME work on the computers where this batch file is used, there is no need to replace that line with a region independent solution.
The batch file in this answer uses delayed expansion of environment variables mainly to prevent an exit of batch processing in case of user enters by mistake a path string which results without usage of delayed expansion in a syntax error.
The user can drag & drop the folder also for example from Windows Explorer over the console window to enter the folder path on prompt.
echo/ is used to output a blank line which is better than echo. as explained by DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/
The ampersand & is interpreted by Windows command interpreter as operator for executing multiple commands in one command line in an unquoted string. For that reason it is necessary to escape this character with the caret character ^ to be interpreted as literal character to echo into the console window. See Single line with multiple commands using Windows batch file for details on meaning of &, && and || in a command line in an unquoted string and not escaped with ^.
The command choice is used on asking if the user wants to proceed on error or exit the batch file. This command appends to prompt text in square brackets the keys to press for Yes or No in language of Windows and a question mark, i.e. [Y,N]? on English Windows or [J,N]? on German Windows. choice does not allow any other key before exiting. The exit code assigned to errorlevel is 2 on No and 1 on Yes.
The Microsoft support article Testing for a Specific Error Level in Batch Files explains the usage of if errorlevel to test on exit code of previous command or application. In this case it is enough to test on errorlevel being greater or equal 2 to exit batch processing on an error in case of user chooses No.
The batch file does not check if "%BaseFolder%\dbrepair.exe" really exists before it tries to execute this application. It would be good if that additional check with appropriate error message for the user is added, best before creating the backup folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
md /?
move /?
set /?
setlocal /?
And read also the Microsoft TechNet article Using command redirection operators for an explanation of 2>nul.

Using Variables to Change Directories

I am new to writing batch files. I want to create a batch file that will allow me to change 2 directories using variables. What I have below is what I have thus far. Any ideas?
#echo off
S:
cd AAA
set /p CLIENTCODE=CLIENTCODE?
cd %CLIENTCODE%
pause
set /p SCHEMANAME=SCHEMANAME?
cd %SCHEMANAME%
pause
Try following batch code:
#echo off
setlocal
set "ClientCode=AAA"
set "SchemaName=5H"
:UserPrompt
cls
set /P "ClientCode=Enter client code (default: %ClientCode%): "
set /P "SchemaName=Enter schema name (default: %SchemaName%): "
if not exist "S:\%ClientCode%\%ClientCode%%SchemaName%" goto InputError
cd /D "S:\%ClientCode%\%ClientCode%%SchemaName%"
endlocal
goto :EOF
:InputError
echo.
echo Client code "%ClientCode%" or schema name "%SchemaName%" is not valid.
set "InputAgain=Y"
set /P "InputAgain=Enter data again (Y/N)? "
if /I "%InputAgain%" == "Y" goto UserPrompt
if /I "%InputAgain%" == "YES" goto UserPrompt
endlocal
This batch file first defines defaults for client code and schema name making it possible for the user to simply hit key RETURN or ENTER when defaults are okay.
Next the window is cleared and the user is prompted for client code and schema name. The input of the user is not validated at all.
A very simple check is made if the appropriate directory (or file) exists.
The current directory is changed if a directory according to entered data exists.
If the directory does not exist, the user is asked if data input should be repeated in case of a typing mistake. The user can input Y or YES in any case to redo data input. Otherwise the batch script exits without changing the directory.
There is no real effort made on validating user input strings and verifying if the entered strings really lead to a directory and not a file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
echo /?
endlocal /?
goto /?
if /?
set /?
setlocal /?

Resources