What's the difference between the use of %s and %%s in BAT or CMD or commandline scripts (and as seen on tech solutions on the Net)?

Hello, Everyone, I found two very similar one-liners as part of the solution to WMI issues in SCCM client installation:

1.) from https://www.virtuallyboring.com/microsoft-wmi-invalid-class-error-0x80041010/
dir /b *.mof *.mfl | findstr /v /i uninstall > moflist.txt & for /F %s in (moflist.txt) do mofcomp %s

2.) from https://blogs.technet.microsoft.com/yongrhee/2016/06/23/wmi-stop-hurting-yourself-by-using-for-f-s-in-dir-s-b-mof-mfl-do-mofcomp-s/
dir /b *.mof *.mfl | findstr /v /i uninstall > moflist.txt & for /F %%s in (moflist.txt) do mofcomp %%s

What's the difference between the two? I used the line from link #2 (%%s), but I got the error "%%s was unexpected at this time."   I understand from other posts that using "%%s" in batch files will not yield errors.  When I used the line from link #1 (%s), the command ran.

If both lines get executed successfully, whether via CMD or BAT files, would they have the same end result?

Thank you very much.
Jee SoonAsked:
Who is Participating?
 
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
It's simple.  a single % is used when using the for command alone on the command line.  When using the for command in a batch file, you must use %% instead.  Try it:

from the command line run (replace 192.168.1. with your local subnet):
for /l %a in (1,1,100) do @ping -n 1 -w 100 192.168.1.%a | find /i "reply"

Open in new window

Now try the same as batch file.
It shouldn't work.
Now fix the batch file and double the % and run it.
It should work fine.

And this only applies to the for command as far as I've ever seen.  you DO NOT double % for referencing command line variables -
%%computername%% is wrong no matter how you want to use it.  It's always %computername%.
Likewise, %1 in a batch file refers to the first parameter after the command.  For example, run the batch file named "runme.cmd" with echo %1 as the contents will result in "Hello" being displayed (spaces as delimiters).  (assuming you run "runcmd Hello World" without quotes.
0
 
Jee SoonAuthor Commented:
Thank you so much, Lee!!!
Exactly the answer that I needed.

Thanks a lot too for the easy-to-understand, ready-to-run examples.

Looking forward to future interactions! :)
0
 
Jee SoonAuthor Commented:
Thank you, Lee!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.