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 /?
Related
I have written a batch file that I use for file management. The batch file parses an .XML database to get a list of base filenames, then allows the user to move/copy those specific files into a new directory. The program prompts the user for a source directory and the name of the .XML file. I would like the program to default the variables to the last used entry, even if the previous CMD session has closed. My solution has been to ask the user for each variable at the beginning of the program, then write those variables to a separate batch file called param.bat at the end like this:
#echo off
set SOURCEDIR=NOT SET
set XMLFILE=NOT SET
if exist param.bat call param.bat
set /p SOURCEDIR=The current source directory is %SOURCEDIR%. Please input new directory or press [Enter] for no change.
set /p XMLFILE=The current XML database is %XMLFILE%. Please input new database or press [Enter] for no change.
REM {Rest of program goes here}
echo #echo off>param.bat
echo set SOURCEDIR=%SOURCEDIR%>>param.bat
echo set XMLFILE=%XMLFILE%>>param.bat
:END
I was hoping for a more elegant solution that does not require a separate batch file and allows me to store the variable data within the primary batch file itself. Any thoughts?
#echo off
setlocal
dir /r "%~f0" | findstr /c:" %~nx0:settings" 2>nul >nul && (
for /f "usebackq delims=" %%A in ("%~f0:settings") do set %%A
)
if defined SOURCEDIR echo The current source directory is %SOURCEDIR%.
set /p "SOURCEDIR= Please input new directory or press [Enter] for no change. "
if defined XMLFILE echo The current XML database is %XMLFILE%.
set /p "XMLFILE=Please input new database or press [Enter] for no change. "
(
echo SOURCEDIR=%SOURCEDIR%
echo XMLFILE=%XMLFILE%
) > "%~f0:settings"
This uses the Alternate Data Stream (ADS) of the batchfile to
save the settings.
NTFS file system is required. The ADS stream is lost if the
batchfile is copied to a file system other than NTFS.
The dir piped to findstr is to determine if the
stream does exist before trying to read from it.
This helps to avoid an error message from the for
loop if the ADS does not exist.
The for loop sets the variable names and values read from the ADS.
Finally, the variables are saved to the ADS.
Note:
%~f0 is full path to the batchfile.
See for /? about all modifiers available.
%~f0:settings is the batchfile with ADS named settings.
dir /r displays files and those with ADS.
Important:
Any idea involving writing to the batchfile could result
in file corruption so would certainly advise a backup of
the batchfile.
There is one way to save variables itself on the bat file, but, you need replace :END to :EOF
:EOF have a good explained in this link .:|:. see Where does GOTO :EOF return to?
Also, this work in fat32/ntfs file system!
You can write the variables in your bat file, and read when needs:
Obs.: Sorry my limited English
#echo off & setlocal enabledelayedexpansion
set "bat_file="%temp%\new_bat_with_new_var.tmp"" & type nul >!bat_file! & set "nop=ot Se"
for /f %%a in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x40"') do set "delim=%%a"
type "%~f0"| findstr "!delim!"| find /v /i "echo" >nul || for %%s in (SOURCEDIR XMLFILE) do set "%%s=N!nop!t"
if defined SOURCEDIR echo/!SOURCEDIR!%delim%!XMLFILE!%delim%>>"%~f0"
for /f "delims=%delim% tokens=1,2" %%a in ('type "%~f0"^| findstr /l "!delim!"^| find /v /i "echo"') do (
set /p "SOURCEDIR=The current source directory is %%~a. Please input new directory or press [Enter] for no change: "
set /p "XMLFILE=The current XML database is %%~b. Please input new database or press [Enter] for no change: "
if /i "!old_string!" neq "!SOURCEDIR!!delim!!XMLFILE!!delim!" (
type "%~f0"| findstr /vic:"%%~a!delim!%%~b!delim!">>!bat_file!"
copy /y !bat_file! "%~f0" >nul
echo/!SOURCEDIR!!delim!!XMLFILE!!delim!>>%~f0"
goto :_continue_:
))
:_continue_:
rem :| Rest of program goes here | replace/change last command [goto :END] to [goto :EOF]
goto :EOF
rem :| Left 2 line blank above, because your variable will be save/read in next line above here |:
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?
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.
I want to create a bat file asking for a user input which will ask for some choices:
#echo off
MKDIR D:\BatFiles\File
SET /P Output="D:\BatFiles\File"
ECHO Select Task
ECHO ==========
#echo off
title Task List Creator
:homescreen
ECHO
Echo 1.) Create Notepad Task File
Echo 2.) Exit
Echo.
set /p input=Type Choice :
if "%input%"=="1" goto getInfo
if "%input%"=="2" exit
Pause
:getInfo
set /p VarOne=Enter Type:
set /p VarTwo=Enter Number:
set /p VarThree=Enter Name:
echo Task Type=%VarOne% >> %Output%\test.txt
echo Task Number=%VarTwo% >> %Output%\test.txt
echo Task Name=%VarThree% >> %Output%\test.txt
echo Entry successfully written
Pause
goto finished
:finished
echo Do you want to create a new set of entry?
set /p response= Y or N?
if "%response%"=="Y" goto getInfo
if "%response%"=="N" goto homescreen
--The problem with this code is that I want to create more than 2 entries. This code only creates an output file if user has only one set of entries. If user creates 2 or more, the output file is not created and data entered appears only when user runs the bat file again and only enters one set of data. Sorry about the lame question, I'm just a batch file beginner here.
Look on this code:
#echo off
title Task List Creator
setlocal EnableExtensions EnableDelayedExpansion
set "OutputFolder=D:\BatFiles\File"
set FileNumber=0
:HomeScreen
cls
echo Select Task
echo ===========
echo.
echo 1 ... Create Notepad Task File
echo 2 ... Exit
echo.
set "Input=2"
set /P "Input=Your choice: "
if "!Input!"=="1" goto PrepareTaskFile
if "!Input!"=="2" endlocal & goto :EOF
goto HomeScreen
:PrepareTaskFile
set /A FileNumber+=1
set "OutputFile=%OutputFolder%\test%FileNumber%.txt"
if exist "%OutputFile%" del "%OutputFile%"
:GetInfo
echo.
set "VarOne="
set "VarTwo="
set "VarThree="
:EnterType
set /P "VarOne=Enter type: "
if not defined VarOne goto EnterType
:EnterNumber
set /P "VarTwo=Enter number: "
if not defined VarTwo goto EnterNumber
:EnterName
set /P "VarThree=Enter name: "
if not defined VarThree goto EnterName
if not exist "%OutputFolder%" mkdir "%OutputFolder%"
echo Task Type=!VarOne!>>"%OutputFile%"
echo Task Number=!VarTwo!>>"%OutputFile%"
echo Task Name=!VarThree!>>"%OutputFile%"
echo.
echo Entry successfully written.
echo.
pause
echo.
echo Do you want to create a new set of entry?
echo.
set "Response=N"
set /P "Response=Y or N? "
if /I "!Response!"=="Y" goto GetInfo
goto HomeScreen
The environment variable on prompt keeps its current value if the user just hits RETURN or ENTER. Therefore it is advisable to define a default value or undefine a variable before prompting the user.
The entered strings assigned to the variables are referenced with usage of delayed expansion in case of user enters something not expected which could result in a syntax error and therefore exit of batch processing on referencing the entered strings with immediate expansion. For example a string comparison would fail with a syntax error if the user enters a string with a double quote.
See How to set environment variables with spaces? why using double quotes as it can be seen here on set "variable=string value" and set /P "variable=prompt text".
The space character left of redirection operator >> in code of question would be also written into the file. This should be avoided by removing it and reference the variables with delayed expansion in case of the variable value is a number with value 1, 2, 3, ... to avoid a wrong handle redirection, see the Microsoft article about Using command redirection operators.
On usage of set /P for a menu instead of command choice it must be always taken into account that the user enters something not suggested. So if the user enters on first prompt whether 1 nor 2, there must be code which defines the behavior on the invalid input.
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 /?
del /?
echo /?
endlocal /?
goto /?
if /?
mkdir /?
pause /?
set /?
setlocal /?
title /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of & between endlocal and goto :EOF.
So, I created a program, which can create and delete folders on the desktop. And I have a problem, when I just write a single space for the name I got the error "Please write a valid folder name!" so it works, but if I'm using more than one spaces, it says "Folder created successfully", and I want to make this program to make an error when I only using spaces in the folder's name. Please help!
Here's a part of my code (the full code is 132 lines long)
set Choice=
set /p Choice="Choose an option: "
if '"%Choice%"'=='"1"' goto masodik
if '"%Choice%"'=='"2"' goto negyedik
if '"%Choice%"'=='"3"' goto otodik
if '"%Choice%"'=='"4"' goto harmadik
if not '"%Choice%"'=='"1"' goto hiba2
if not '"%Choice%"'=='"2"' goto hiba2
if not '"%Choice%"'=='"3"' goto hiba2
if not '"%Choice%"'=='"4"' goto hiba2
:masodik
cls
echo Create a folder
echo ---------------
echo.
cd "%systemdrive%/documents and settings/%username%/desktop"
echo Enter the folder's name!
echo.
set /p mappaneve="The folder's name: "
if "%mappaneve%" EQU "" goto hiba
if EXIST "%mappaneve%" goto hiba3
md "%mappaneve%"
cls
echo Create a folder
echo ---------------
echo.
echo Successfully created "%mappaneve%"!
timeout /t 3 >nul
cls
goto elso
if "%mappaneve: =%" EQU "" goto hiba
ie, replace all spaces with nothing and if the result is nothing then the input must have been all spaces (if that was what you were asking)
quotes is used in variable asignation to make sure space are handle correctly. don't include quotes inside your variable unless when you want to use it.try change your code into this.
set /p "mappaneve=The folder's name: "
Magoo posted the right code in his short answer.
Here is a much longer answer with commented batch code and additional explanations on the various improvements in this code in comparison to code in question post.
set "UserChoice="
set /p "UserChoice=Choose an option: "
if "%UserChoice%" == "1" goto masodik
if "%UserChoice%" == "2" goto negyedik
if "%UserChoice%" == "3" goto otodik
if "%UserChoice%" == "4" goto harmadik
rem None of the 4 valid numbers was entered.
goto hiba2
:masodik
rem Set current directory to desktop directory of current user.
cd /D "%USERPROFILE%\Desktop"
cls
echo Create a folder
echo ---------------
echo.
rem Define a double quote as default value to be able to remove all
rem double qotes from input string with no syntax error even if the
rem user just hits key RETURN or ENTER without entering any string.
set "mappaneve=""
set /p "mappaneve=Enter folder name: "
rem Remove all double quotes from entered folder name.
set "mappaneve=%mappaneve:"=%"
rem Check if user has entered anything at all and if entered folder name
rem does not exist of only 1 or more spaces by using environment variable
rem substitution which removes for string comparison all spaces from the
rem entered folder name.
if "%mappaneve%" == "" goto hiba
if "%mappaneve: =%" == "" goto hiba
rem Appending \* makes sure to test on existence of a folder and not a file.
rem It does not matter if entered folder name ends already with a backslash.
rem Does the folder already exist?
if exist "%mappaneve%\*" goto hiba3
rem Create the folder and verify if that was successful with
rem saving error message into a temporary file for output below.
md "%mappaneve%" 2>"%TEMP%\%~n0_FolderError.tmp"
if errorlevel 1 goto FolderError
rem On successful creation of the folder an empty file is
rem created which should be removed before processing further.
del "%TEMP%\%~n0_FolderError.tmp"
cls
echo Create a folder
echo ---------------
echo.
echo Created successfully "%mappaneve%".
timeout /t 3 >nul
cls
goto elso
:FolderError
cls
echo Failed to create folder "%mappaneve%".
echo.
echo Error message:
echo.
type "%TEMP%\%~n0_FolderError.tmp"
del "%TEMP%\%~n0_FolderError.tmp"
echo.
pause
The improvements are:
Comparing strings with command IF should be done with using surrounding double quotes only. The single quotes are removed from the 4 string comparisons.
Of course if the batch user enters for example "2" (number 2 with double quotes) the batch execution would be exited by command processor because of a syntax error caused by processing if ""2"" == "1" goto masodik. Using delayed variable expansion would be one solution for this issue.
The 4 lines with if not '"%Choice%"'==... can be replaced simply by goto hiba2 as they are all definitely true after the first 4 string comparisons.
choice (SS64 article) is a standard Windows command. Therefore it is advisable to avoid choice (Microsoft article) as name for an environment variable or label. UserChoice (CamelCase spelling for easier reading) is used instead of Choice.
Command choice would be a very good alternative for first user prompt because then the user can't enter something different than 1, 2, 3, or 4.
There is predefined by Windows the environment variable USERPROFILE containing path to the user´s profile directory containing the directory Desktop and other user account related directories. This variable should be used instead of building path with other variables.
The directory separator on Windows is the backslash character and not the forward slash character. Windows supports in the meantime also directory paths with / instead of \, but it is nevertheless better to use the right directory separator.
The user´s profile directory can be on a different drive than system drive. Or the drive from which the batch file is executed is a different one than the drive of user´s profile directory. Therefore it is advisable to use command CD with parameter /D as otherwise changing directory could fail if current drive is different to drive of user´s profile directory.
It is advisable to define a default for an environment variable before prompting a user or clearing the variable. The user can hit just RETURN or ENTER in which case the environment variable keeps its value.
This was done for Choice, but was forgotten for mappaneve.
The user could enter a folder name with double quotes. Therefore it is advisable to remove all double quotes from entered folder name as otherwise the batch script processing would be exited by command processor because of a syntax error on further processing the batch file.
To really test existence of a folder with if exist and not also a file, it is necessary to append \* to the folder name as otherwise if exist could be true also with user input string is name of an existing file.
Creation of folder could fail because of 1 or more invalid characters in folder name, a file exists with same name already, or the user does not have permission to create a folder.
The last reason is very unlikely here with current directory being the desktop directory of the user if user really enters just a folder name and not a directory path to create anywhere a directory (tree) which would be possible here, too.
So it is advisable to test on success on creating the 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.
cd /?
cls /?
del /?
echo /?
goto /?
if /?
md /?
rem /?
set /?
timeout /?
type /?
And read also the Microsoft articles:
Testing for a Specific Error Level in Batch Files
Using command redirection operators