Escaping everything in batch files

I've got a list of commands (generated by nmake and obtained using event tracing) that I want to run. The obvious way to do this is to put them in a batch file. This works fine for simple commands, but then one of them turned out to be the following:

C:\Windows\system32\cmd.exe /c for %I in (..\..\bin\*) do @for %J in ("%~nI.exe: $(srcdir)/bin/%~nI" " $(ECHO) generating $(@)" " $(Q) copy /y /b $(STUBPROGRAM) $(@) > nul" " $(Q) echo.>>$(@)" " $(Q) echo.>>$(@)" " $(Q) copy /b $(@)+$(srcdir:/=\)\bin\%~nI $(@) > nul" "" ) do @echo.%~J>>scriptbin.mk

which cmd barfed on:

The following usage of the path operator in batch-parameter substitution is invalid: %~nI.exe: $(srcdir)/bin/%~nI" " $(ECHO) generating $(@)" " $(Q) copy /y /b $(STUBPROGRAM) $(@) > nul" " $(Q) echo.>>$(@)" " $(Q) echo.>>$(@)" " $(Q) copy /b $(@)+$(srcdir:/=\)\bin\%~nI $(@) > nul" "" ) do @echo.%~J>>scriptbin.mk

Which I suppose shouldn't be very surprising as the act of reading commands from a batch file performs various substitutions, interpretations of special characters et cetera.

I could try to modify the stuff in the batch file by escaping things, but that would require the program that does the escaping to know everything there is to know about how cmd parses things and what needs to be escaped; I could see myself still trying to fix bugs in that this time next year.

I could write my own program to run a batch file without doing any substitutions or interpretations, but that wouldn't be as good as having batch files that can be run as-is.

Is there any way to say something like 'escape everything' or 'turn off substitutions for the rest of this file' or anything like that?

-------------Problems Reply------------

It seems to solve this problem if you run the batch file with:

cmd <foo.bat

Category:windows Views:92 Time:2019-02-28

Related post

  • How do I escape ampersands in batch files? 2009-08-25

    How do I escape ampersands in a batch file (or from the Windows command line) in order to use the start command to open web pages with ampersands in the URL? Double quotes will not work with start; this starts a new command-line window instead. Updat

  • Escaping 'echo' in batch files 2010-03-09

    I'm trying to create a bash command file on the fly from within an Windows 7 DOS shell: :: inside the .BAT file .. :: check we are in the right directory echo pwd > command.txt :: get the shell to echo its environment variables :: !!!! How do I ge

  • Escaping ampersands in Windows batch files 2010-11-03

    I realise that you can escape ampersands in batch files using the hat character e.g. echo a ^& b a & b But I'm using the command for /f "tokens=*" %%A IN ('DIR /B /A-D /S .acl') DO ProcessACL.cmd "%%A" which is finding all the files named '.a

  • Escaping a batch file echo that starts with a forward slash and question mark 2011-06-29

    Bit of a tricky one. How can I correctly escape the following in a batch file? echo /? display this help text This particular combination of characters is treated as an "ECHO /?" command: C:\Batch>ECHO /? display this help text Displays messages,

  • How to escape variables with parentheses inside if-clause in a batch file? 2011-10-19

    Running this batch file @echo off set a=some value with (parentheses) inside if 1 == 1 ( set PATH=%a% ) gives inside was unexpected at this time. error. How to escape a variable to avoid this error? --------------Solutions------------- You can use tw

  • Escape user input in windows batch file 2012-03-28

    I have a windows batch file that accepts a password as user input: SET /P PASSWORD=Password: This password might have characters that need escaping like !. The PASSWORD variable then gets passed to other batch files using CALL CALL Foo.Bat %PASSWORD%

  • What is wrong with my batch file? Skips over everything after the SQL command 2015-01-14

    I am trying to pull folder names from a database, and use this script to loop into those specific folders in a directory and delete certain file names that are stored in another txt file. For some reason it runs the SQL command but it skips everythin

  • What does this batch file code do? 2009-09-25

    What does this bat code do? for /f %%i in ('dir /b Client\Javascript\*_min.js') do ( set n=%%~ni set t=!n:~0,-4! cp Client\Javascript\%%i build\Client\Javascript\!t!.js ) What does %%~ni,~n:~0,-4!,%%i,!t! mean? --------------Solutions------------- Ke

  • Batch File/Script to delete Several non sequential lines from text file(s) 2009-12-03

    I have read the suggestion listed here. The batch file solution seems to work untill I get to about 5 ignore conditional strings and then the output starts to ignore very large sections of the text files(s) even things it shouldn't. Basically, I have

  • How to run a PowerShell script within a DOS batch file 2010-04-09

    How do I have a PowerShell script embedded within the same file as a DOS batch script? I know this kind of thing is possible in other scenarios: Embedding SQL in a DOS batch script using sqlcmd and a clever arrangements of goto's and comments at the

  • Batch file include external file for variables 2010-05-04

    I have a batch file and I want to include external file containing some variables (say configuration variables). Is it possible? --------------Solutions------------- Note: I'm assuming Windows batch files as most people seem to be unaware that there

  • Windows batch file file download from a URL 2011-01-06

    I am trying to download a file from a website (ex. http://www.foo.com/package.zip) using a Windows batch file. I am getting an error code when I write the function below: xcopy /E /Y "http://www.foo.com/package.zip" The batch file doesn't seem to lik

  • Unexpected handling of "variables" in cmd.exe batch file 2011-02-04

    I have a xyz.bat file with this content: @set val=one @if "%val%" equ "one" ( @set val=yes echo val after setting it: %val% ) else ( @set val=no ) @echo %val% Running it in cmd.exe prints val after setting it: one yes but I expected val after setting

  • autosys doesn't run batch file 2011-07-19

    i wrote a batch file to open a webrowser, then close it, then open another and close it. upon opening the browser it will send email out. when i manually run the batch it works, but when i use autosys to run it, email never got sent. here's the batch

  • How to write a batch file showing path to executable and version of Python handling Python scripts on Windows? 2011-10-19

    It should display path to executable and version of Python for scripts run with direct invocation of Python (python myscript.py) as well as for scripts run directly (myscript.py). Script should not make too many assumptions on the configuration of th

  • How to create batch file in Windows using "start" with a path and command with spaces 2008-09-16

    I need to create a batch file which starts multiple console applications in a Windows .cmd file. This can be done using the start command. However, the command has a path in it. I also need to pass paramaters which have spaces as well. How to do this

  • How can I make a batch file to act like a simple grep using Perl? 2008-09-19

    I already know the obvious answer to this question: "just download <insert favorite windows grep or grep-like tool here>". However, I work in an environment with strict controls by the local IT staff as to what we're allowed to have on our comp

  • How do I protect quotes in a batch file? 2008-09-24

    I want to wrap a Perl one-liner in a batch file. For a (trivial) example, in a Unix shell, I could quote up a command like this: perl -e 'print localtime() . "\n"' But DOS chokes on that with this helpful error message: Can't find string terminator "

  • How can you echo a newline in batch files? 2008-09-25

    How can you you insert a newline from your batch file output. I want to do something like: > echo hello\nworld Which would output: hello world --------------Solutions------------- echo hello & echo.world This means you could define & echo.

  • How can I load the contents of a text file into a batch file variable? 2008-09-25

    I need to be able to load the entire contents of a text file and load it into a variable for further processing. How can I do that? Here's what I did thanks to Roman Odaisky's answer. SetLocal EnableDelayedExpansion set content= for /F "delims=" %%i

  • Why does batch file FOR fail when iterating over command output? 2008-10-08

    I have a batch file that uses this idiom (many times) to read a registry value into an environment variable: FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\SOFTWARE\Path\To\Key" /v ValueName') DO SET MyVariable=%%B (There's a tab character afte

  • Batch files: How to read a file? 2008-10-15

    How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode? --------------Solutions------------- You can use the for command: FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i

  • Is there a timeout for a batch file? 2008-11-03

    I am using a bat file on a Windows 2000 SP4 server to copy database files while the database is shut down. Once the bat file hits the xcopy command, it does the copy, but never returns to the bat file to continue with the other commands (start up the

  • Windows XP or Vista: How can I run a batch file in the background (no windows displayed)? 2008-11-18

    I know I have already answered a similar question (Running Batch File in background when windows boots up), but this time I need to launch a batch: From another batch Without any DOS windows displayed With all arguments passed to the invisible batch

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.141 (s). 11 q(s)