RedWolf
Deity
Hi Guys/Girls,
I have a windows batch scripting question for you. It's the one that uses the "cmd.exe" (ships with windows 2000, NT, XP etc)
I'm having problems because my script has to call a command that contains brackets.. however the batch language hates it and immediately terminates the script. I have an example below - it's nonsensical and doesn't do anything usefull but was created as means of demonstrating the problem. There's no point in trying to find an "alternate" solution for me because I DO have to run a command with brackets... so I need it to work SOMEHOW.
Explanation for the sample:
myfile.txt and myfile(1).txt are simply empty text files.
The system checks "myfile.txt" for the text "NEW TEMPLATE". If it finds it, it returns an errorcode of 0. If it does not it returns 1. Since it will NOT find it, it returns 1 and runs the ELSE code. At which time it runs the command contained in the "mycommand% variable which tries to delete "myfile(1).txt". The file out.txt is simply used a a trace so you can see WHEN it crashes.
The problem is that "mycommand" contains brackets. The batch language realizes this as soon as it hits the if statement (since ifs are pre-compiled as soon as they start). At this point the batch crashes and exits. You can tell this because the file out.txt is empty.
Anybody have any ideas? A solution will save me many hours of work (professionally) and probably make my bosses happier.
Thanks!
rem START TEST BATCH SCRIPT ************
set mycommand=del myfile.txt
findstr /B /C:"NEW TEMPLATE" myfile(1).txt
if NOT ERRORLEVEL 1 (
echo Failed 1 > out.txt
) ELSE (
echo Failed 2 > out.txt
%mycommand%
)
echo RAN THROUGH TO THE END >> out.txt
rem END TEST BATCH SCRIPT ************
I have a windows batch scripting question for you. It's the one that uses the "cmd.exe" (ships with windows 2000, NT, XP etc)
I'm having problems because my script has to call a command that contains brackets.. however the batch language hates it and immediately terminates the script. I have an example below - it's nonsensical and doesn't do anything usefull but was created as means of demonstrating the problem. There's no point in trying to find an "alternate" solution for me because I DO have to run a command with brackets... so I need it to work SOMEHOW.
Explanation for the sample:
myfile.txt and myfile(1).txt are simply empty text files.
The system checks "myfile.txt" for the text "NEW TEMPLATE". If it finds it, it returns an errorcode of 0. If it does not it returns 1. Since it will NOT find it, it returns 1 and runs the ELSE code. At which time it runs the command contained in the "mycommand% variable which tries to delete "myfile(1).txt". The file out.txt is simply used a a trace so you can see WHEN it crashes.
The problem is that "mycommand" contains brackets. The batch language realizes this as soon as it hits the if statement (since ifs are pre-compiled as soon as they start). At this point the batch crashes and exits. You can tell this because the file out.txt is empty.
Anybody have any ideas? A solution will save me many hours of work (professionally) and probably make my bosses happier.
Thanks!
rem START TEST BATCH SCRIPT ************
set mycommand=del myfile.txt
findstr /B /C:"NEW TEMPLATE" myfile(1).txt
if NOT ERRORLEVEL 1 (
echo Failed 1 > out.txt
) ELSE (
echo Failed 2 > out.txt
%mycommand%
)
echo RAN THROUGH TO THE END >> out.txt
rem END TEST BATCH SCRIPT ************