Solved batch file that generates another batch file

September 16, 2011 at 05:24:58
Specs: Windows 7
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!



See More: batch file that generates another batch file

Report •

#1
September 16, 2011 at 06:05:05
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.

Report •

#2
September 16, 2011 at 06:34:21
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?


Report •

#3
September 16, 2011 at 07:10:19
✔ Best Answer
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.bat

pause


Report •

Related Solutions

#4
September 16, 2011 at 07:54:24
Hey IVO,

Thanks a lot for that! Things a looking clearer at my end! Thanks for the RD tip!


Report •

Ask Question