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
How to adjust command to run in batch script?
Moderator: DosItHelp
Re: How to adjust command to run in batch script?
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
Steffen
Re: How to adjust command to run in batch script?
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.
Re: How to adjust command to run in batch script?
Your one liner appears to have a parenthesis imbalance and could be improved upon:The batch script would look like this:
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"
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