FOR batch command syntax in Windows 2012


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!

Ben ConnerCTO, SAS developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cliff GaliherCommented:
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 developerAuthor Commented:
<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.

Cliff GaliherCommented:
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.
Ben ConnerCTO, SAS developerAuthor Commented:
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!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.