Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

"Quotes" Question about CMD Batch

- - - - -

  • Please log in to reply
23 replies to this topic

#1
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
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, 08 November 2011 - 12:04 AM.



How to remove advertisement from MSFN

#2
Ponch

Ponch

    MSFN Junkie

  • Patrons
  • 3,308 posts
  • Joined 23-November 05
  • OS:none specified
  • Country: Country Flag
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.

#3
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
Thank you, now i've more clear ideas! :yes: Only in the last case, if
%Temp%
variable is:
C:\Users\Name Surname\AppData\Local\Temp
... as you see there is a space, so is it correct to write
"%Temp%"
?

Thanks!!!!

#4
Tripredacus

Tripredacus

    K-Mart-ian Legend

  • Super Moderator
  • 9,972 posts
  • Joined 28-April 06
  • OS:Server 2012
  • Country: Country Flag

Donator

No I think it is safe to use environment variables without quotes.
MSFN RULES | GimageX HTA for PE 3-5 | lol probloms
tpxmsfn1_zps393339c1.jpg

#5
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
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".

#6
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
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.

#7
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
Yzöwl was right: in this case if the hotfixes contains space in their name, the quotes are required (and won't cause any trouble). And that's why he put them.

#8
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
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, 08 November 2011 - 09:28 PM.


#9
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
Yes and yes if you do it properly.

#10
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,676 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
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

#11
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
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, 10 November 2011 - 09:34 AM.


#12
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,676 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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:

  • 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:
  • 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:
http://www.msfn.org/...s-installation/

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

#13
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
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....

#14
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
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

#15
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,676 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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

#16
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
Directory di C:\teststructure

10/11/2011  20:23    <DIR>          .
10/11/2011  20:23    <DIR>          ..
08/11/2011  20:51            45.569 batch.cmd
10/11/2011  20:20    <DIR>          Dir
10/11/2011  20:20    <DIR>          Tools
               1 File         45.569 byte

 Directory di C:\teststructure\Dir

10/11/2011  20:20    <DIR>          .
10/11/2011  20:20    <DIR>          ..
10/11/2011  20:20    <DIR>          Temp
               0 File              0 byte

 Directory di C:\teststructure\Dir\Temp

10/11/2011  20:20    <DIR>          .
10/11/2011  20:20    <DIR>          ..
               0 File              0 byte

 Directory di C:\teststructure\Tools

10/11/2011  20:20    <DIR>          .
10/11/2011  20:20    <DIR>          ..
18/04/2011  19:54           941.568 7z.dll
18/04/2011  19:34           168.448 7z.exe
               2 File      1.110.016 byte

In the "Dir" directory i put the updates, now is empty, while in the "Temp" subdir i'll extract the zipped updates found in "Dir".

Edited by Caml Light, 10 November 2011 - 01:28 PM.


#17
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,557 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Microsoft's updates don't come in zip files!

#18
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
sometimes i decide to zip them to save space when i put them in my fullest USB pen drive :)

So, this is a condition to consider for the batch.

Edited by Caml Light, 10 November 2011 - 01:41 PM.


#19
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,676 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

sometimes i decide to zip them to save space when i put them in my fullest USB pen drive :)

So, this is a condition to consider for the batch.

Well, for the record not really, no.
It is a condition you imposed.
The issue I see is that if you have EITHER:
  • ONLY non-zipped files
OR:
  • ONLY zipped files
THEN the file name sort sequential processing MAY make sense, BUT if you have a "mixed set of files" the order in which they are processed may well become a BIG problem. :ph34r:

In such a setup I would rather use a verified sorted list instead.

jaclaz

Edited by jaclaz, 10 November 2011 - 02:45 PM.


#20
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06
Just to satisfy my curiosity, how much space do you gain by compressing already compressed updates (please post the total space used by updates before and after) ?

#21
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03

Just to satisfy my curiosity, how much space do you gain by compressing already compressed updates (please post the total space used by updates before and after) ?


right consideration :), only using x86 post-SP1 updates for Windows 7 compressed with 7-Zip (.7z ext) 272MB -> 259MB (13MB earned), only for ONE O.S.! not bad (at least for me, clear :lol:)

Coming back in topic, have you others suggestions for me after having seen my batch structure? Thanks

#22
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,676 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
And, AGAIN, can you post an example of a POPULATED by the actual fles directory structure? :realmad:
Can you READ my previous post? :rolleyes:
Do you understand that until now what you propose is to process first all 7-zip compressed files (or ZIPped, you posted CONTRASTING info on this) and later all "normal" SFX .exe files (or the other way round first all the .exe's and then all the .7z's) and that this may not be a smart move as it may alter the proper order in which the updates should be installed? :unsure:


jaclaz

#23
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,421 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Here is what I understand, could be wrong
1:\ He has uncompressed updates in one folder
2:\ He has compressed updates in another
3:\ He want to extract compressed ones then install all

Here is a VBS script only tested to run without errors without
testing these Install Updates or Extract Updates
Item In The Script You Need To Change Or Check

Zip Path For 7z. This assume the script location is the Parent Directory With a
folder called Tools EG USB_DriveLetter:\Tools\

Dim Zip :Zip = Act.CurrentDirectory & "\Tools\"
Extract
Change From
'-> UnComment To Make Active Run 7z Extract
'      Act.Run(Zip & "7z x " & c34 & Obj.Path & c34 & " -o" & Ext & " -y")
Change To
'-> UnComment To Make Active Run 7z Extract
       Act.Run(Zip & "7z x " & c34 & Obj.Path & c34 & " -o" & Ext & " -y")

Install Updates
Change From
'-> UnComment To Make Active Run Install
'    Act.Run(c34 & Obj & c34 & " /quiet /norestart"),1,True 
Change To
'-> UnComment To Make Active Run Install
    Act.Run(c34 & Obj & c34 & " /quiet /norestart"),1,True 


Save As ExtInstallKb.vbs this script will run and Produce a text file without changes
''-> Object For Run Time
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles Used At Run Time
Dim c34 :c34 = Chr(34) 
Dim Zip :Zip = Act.CurrentDirectory & "\Tools\"
Dim Ext :Ext = Act.ExpandEnvironmentStrings("%tmp%\KbInstall")
Dim Rpt :Rpt = Act.ExpandEnvironmentStrings("%tmp%\KbInstall\KbRpt.txt")
'-> Varibles For Loop And Text Stream
Dim Col, Chk, Obj, Ts 
'-> Create Temp Folder
 If Not Fso.FolderExists(Ext) Then Fso.CreateFolder(Ext)
'-> Open Text Report
 Set Ts = Fso.CreateTextFile(Rpt)
  Ts.WriteLine " Script Date : " & Year(Now) & "-" & _
  MonthName(Month(Now),False) & "-" &  _
  WeekdayName(Weekday(Now),False) & "," & Day(Date) 
  Ts.WriteLine " Script Time : " & Hour(Time) & ":" & _
  Minute(Time) & ":" & Second(Time) & vbCrLf 
'-> Recursive(Fso.GetFolder("."))
' Uses Script Location As Parent Folder To Start Recursive Search 
' Of All Folders And There Sub Folders
  Recursive(Fso.GetFolder("."))
  If Chk = True Then 
  Ts.WriteLine vbCrLf & " Installing Updates In : " & Fso.GetFolder(Ext).Path
'-> Loop Threw Updates In Temp Folder
  For Each Obj In Fso.GetFolder(Ext).Files
   If Right(InStr(1,Obj.Name,"msu",1),3) Or _
      Right(InStr(1,Obj.Name,"ms1",1),3) Or _
      Right(InStr(1,Obj.Name,"exe",1),3) Then
    Ts.WriteLine " Install Update  : " & Obj.Path
'-> Remove Only For Demo Show KB Unstall Command
    Ts.WriteLine "Act.Run(" & c34 & Obj & c34 & " /quiet /norestart" & "),1,True"
'-> UnComment To Make Active
'    Act.Run(c34 & Obj & c34 & " /quiet /norestart"),1,True 
   End If 
  Next 
  Else
   Ts.WriteLine vbCrLf & " Nothing was extracted or installed."
  End If 
'-> Close The Report
  Ts.Close()
'-> Open The Report To Read
  Act.Run(c34 & Rpt & c34),1,True
'-> Ask To Keep Or Delete Report
  If MsgBox("Would You Like To Keep This Report",4132, _
   "Keep Or Delete") = 6 Then
   Fso.CopyFile Rpt, Act.SpecialFolders("Desktop") & "\KbRpt.txt",True 
  End If 
'-> Delete The Temp Folder
  If Fso.FolderExists(Ext) Then Fso.DeleteFolder(Ext)
'-> List All Folders And Files
   Function Recursive(Folder)
    Ts.WriteLine vbCrLf & " Searching Folder: " & Folder
    For Each Obj In Folder.Files
'-> Filter Out Script From Being Process
     If Not InStr(1,Obj.Path,WScript.ScriptFullName,1) Then
'-> Filter Out Updates By Name
      If InStr(1,Obj.Name,"kb7",1) Or InStr(1,Obj.Name,"kb8",1) Or _
      InStr(1,Obj.Name,"kb9",1)  Then
       If Right(InStr(1,Obj.Name,"msu",1),3) Then
       Chk = True 
       Ts.WriteLine " Confirm Update  : " & Obj.Name
       Fso.CopyFile Obj, Ext & "\", True 
       ElseIf Right(InStr(1,Obj.Name,"zip",1),3) Then
       Ts.WriteLine " Confirm Zip KB  : " & Obj
       Chk = True 
'-> Remove Only For Demo Show 7z File For Extract To Where It Extracted To
       Ts.WriteLine "Act.Run(" & Zip & "7z x " & c34 & Obj.Path & c34 & " -o" & c34 & Ext & c34 & " -y),1,true"
'-> UnComment To Make Active
'       Act.Run(Zip & "7z x " & c34 & Obj.Path & c34 & " -o" & Ext & " -y")
       End If 
      End If
     End If 
    Next
    'Ts.WriteLine
    For Each Col In Folder.SubFolders
      Recursive(Col)
    Next 
   End Function

Rename ExtInstallKb.vbs.txt from to ExtInstallKb.vbs to make active script.
This will run and Produce a text file without changes or errors
Attached File  ExtInstallKb.vbs.txt   3.18KB   3 downloads


GunSmokingMan



#24
Caml Light

Caml Light

    Junior

  • Member
  • Pip
  • 85 posts
  • Joined 03-October 03
Thank you! :yes:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users