batch file changing double quote - batch-file

Having trouble running a batch file.
SET startIN="D:\Documents and Settings\%USERNAME%\Desktop\DataXfer Helper\StartFolder"
SET collection="D:\Documents and Settings\%USERNAME%\Desktop\DataXfer Helper\StartFolder\*.zip"
FOR /F %%G IN (‘dir /b %collection%\*.zip’) DO “C:\Program Files\Winzip\wzunzip” –E %startIN%\%%G %startIN%
When I type the line at the command prompt - it works fine (the only difference is when typed at the command prompt I use %G instead of %%G).
So, the environement variables are being created properly - but when I run the batch, the single quotes and the hyphen before the E are being turned into other characters (can't identify what they are).
So, any ideas why running the batch would change the characters?
Thank you.

There are a couple of problems:
The symbols in your FOR line are not supported ASCII chars. Retype the ', ", and - in the command line to correct them.
The string expansion is going to include the quotes you set in the path above. For example, %startIN%\%%G will expand to "D:\Documents and Settings\%USERNAME%\Desktop\DataXfer Helper\StartFolder"\MyFile.zip. When dealing with paths, make sure your logic is aware of quotes so they don't get trapped in the middle of the result.
Try updating your script to this:
REM Remove the quotes from the path.
REM Quotes will be added once we know the full path.
SET startIN=D:\Documents and Settings\%USERNAME%\Desktop\DataXfer Helper\StartFolder
REM Quote here because we know the full path.
SET collection="%startIN%\*.zip"
REM Update FOR loop to include parsing options and properly quote output.
REM Also update invalid ASCII chars.
FOR /F "usebackq tokens=* delims=" %%G IN (`dir /b %collection%`) DO "C:\Program Files\Winzip\wzunzip" -E "%startIN%\%%G" "%startIN%"

Related

Windows command SET seems possibly redundant?

I've inherited a batch file which reads major, minor and build numbers from a text file as part of a long-established build process - the actual reading part code is a simple FOR /F:
FOR /F "tokens=1,2,3* delims=." %%A IN (C:\MyBuildArea\version.txt) DO (
SET Major=%%A
SET Minor=%%B
SET Build=%%C
)
However this is followed by the line:
SET Build=%Build: =%
I don't understand this last line - what it is doing. Guessing it might be trying to catch and remove a trailing space (?) from the original file read as the FOR /F was delimited on dot (".") not space.
Hoping someone can help me understand - is this line redundant or serving some purpose?
The command SET Build=%Build: =% will simply substitute all spaces with nothing, i.e. it will remove all spaces in the text stored in Build.
See set /? on command prompt for details.

Saving %CD% into variable, but path has spaces. How do I use my variable if the path has spaces?

Example:
set mydir=%CD%
echo %CD%
C:\Stuff\More Stuff\Lots of Stuff
So how should the variable be used? Usually the entire path could be encased in quotes, but what about the variable?
for /f %G in (%mydir%\file.txt) DO (echo %G)
The key is proper quotation. The following syntax is the only secure way to set a variable -- supposing the value does not contain quotation marks on its own, and the command extensions are enabled (which is the default anyway, and there are almost no situations where command extensions disturb):
set "myDir=%CD%"
The quotation marks do not become part of the value that way.
To use the value in a for /F loop as you try to do in your question, use this (remember to double the % signs in a batch file):
for /F "usebackq" %G in ("%myDir%\file.txt") do (echo %~G)
The option usebackq is now required in order for the quoted path not to be treated as a literal string.
EDIT: This is the former answer before I noticed the /F option at the for command.I do not remove it because that might be helpful in case a standard for loop is used:
To use the value in a for loop as you try to do in your question, use this (remember to double the % signs in a batch file):
for %G in ("%myDir%\file.txt") do (echo %~G)
The ~ modifier removes the quotation marks from the value.
To ensure that the expanded value is always surrounded by quotation marks, use this:
for %G in ("%myDir%\file.txt") do (echo "%~G")
In general, paths should always be quoted in order to avoid trouble with white-spaces and also other special characters. For example, %myDir% holds a path like D:\Docs & Data, the command line echo D:\Docs & Data echoes the string D:\DocsSPACE and tries to execute a command named Data, which does not exist (most probably), because the & symbol has a special meaning, namely to concatenate two separate commands in one line. Quoting the path like echo "D:\Docs & Data" avoids this problem (although the quotes are are returned too by echo; but they are ignored by commands that accept path arguments, like cd or copy, for instance).
Use either this:
set mydir="%CD%"
echo %mydir%
or in the loop case
set "mydir=%CD%"
echo "%mydir%"
for /f "usebackq delims=" %%G in ("%mydir%\file.txt") DO echo %%G
although I would see no benefit in not using
echo "%__CD__%"
for /f "usebackq delims=" %%G in ("%__CD__%file.txt") DO echo %%G
Why set something you don't need?

Using batch echo to write a vbe file (characters are not escaped)

So I'm currently writing a tutorial about security and for that reason I have to write a vbe file (encoded script written in VBScript) using a batch file.
So, I just have to write this to a file:
##~^mgAAAA==6 P3MDKDP"+k;:PH+XY~###&fks~D;EdO{6k^+SPhnk/Co8WX~~AMkYnm6ks+B~T+O|wmYtBPDn:a{2lDtS~6kxms{alY4~###&s+k/Con8K6~',h/T4GavJKndDJ~~8BPEwlDlV,2M.WMJbP###&2zEAAA==^#~#
(Note: There are some characters that cannot be printed above).
But the problem is that I never managed to write it successfully, I tried escaping all the characters using instructions from http://www.robvanderwoude.com/escapechars.php and it didn't work.
I tried using DelayedExpansion like this:
SET "foo=##~^mgAAAA==6 P3MDKDP"+k;:PH+XY~###^&fks~D;EdO{6k^+SPhnk/Co8WX~~AMkYnm6ks+B~T+O|wmYtBPDn:a{2lDtS~6kxms{alY4~###&s+k/Con8K6~',h/T4GavJKndDJ~~8BPEwlDlV,2M.WMJbP###^&2zEAAA==^#~# "
setlocal EnableDelayedExpansion
(
echo !foo!
) > test.vbe
And it did not work either, I have problems with characters that are not escaped.
Any ideas?? Thanks!!
The reason is obvious, that is a quotation mark after [...P3MDKDP]. Since you assign the variable "foo" to jumble characters with a open and a close quotation mark, like so SET "foo=...", batch think you stop assigning "foo" after [...P3MDKDP]. This leaves [+k;:PH+XY~.....] alone, without assigning to a variable or working with commands. Batch can't recognize it, and so the command prompt quit automatically.
What you can do is, assign the part after the quotation mark to another variable, I named it "foo2" in the following example:
#echo off
setlocal enabledelayedexpansion
SET "foo=##~^mgAAAA==6 P3MDKDP""
SET "foo2=+k;:PH+XY~###^&fks~D;EdO{6k^+SPhnk/Co8WX~~AMkYnm6ks+B~T+O|wmYtBPDn:a{2lDtS~6kxms{alY4~###&s+k/Con8K6~',h/T4GavJKndDJ~~8BPEwlDlV,2M.WMJbP###^&2zEAAA==^#~# "
echo !foo!!foo2!>test.vbe
pause >nul
And also, if you add another quotation mark before / after the quotation mark, like so [P3MDKDP ""], even though you did not assign the second part to a new variable, it still work, but it output an extra quotation mark in the string.
maybe this little trick helps you:
#echo off
for /f "delims=[]" %%n in ('find /n "REM DATA:" "%~dpnx0"') do set /a n=%%n
more +%n% "%~dpnx0">test.vbe
REM rest or your batchfile
goto :eof
REM DATA:
##~^mgAAAA==6 P3MDKDP"+k;:PH+XY~###^&fks~D;EdO{6k^+SPhnk/Co8WX~~AMkYnm6ks+B~T+O|wmYtBPDn:a{2lDtS~6kxms{alY4~###&s+k/Con8K6~',h/T4GavJKndDJ~~8BPEwlDlV,2M.WMJbP###^&2zEAAA==^#~#
(this trick avoids any character escaping or splitting the string. Can also be used to write a multiline text)

bat file: Use of if, for and a variable all together

Here goes:
I want to add text to a file if it exists. The text is to go on each line of the file at the very end. The text to be added is "Note: certain conditions apply"
I found syntax to check if the file exists, and syntax for a for loop to add text at the end of the line, but they do not seem to work at the same time in the bat file.
Also, advice varies about variable names, using "%" and using the quote marks themselves,
so here is what I have (assume everything takes place in the same directory)....
#echo off
setLocal EnableDelayedExpansion
PAUSE
REM File to be modified -- 1
SET FileToModify1=abcd.txt
SET SaveFile1=abcd1.txt
PAUSE
IF EXIST "%FileToModify1%" (
echo Yes
)
IF EXIST "%FileToModify1%" (
for /f "tokens=* delims= " %%a in "%FileToModify1%" do (
echo %%a Note: certain conditions apply >> "%SaveFile1%"
)
)
PAUSE
Does anyone have a suggestion on what to do here?
Also, is it better to have quotes around abcd.txt or not?
Why all the mysterious "%" around variables?
Please also see loop through file saving to variable for a much more efficient solution to the same problem....
You need this (works):
for /f "tokens=* delims= " %%a in (%FileToModify1%) do (
Where you have (doesn't work, wrong syntax):
for /f "tokens=* delims= " %%a in "%FileToModify1%" do (
From the online help (via for /?)
For file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.
Also, is it better to have quotes around abcd.txt or not?
It's required if your file name has spaces, otherwise it's optional.Your file name doesn't have spaces so I used the simpler approach, no quotes.
Why all the mysterious "%" around variables?
Well it doesn't have to be "mysterious". Here's an example, from the command line:
> set fname=MyFile.txt
> echo %fname%
MyFile.txt
The %'s around the variable name are seen by the shell and trigger the variable expansion. To see the online help, type set /?, and you'll get more details than you can stand.

Batch File: For Loop: Opens file instead of Rading it. Why?

The code below works when the file name (i.e. links.txt) only is specified without quotes (might work with quotes, but I haven't tested it.).
When I substitute the filename for an entire file path, which also includes spaces, the batch file doesn't work the way it should. Instead, the text file is opened and when I close the text file, the cmd box then shows the message of Echo is off. It's like on the first instance of the loop it's opening the file. Why is this happening?
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
FOR /f %%i in ('"Z:\My Docs\links.txt"') DO (
SET var=!var!%%i
)
echo !var!
ENDLOCAL
pause
for /f "usebackq" %%i in ("Z:\My Docs\links.txt") do ...
If you use single quotes around your file name, for command interprets you want to execute it, but with only double quotes, it is considered as a literal string.
From the for help on usebackq (see for /?): ... allows the use of double quotes to quote file names in file-set

Resources