Solved

for loop not working in .bat file

Posted on 2011-03-06
2
645 Views
Last Modified: 2012-08-14
I am calling this for loop in my .bat file  but it doesn't work.  When i type it at the command prompt, it works. I am new to scripting.

for %F in (*.*) do ren "%F" "%~nF"
0
Comment
Question by:SyFy
2 Comments
 
LVL 3

Accepted Solution

by:
OrenRozen earned 400 total points
ID: 35047045
hi,

in a batch file the command should be:
for %%F in (*.*) do ren "%%F" "%%~nF"
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 35047222
Correct. The reason for that is the special and different meaning of % as both FOR and environment variable deliimiter. The cumbersome syntax that the FOR var is only started by %, but not ended, introduces several issues. As a rule of thumb: always use two percents for FOR vars in a batch.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question