Windows package to add a prefix to file names, why add twice?

To add a simple “hi” prefix to all the PDF files in the folder, I use this batch file:

FOR %%F IN (*.pdf) DO (RENAME "%%F" "hello%%F") 

I saved this in the file "rename.bat" and placed it in the folder in which I need the files that need to be renamed. Then I just double click on "rename.bat".

This almost works, but the first file gets the prefix added twice.

Let's say in the folder that I have: A.pdf, B.pdf, C.pdf, they are converted to:

  • hellohelloA.pdf
  • helloB.pdf
  • helloC.pdf,

Do you know what's wrong in a batch file?


I noticed that this always happens when files are more than one . It works fine when there is only one file in the folder, but this is not very useful :-).

+5
source share
2 answers

/f fixes the problem of re-capturing an existing file:

 FOR /f "delims=" %%F IN ('DIR /ad /b *.pdf') DO (RENAME "%%F" "hello%%F") 
+8
source
 @echo off echo. echo. Add Whatever Prefix... echo. echo. You Want To Add... echo. echo. To The Filename... echo. set /p variable=" > " setlocal enabledelayedexpansion for /f "delims=" %%a in (' dir /b /ad *.pdf') do ( set oldName=%%a Set newName=%variable%!oldName! Ren "!oldName!" "!newName!" ) exit 

This works well ..... Try it ... No double prefix ... Ever.

0
source

Source: https://habr.com/ru/post/1211155/


All Articles