I'm making a bat file that prompts directory from user and make directory in that prompted directory
@echo off
set /p dir=enter directory:
cd %dir%
cd
md assets
cd assets
md folder folder2 folder
md addons
cd addons
md folder folder2 folder
I put this file one desktop so when I run this file everything happens on desktop but not the directory I prompted. What should I do ?
First recommendation: Remove (or comment out) your @echo off
so that you can see what it's doing when it runs. This will likely make it immediately clear why it's not working as you expect. Once you have it running the way you want, add @echo off
back in.
Second recommendation: You may need some validation to ensure that your %dir%
is usable to the cd
command. For example, if the %dir%
directory doesn't actually exist, the cd
command will fail. Or, if your input includes a drive letter (e.g. E:\newFolder\
) the cd
command won't get you there unless you use the /d
switch.
Third recommendation: It's good practice to put your path names and path variables in quotes, like this:
set /p "dir=enter directory:"
Here's an alternate approach I prefer, which is to avoid cd
in batch files altogether. With some input validation, thrown in.
:SetDriveLetter
set /p "drive=enter drive letter:"
:: check for colon following drive letter, add it if it's not there
:: (add whatever other input validation you think necessary)
if not "%drive:~1%"==":" set drive=%drive%:
if not exist "%drive%\" (
echo invalid drive.
goto SetDriveLetter
)
:SetDirectory
set /p "dir=enter directory:"
:: check for backslash preceding the directory, remove it if it's there
:: (add whatever other input validation you think necessary)
if ^%dir:~0,1%==^\ set dir=%dir:~1%
echo This will create folders in %drive%\%dir%\
choice /m "Continue with this operation?"
if errorlevel 2 (
echo Operation cancelled by user.
goto End
)
:: You can use the `md` command to create multiple directories in one line.
:: I prefer to separate them out, one per line, for better readability.
md "%drive%\%dir%\assets
md "%drive%\%dir%\assets\folder"
md "%drive%\%dir%\assets\folder2"
md "%drive%\%dir%\assets\addons"
md "%drive%\%dir%\assets\addons\folder"
md "%drive%\%dir%\assets\addons\folder2"
:End