Jump to content

"Quotes" Question about CMD Batch


Recommended Posts

Hi guys, sorry for the stupid question, but i've a doubt about the use of quotes, so i would need to know if in a batch file is more correct to use:

START /WAIT Updates\"%%~nA"\KB123456.exe /quiet or START /WAIT "Updates\%%~nA\KB123456.exe" /quiet

RD /S /Q Updates\"%%~nA" or RD /S /Q "Updates\%%~nA"

RD /S /Q %Temp%\Dir or RD /S /Q "%Temp%\Dir" or RD /S /Q "%Temp%"\Dir

Thank you in advance guys, you're always very helpful!

Edited by Caml Light
Link to comment
Share on other sites


You only need quotes when there is a space in a file name or path name so the command knows it is not an other argument or switch. And if you use them, they should englobe the total name and path. So in your 2 first examples, 2nd part is correct. In the 3rd line, 1st part is best, 2nd is ok, 3rd is not (not sure it could work).

But quotes are not needed in any as you don't have any spaces in names.

Link to comment
Share on other sites

Variable with space might be used without quote in some and never in other:

- when using a variable with space after echo, you don't need quote whatever the variable with space (of course if the variable doesn't contain other special characters).

- when using a variable with space after if (for testing), in most cases you'll need to have it quoted the only exception is the syntax "if defined".

Link to comment
Share on other sites

ok guys, but Yzöwl wrote this:

@ECHO OFF 
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /R "%~dp0" %%A IN (*-KB*.EXE) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL PING -n 4 127.0.0.1
"%%A" /quiet /norestart)
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
GOTO :EOF

:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"

as you can see, this variable has quotes:

 "%%A" /quiet /norestart

So, Tripredacus has written that it is safe to use environment variables without quotes. Could you explain me bacause in the batch i posted there are quotes? Sorry but i'm trying to understand better.

Link to comment
Share on other sites

ok, so summarizing, if i have an environment variable that contain spaces (ex. %COMMONPROGRAMFILES%\filename.exe -> C:\Program Files\Common Files\filename.exe), if i write in the batch:

START /WAIT %COMMONPROGRAMFILES%\filename.exe

i can use or less quotes, while writing:

START /WAIT "C:\Program Files\Common Files\filename.exe"

i MUST use quotes

Is it correct??

But simplifyng, for not commit errors can i always use quotes, also if not required because there aren't spaces (ONLY to start a program or delete file and folders with DEL/RD)?

RD /S /Q "%Temp%\Dir"
RD /S /Q "Updates\%%~nA"
START /WAIT "Updates\%%~nA\KB123456.exe" /quiet

PS: for a simple question i'm making a dust cloud (sorry) :}

Edited by Caml Light
Link to comment
Share on other sites

IMHO the issue is that you are failing to see some of the possibilities.

If you have a variable Temp which value %Temp% resolves to (say) "C:\windows\my stupid path with spaces in it" (i.e. the quotes are INSIDE the value of the variable, when you "mix" them and enclose in quote you do a mess, i.e.:

"%Temp%\my_folder" 

will resolve to

""C:\windows\my stupid path with spaces in it"\my_folder"

which is NOT what you want.

You want to have everything in quotes AND strip those quotes AND re-apply them "outside".

Example:

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET partial_path="C:\my partial path with spaces"
SET second_part="my dir_name also with spaces"
SET partial
SET second
CALL :strip partial_path
CALL :strip second_part
SET partial
SET second
CALL :join Full_path partial_path second_part
SET Full
GOTO :EOF

:strip
SET %1=!%1:"=!
GOTO :EOF

:join
SET %1="!%2!\!%3!"
GOTO :EOF

jaclaz

Link to comment
Share on other sites

This is my batch:

@ECHO OFF
COLOR F9
ECHO=_%~1|FINDSTR/X "_-? _/?">NUL 2>&1||GOTO Error
SETLOCAL ENABLEEXTENSIONS
SET "_="
PUSHD %~dp0

FOR /R %%A IN (Windows*-KB*.EXE) DO (
SET _=T
ECHO=Installation of %%~nA...>>install.log
>NUL PING -n 4 127.0.0.1
"%%A" /quiet /norestart
DEL /F /Q "%%A")

IF NOT DEFINED _ GOTO Error
:: ECHO=>>install.log
ECHO=Installation complete.>>install.log
>NUL PING -n 4 127.0.0.1
EXIT
GOTO :EOF

:Error
ECHO=Errore di inizializzazione.>>install.log
>NUL PAUSE

My doubts are about the quotes use for commands like: START /WAIT filename and DEL/RD file/folder. Instead of:

        "%%A" /quiet /norestart
DEL /F /Q "%%A")

i could have:

        START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
RD /S /Q Dir\"%%~nA")

where %%~nA is a subdirectory called with the name of the extracted file, or yet:

        START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
RD /S /Q "%Temp%\Dir")

So, i would need to adjust these double quotes for these typical cases to don't fall in error.

Edited by Caml Light
Link to comment
Share on other sites

My doubts are about the quotes use for commands like: START /WAIT filename and DEL/RD file/folder. Instead of:

        "%%A" /quiet /norestart
DEL /F /Q "%%A")

i could have:

        START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
RD /S /Q Dir\"%%~nA")

where %%~nA is a subdirectory called with the name of the extracted file, or yet:

        START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
RD /S /Q "%Temp%\Dir")

So, i would need to adjust these double quotes for these typical cases to don't fall in error.

But you have to make up your mind, first:

  • Is the batch to be run in the SAME directory where the "Windows*-KB*.EXE" (I presume Windows XP updates are saved?
  • WHAT exact parameter are you supplying the batch as %1? ( a full path, a relative path, what)?
  • Still IMHO running a bunch of updates with the /norestart parameter and in an order given by the result of the FOR /R loop may need LOTS of testing to make sure the thing actually runs.
  • You have THREE options (two of which are "wrong", though for different reasons):
  •         "%%A" /quiet /norestart
    DEL /F /Q "%%A")


  • this is not normally a good idea IMHO, using START /WAIT is always advised
  •         START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
    RD /S /Q Dir\"%%~nA")


  • this one is seemingly OK, though the "Dir" materializes itself from thin air :w00t: and you seemingly completely missed that the Quotes have to be BEFORE and AFTER
  •         START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart
    RD /S /Q "%Temp%\Dir")


  • this one is "wrong" for TWO reasons:
    1. you run a file in sub-directory of "Dir" (still materializing from thin air) that has the same name of the .exe file you should be running, BUT you have hardcoded the actual .exe file name :w00t:
    2. then you delete EACH TIME the WHOLE contents of "%Temp%\Dir" (which is NOT the same "Dir" you used before).

You quoted a snippet posted by Yzöwl here:

AND changed it. WHY? (I mean see earlier what is the directory structure and placement of the batch you expect/want?)

Have you tried using it EXACTLY as it was posted before anything else?

In what that EXACT, AS IS, snippet does not work for you or what missing feature you want to add to it? (apart from the logging, I mean) :unsure:

jaclaz

Link to comment
Share on other sites

The right syntax for "start" is at least to give a title and an executable.

START /WAIT Dir\"%%~nA"\KB123456.exe /quiet /norestart

should be

 START "hotfix" /WAIT "Dir\%%~nA\KB123456.exe" /quiet /norestart 

and i put "hotfix" you can put almost whatever you want. I also set the right quoting method in this case.

Lastly, i don't understand why you would get a folder named like Windows*-KB* (that's what you do when using "\%%~nA\ ") as in the batch you pasted, all hotfixes must be in the same folder....

Link to comment
Share on other sites

ok guys, Yzowl has modified his original batch, as you can see here:

http://www.msfn.org/board/topic/153204-cmd-option-to-jump/page__st__20

- updates are in the same directory (or in a sub-dir) of the file batch

- sometimes, before to install an update, i have to extract a zip file (containing the update) in a Temp directory with 7-zip command line, for ex.:

FOR /R %%A IN (filename*.ZIP) DO (
SET _=T
ECHO=Extraction of %%~nA...>>install.log
>NUL PING -n 4 127.0.0.1
Tools\7z x "%%A" -oDir\Temp\"%%~nA" -y
ECHO=Installation of %%~nA...>>install.log
>NUL PING -n 4 127.0.0.1
START /WAIT Dir\Temp\"%%~nA"\KB123456.exe /quiet /norestart
RD /S /Q Dir\Temp\"%%~nA")

This is my folder structure:

- batch file

- Tools ( dir that contains 7-Zip command line)

- Dir and Temp (dir and subdir that contain updates and extracted updates)

I hope you have explained better the situation. Thanks

Link to comment
Share on other sites

This is my folder structure:

- batch file

- Tools ( dir that contains 7-Zip command line)

- Dir and Temp (dir and subdir that contain updates and extracted updates)

I hope you have explained better the situation. Thanks

MUCH better than before :thumbup , but not yet enough. :ph34r:

Let's do it like this :), prepare on a drive your source files EXACTLY as you wish to use them, let's say they are in a directory under root like "C:\teststructure" (containing directly or inside subdirectories a sample of the files you wish to use), then run:

DIR /S C:\teststructure>C:\mystructure.txt

zip the resulting C:\mystructure.txt and attach the zipped file.

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...