0

I want to create a program that can loop through multiple pdf files and have the user rename each file a unique name like so:

  • 234324.pdf to Batch150.pdf
  • 32154687.pdf to AdvancedPayment.pdf
  • and so on...

Here is my code:

setlocal EnableDelayedExpansion 


echo rename pdf files

FOR %%F IN (*.pdf) DO (  
  set /p x=Enter:
  move %%F !x!
) 
endlocal

This seems to work for the first file and then when I try to rename the second one it says: The syntax of the command is incorrect..

I have tried using the rename command and haven't had much luck with it.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
L.Rengel
  • 3
  • 3
  • 1
    Shot in the dark: Your first file does not contain a space and the second one does? Are the examples above actual filenames that are renamed? Can you provide the filename, where it crashed and the name you tried to replace with. I guess that you are trying to rename the file (with a space) to another filename (with a space). As you are missing double quotes this would lead to a syntax error in the move command. – geisterfurz007 Dec 05 '16 at 06:45
  • Modifying the iterated items in the body of a `for` loop is dangerous, because `for` does not enumerate all items in advance -- reference the following post: [At which point does `for` or `for /R` enumerate the directory (tree)?](http://stackoverflow.com/q/31975093)... – aschipfl Dec 05 '16 at 12:37
  • Hey geisterfurz007 the orginal names of the files have no spaces the orginal file would be named something like (123215642016.pdf ) since they are being scanned into a folder from a printer and then I tried to change it to Batch 150.pdf since in this case with the move command if you don't specify the extension it will change the type of file. – L.Rengel Dec 07 '16 at 02:15

1 Answers1

0
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84