0

Context

  • I am thinking I can solve a problem with the proper creation of a *.bat file.

  • I am automating a process in a backup program called Acronis Backup and Recovery.

  • I am able to make a script (jScript) that creates all the syntax except for one part correctly.

  • In a normal command prompt the command I would run looks like this

acrocmd backup file --include="C:\documents\Gale_thesis.doc" "D:\Sandbox\!oDC!-IMG_0222.MOV" "C:\temp\magnifyReader" --loc="D:\backups" --arc="Backup1a"
  • The jScript I am creating can generate this with no problem and save as a *.bat file. This can works perfect if my file names are clean. By clean I mean no characters the batch files think are key words and commands.

  • Anytime I have a word like “copy” or a character like “!” in a file name it fails.

Question

So I am now wondering if loading variables from a text file would do the trick?

I am sure a lot of readers know that when load multiple file/folder paths at the command line you need to surround them with double quotes.

  • So I need this variable to have the correct syntax to be parsed by the batch file and work like the example when I type it directly at a command prompt.

  • I had tried to follow info about using for /f etc.

  • But the examples are not broad enough for me to understand, nobody seems to explain how to use these variables mixed in with other syntax.

  • I know a little about working with variable in a *.bat file. My jScript application can produce the text in any format a list, escaped, what ever is needed.

Thanks

dbenham
  • 127,446
  • 28
  • 251
  • 390
bistel
  • 27
  • 4
  • 1
    Before you embark on a wild goose chase (invent a complex "solution" using technology you don't understand), you ought to understand why your original code is failing. I can see how `!` could cause problems if delayed expansion is enabled, but I can't imagine why you would need delayed expansion. I can't envision how a file name containing `copy` would fail. Please edit your question and post an example generated bat script with "clean names" that succeeds. Then post an example bat script that fails with `!` in name, and one with `copy` in name. Also, show how you are launching the bat script. – dbenham Jan 03 '16 at 14:40
  • Is `!oDC!` supposed to be a string literal in the file name (that would be a very unusual name)? Or is it supposed to be delayed expansion of an environment variable? – dbenham Jan 03 '16 at 15:45

1 Answers1

0

I might suggest you to take a look at escaping characters http://www.robvanderwoude.com/escapechars.php

in for loops !var! is used when delayedexpansion is enabled so you might need to escape it

I used the following code provided by Aacini to test the arguments that are being passed

@echo off
setlocal enabledelayedexpansion

set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
   set "argVec[!argCount!]=%%~x"
)

echo Number of processed arguments: %argCount%

and since delayedexpansion is enabled I had to escape ! character

arg.bat --include="C:\documents\Gale_thesis.doc" "D:\Sandbox\^^^!oDC^^^!-IMG_0222.MOV" "C:\temp\magnifyReader" --loc="D:\backups" --arc="Backup1a"

Also about the triple escape quotes ^^^

the problem here is that we need to pass two special characters, 1st is the up arrow ^ and 2nd is the exclamation mark ! so the 2nd batch file (the one that reads our arguments) should get ^! to escape ^ we use ^^ and to escape ! we use ^!

Thanks to Aacini for his code in HERE

Community
  • 1
  • 1
xDeathwing
  • 139
  • 13
  • Hi thanks for the reply. Wow this is a different animal. I mean from things like PHP and other things I have seen. I can enter the exact syntax in a command prompt and get exactly what I need (pics). – bistel Jan 03 '16 at 15:21
  • I was thinking there must be some way to tell the *.bat that everything between the escaped quote is legal. I can see with your help that this is not the case. So it is a common practice to build a set of sanitizing commands to make sure all paths are legal? I could do this with what generates the *.bat that I am loading the variables from it just seems like a lot. In some languages they have functions or packages that sanitize for you, is there anything like that for this? Thanks – bistel Jan 03 '16 at 15:21
  • Here are two pics of what works in a cmd window [Pic1](http://pctechtv.com/forumpic/stack/160103/cmd.jpg) [Pic2](http://pctechtv.com/forumpic/stack/160103/conEmu.jpg) – bistel Jan 03 '16 at 15:22
  • Well, about auto sanitizing unfortunately there is not as far as I know [As Mentioned Here](https://community.office365.com/en-us/f/172/t/251788) But on the good side the list for characters to escape is not so long, so you can write a basic for loop to check characters and add them the escape that they need – xDeathwing Jan 03 '16 at 15:34
  • Great help thank you so much. Now I won't spend too much time looking for something that does not exit. As for what might exist... if you know of any examples of loops built to escape could you link me. Thanks again. – bistel Jan 03 '16 at 16:08
  • Well Im running the same problem as you run right now for a while.. Instead of hand fixing the code Im thinking to write my own function for it but its kinda complicated and [requires time :(](http://stackoverflow.com/questions/6828751/batch-character-escaping) – xDeathwing Jan 03 '16 at 16:13
  • Oh! i almost forgot, my code also broke when I had a file path like so `D:\Sandbox\htf3 - Copy.txt` I seems like the *.bat thinks the word "Copy" is its own. What are ways people deal with this? Thanks – bistel Jan 03 '16 at 16:29
  • Thanks for the reply. Could you explain better? The screenshots are what work. The *.bat cant seem to give me what I can so easily type in the command prompt manually. Thanks – bistel Jan 03 '16 at 17:00
  • While being not sure about why it interprets copy as a command inside double bracets, there might be possible 2 solutions one being escaping the double quotes (because it seems like batch is not getting the double quotes and trying to parse everything) or second one is making a variable to hold the legal-command text such as copy so using a temp variable instead of the direct word copy might do the trick for you – xDeathwing Jan 03 '16 at 17:03
  • Are you saying that if there were double quotes wrapped around these in the *.batch file that they should get parsed correctly? If so I have something else going on.. Going to test right now! – bistel Jan 03 '16 at 18:07
  • Wow! you solved it. I am laughing... I suspected this the hole time. I was trying to use \ to escape everything it is " to escape ". The minute I changed them it works with any file name perfectly. I was thinking there had to be something wrong. Thanks for all the help! I am laughing so hard! Thanks – bistel Jan 03 '16 at 18:12
  • No problems :) Im glad that you could get it sorted – xDeathwing Jan 03 '16 at 18:16