I'm a starting to learn about batch files and I'm trying to make a batch file that will generate another batch file. I've been trying to use this, without success: (
@echo off
echo
echo.
echo.set TEMPSAVE=%path%
if exist %TEMPSAVE% del %TEMPSAVE%echo Removing files from %name%...
echo.
echo.
echo Done! Please Press any key to exit
echo.
@pause) >%name%_save_removal.bat
The variables %path% and %name% are being set by data entered in the main bat. Will those variables be accepted? Or did I get that it wrong, and the code between () should be self contained?
Thanks a mil for your help!
Your script is very obscure to me, just be aware never modify the path system variable since that allows the OS to find and load executables. Use e.g. myPath if you want to enter a location pathname.
Thank you for the quick reply IVO! Indeed the script looks dodgy on its own. here is what it should look like in full: @echo off
echo.
set /p name=Name:
echo.
cls
echo.
set /p mypath=Path:
echo.
cls(
@echo off
echo
echo.
echo.set TEMPSAVE=%mypath%
if exist %TEMPSAVE% del %TEMPSAVE%echo Removing files from %name%...
echo.
echo.
echo Done! Please Press any key to exit
echo.
@pause) >%name%_save_removal.bat
@pause
but it doesn't seem to work, any tips?
To create a batch from inside another batch you have to prefix each line with echo. see the following code. Notice I nullify your if statement since del can't remove a path (use RD instead) @echo off
echo.
set /p name=Name:
echo.
cls
echo.
set /p mypath=Path:
echo.
cls(
ECHO.@echo off
ECHO.echo.
ECHO.echo.
ECHO.echo.ECHO.set TEMPSAVE=%mypath%
ECHO.::if exist %TEMPSAVE% del %TEMPSAVE%ECHO.echo Removing files from %name%...
ECHO.echo.
ECHO.echo.
ECHO.echo Done! Please Press any key to exit
ECHO.echo.
ECHO.pause
) >%name%_save_removal.batpause
Hey IVO, Thanks a lot for that! Things a looking clearer at my end! Thanks for the RD tip!
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |