A windows batch question. HELP!

RedWolf

Deity
Joined
Feb 2, 2001
Messages
3,113
Location
Ontario, Canada
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 ************
 
Well I don't have a clue about that I'm afraid. My only suggestions would be to try the "help.com" programme that I think was in dos 6.22 (XP doesn't have it, I just checked). Also, you might prefer a VBScript instead, which is easier to use and more powerful, and still has plenty of support from Microsoft.
 
Chairman Meow said:
Have you tried putting quotes around the command? (I'm not sure whether it would be double or single quotes in a DOS batch file, though...)

You mean like this?

'%mycommand%'


or like this:

set mycommand='del myfile.txt'

I think I tried the second one but not the first one. Didn't work before but I'll give it another go (i tried so many things I'm not really sure anymore)
 
Back
Top Bottom