FOR batch command syntax in Windows 2012

Ben Conner
Ben Conner used Ask the Experts™

I'm trying to get the syntax right for using the FOR command in a batch file that will run in a Windows 2012 server environment.  What I have so far is:

compall.bat file:
rem This batch file compares text files in 2 directories using the Ultracompare batch progam (uc).
rem It uses old DOS commands to step through the folder structure.  Two arguments are needed:
rem The first is the first folder to look in, and the 2nd argument is the second folder to look in.
rem Example:
rem  compall 07092018 08072018
rem  Results go to a file called comp.txt in the current directory this batch file was executed in.

for %%f in ("dir %2 /b") do uc -t -i -b -B -ne -title1 "%%f old" -title2 "%%f new" -op comp.txt %1\%f %2\%f

compall 07092018 08072018
Results in:

c:\temp1>for %f in ("dir 08072018 /b") do uc -t -i -b -B -ne -title1 "%f old" -title2 "%f new" -op comp.txt 07092018\2\f
c:\temp1>uc -t -i -b -B -ne -title1 ""dir 08072018 /b" old" -title2 ""dir 08072018 /b" new" -op comp.txt 07092018\2\f

What I was hoping for:

>uc -t -i -b -B -ne -title1 "08072018 old" -title2 "08072018  new" -op comp.txt 07092018\file1.txt 08072018\file1.txt

Just can't get the syntax quite right on the FOR line.  ?

Thanks much!

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Before digging into that, may I suggest you reconsider even using a batch file? Powershell is the preferred automation language in windows. It will make your script easier to read, maintain, and even code.  There is very little reason to write a batch file in this day and age.
Ben ConnerCTO, SAS developer


<blush>  Yes, I know. :)  I haven't used Powershell at all and just didn't want to have to spend more time learning another tool to get the task at hand done.

Is there a similar way to approach that with Powershell instead?  I'm open to running it as long as I get the UC commands properly constructed.

Distinguished Expert 2018

Yes. Powershell loops are straightforward and are actual loops with blocks of code. You should be able to rewrite that bit with just a little Google help and an hour of time.
CTO, SAS developer
I just stumbled across the correct syntax:

for %%f in (%2\*.*) do uc -t -i -b -B -ne -title1 "%%f old" -title2 "%%f new" -op comp.txt %1\%%~nxf  %%f

That was certainly not intuitive.  Before I have to do this again I'll spend some time with Powershell.

Thanks, Cliff!


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial