Reputation: 11
I am trying to use a batch file to add the folder name to a file. I want to use the folder name from 1 folder up. I have, from here and other sources :
for %%* in (..) do set CurrDirName=%%~n*
echo %CurrDirName%
set strPrefix=%CurrDirName%
for %%a in (*) do rename "%%a" "%CurrDirName%_%%a"
The correct folder name is being added but it is caught in an endless loop of adding it until the file names are too long for the os. I am very new to this, having started today, so any help would be much appreciated and if you reply like talking to an idiot you will not be far off! Many thanks
Upvotes: 1
Views: 1925
Reputation: 11367
This behavior is a known bug/feature of the for
command. The file list that the for loop generates is dynamic, meaning that files modified in the loop will be re-added to the list causing an infinite loop.
Use the dir
command to generate a static file list.
for /f "delims=" %%A in ('dir /b *') do rename "%%A" "%CurrDirName%_%%A"
See dir /?
for all the options.
Welcome to the world of Batch scripting. :)
Upvotes: 2