How to adjust command to run in batch script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to adjust command to run in batch script?

#1 Post by pstein » 23 Apr 2017 04:02

I have got from some tutorials two commands which run perfect if entered manually at the command prompt (on 64bit Win7):

FOR /F "Tokens=*" %A IN ('DIR /al /b /s D:\') DO @( for /F "Tokens=2,4 delims=<[]>" %B IN ('DIR /al "%~A"? ^| FIND /I " %~nA " ^| FIND /I "<" ^| FIND /I ">" ') DO @( ECHO.%~B: "%~A" ? "%~C" )

and

for /F "usebackq tokens=2* delims=:" %G in (`forfiles /s /c "cmd /c fsutil hardlink list @path | findstr /n .* | findstr /b /v 1"`) do @fsutil hardlink list "%G" & echo.

They are used to find all symbolic links & junction points & hard links on the current partition/directory tree.

Now I want to put them into a batch script. But then I got error about invalid syntax.

What do I have to adjust so that they are running in DOS batch script?

Thank you
Peter

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to adjust command to run in batch script?

#2 Post by aGerman » 23 Apr 2017 04:47

The main difference is the syntax of FOR variables. While they have only one percent sign prepended in the command line you have to double them in a batch code.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to adjust command to run in batch script?

#3 Post by Squashman » 09 Sep 2017 11:27

If you read the help for the FOR command it literally tells you this on the 7th line.

Code: Select all

C:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to adjust command to run in batch script?

#4 Post by Compo » 10 Sep 2017 05:14

Your one liner appears to have a parenthesis imbalance and could be improved upon:

Code: Select all

For /F "Delims=" %A In ('Dir/B/S/AL D:\') Do @For /F EOL^=^ Delims^=[^<^>]^ Tokens^=2^,4 %B In ('Dir/AL "%A"?') Do @Echo %B: "%A" ? "%C"
The batch script would look like this:

Code: Select all

@Echo Off
For /F "Delims=" %%A In ('Dir/B/S/AL D:\'
) Do For /F EOL^=^ Delims^=[^<^>]^ Tokens^=2^,4 %%B In ('Dir/AL "%%A"?'
) Do Echo=%%B: "%%A" ? "%%C"
Timeout -1

Post Reply