2

This SEEMS very simple to me, and I'm sure it will be to most of you as well, but I can't seem to understand why this is happening.

All I'm trying to do is use this one little line in the script to look for .png files in a certain folder (represented by the variable %folder2%) and PRE-pend them with "DONE-". It IS doing that, BUT, it is overwriting the first five characters of the filename with what I want it prepended with (DONE-). Here is the line of code:

for %%a in (PNG) do (rename "%folder2%*.%%a" "DONE-*.%%a")

I'm hoping some of you can shed some light on this.

  • Please include your script as it works for me when I try it. – Tommiie Mar 26 '19 at 12:03
2

It is impossible to do what you want using just the RENAME command - it simply does not have a mechanism to prepend a value to an existing name when you use wildcards. Any characters in your target name will replace characters in the source name, up to the first ..

I can't blame you for not understanding, because the Windows documentation is horrible for this command. I had never seen anything written anywhere that describes the rules. That is why I investigated and wrote How does the Windows RENAME command interpret wildcards?.

The batch solution for prepending text to an existing name is to use a FOR loop to rename each file individually. Something like the following should work for you.

for %%F in ("%folder2%*.PNG") do rename "%%F" "DONE-%%~F"

Another option is to use a 3rd party utility that is more powerful than the native RENAME command. If you are comfortable with regular expressions, then one option is my JREN.BAT rename utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward - no 3rd party exe file needed. Once you have a copy, help is available from the command line via jren /?, or jren /?? for paged help.

With JREN.BAT, the solution is as simple as jren "^" "DONE-" /p "%folder2%" /fm *.png. If you put the command in a batch script you need to use call jren so that it will return to your script.

  • Thank you for taking the time to look at my issue. I'll read the piece you link to in hopes of expanding my knowledge. Sincerely appreciate that! However, the line of code does not seem to work. I copied it and pasted it directly into my script and it seems to not do anything. The filenames remain unchanged. I noticed inside the parentheses there is only one quotation mark. Should there be a second? Should there also be a backslash? Just wondering... – Moto Dork Mar 14 '19 at 12:16
  • @MotoDork - It didn't work for a very good reason - I had a bone head typo in the answer :-( The IN clause had an opening quote without the needed closing quote. I've fixed that bug, and also added a ~ near the end that should have been there. Please try again - It really is the "standard" way to get around the REN limitation. – dbenham Mar 14 '19 at 13:47
  • That is also what I was thinking. Thank you for fixing the two issues. But I still am not able to get this to work. I directly copied and pasted this into my batch file, and when it runs there are no errors whatsoever, but no filename changes. It's like it runs but makes no changes. I even pasted it into an elevated command prompt (with single percents of course) and it still runs with no errors but no files change (I even changed %folder2% to the actual path with no luck). Not sure why this is not working. Any thoughts? – Moto Dork Mar 14 '19 at 14:11
  • @MotoDork - Remove any ECHO OFF, or put ECHO ON before the FOR loop, and then run. It should echo all the RENAME commands that it is trying. If there is no output, then either there are no files to rename, or your path is wrong. – dbenham Mar 14 '19 at 17:20
  • Yes that worked. Thank you so much for your help with this! – Moto Dork Mar 15 '19 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy