Batch File: Do command Options

jstevens
GoldLounger
Posts: 2630
Joined: 26 Jan 2010, 16:31
Location: Southern California

Batch File: Do command Options

Post by jstevens »

Is it possible to code more than one "do command" line in a "for" statement?

Code: Select all

For Example:   for /D {%% | %}variable in (set) do command [CommandLineOptions]

Line 1                      for /D {%% | %}variable in (set) do command 
Line 2                                First command [CommandLineOptions]
Line 3                                Second command [CommandLineOptions]

Please note that there are two "do" commands, line 2 and line 3. I know that if I place line 2 directly to the right of line 1 the code will work but do not want to include two seperate "for" statements.

Thanks for taking a look,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch File: Do command Options

Post by John Gray »

Does the following code answer your question?

Code: Select all

:: Generic date parser
:: Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit)
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (
    set %%a=%%d
    set %%b=%%e
    set %%c=%%f
    set toks=
  )
)
if %yy% LSS 100 set yy=20%yy%
echo Year is %yy%, Month is %mm%, Day is %dd%
The indentation for the second FOR command could have been written, much less intelligibly to a human, as:

Code: Select all

  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (set %%a=%%d&set %%b=%%e&set %%c=%%f&set toks=)
This sort of code structure can require pairs of double quotes or pairs of brackets to prevent a stray trailing blank appearing at the end of a variable.
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2630
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Batch File: Do command Options

Post by jstevens »

Hi John,

Thanks for the reply. I see how you handle seperate lines but cannot incorporate it into my code.

Code: Select all

:40000 Series
Set Source=I:\SourcePath\
Set Destination=I:\DestinationPath\
Set File=40*_bud.xls

FOR /f "tokens=*" %%G in ('dir /a /b %Source%%File%') Do echo F| xCopy /H/Y %Source%%%G "%Destination%Copy of %%G" attrib -h "%Destination%Copy of %%G


Pause
I'm copying files and then would like to change the file attributes all in one step. I know I can write two FOR statements and accomplish the attribute change with the second statement but would really like to do it with one pass.


Regards,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch File: Do command Options

Post by John Gray »

Looks as though you missed the & which combines two statements!

Try:
FOR /f "tokens=*" %%G in ('dir /a /b %Source%%File%') Do (echo F| xCopy /H/Y %Source%%%G "%Destination%Copy of %%G" & attrib -h "%Destination%Copy of %%G")

The left and right brackets may not be necessary, but it won't hurt to include them.
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2630
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Batch File: Do command Options

Post by jstevens »

John,

Your suggestion worked.

Thank you,
John
Regards,
John