FOR %%F IN (*.pdf) DO (
SET "x=%%F"
set /p "x=%%F Enter: "
IF /i "!x!.pdf" neq "%%F" ren "%%F" "!x!.pdf"
)
Your code worked fine for me. Since you don't indicate what the "to" and "from" names you used were, we're reduced to guessing.
I developed the above code to perform the rename in the manner you originated. Note that it works as I expect. Using ren
the "to" filename must be a filename within the directory where the "from" file resides [ie. it simply renames the file]. If you use "move" then the file can be moved to another directory if you specify that in the "to" name.
The first fix is purely cosmetic. By enclosing the variablename and prompt in the set /p
in quotes, you can include a terminal space in the prompt (which I prefer), and including the %%F
in the prompt shows you which file is about to be renamed.
The next fix is to quote the arguments to the ren
or move
. This ensures the syntax remains properly constructed in the case of eithr "to" or "from" name containing a space.
The next is to initialise x
with the "from" filename. It's enclosed in quotes so any invisible trailing spaces are not included in the value assigned to x
. Note that set /p
does not alter the variable if Enter alone is keyed, so setting x
ensures that if a file is not to be renamed, all you need do is press Enter
The next is to detect whether the "to" and "from" names are equal. ren
will generate an error report if you attempt to rename a file to itself; equally, you can't move
a file to itself. Hence, /i
=ignore case, and only attempt the operation if the names are different.
Finally, add the .pdf
to each usage of !x!
in order that you don't need to key it in. Naturally, you could omit this change if you want to alter extensions or you could put .pdf
into another variable and use that variable in place of the constant .pdf
so that the extension being selected can be easily varied by being changed in one plaace rather than using a mass-edit. You could even use a set /p
to assign the extension being processed dynamically at the start of the routine.
Note that if you rename say one.pdf
to yellow.pdf
then this construct is very likely to propose yellow.pdf
for a rename later on. This is because the next-filename logic locates the next filename currently in the directory,then processes the loop, then locates the next filename currently in the directory, and so on.
You would need
For /f "delims=" %%F in ('dir /b/a-d "*.pdf" ') do (
to ensure that each filename is only presented once. This mechanism performs a directory scan of filenames-only in basic form, and stores the list in memory, then processes the list item-by-item and since that list is created and then processed, any subsequent alterations to the directory do not affect the list.