Frage

First off, I know there are many batch file rename questions here on Stackexchange, but no amount of searching here or on the web got me any satisfactory results. Also, I know this can probably be done by other means, but I've always been able to get batch files to work the way I want (simple tasks, nothing overly complex). Even if I do find an easier solution (using a GUI aided tool for example), I would still like a solution to this problem for knowledge.

I am trying to rename a bunch of images which I (regularly) import from my phone. The default name template for images is IMG_YYYYMMDD_HHMMSS.jpg; eg: IMG_20121224_165222.jpg

So, let's say I have a folder full of these files (along with others). I want to select file starting with IMG and rename the file to the following format: YYYY-MM-DD HH.MM.SS.jpg

This seemed rather trivial to me to start with using substrings, but I couldn't get it to work. I used delayed expansion for the variables; I have added the code I'm using to the end of this post.

Now for the example file above (IMG_20121224_165222.jpg), instead of getting "2012-12-24 16.52.22.jpg" I got "2012-12-24 16.NAME:15,2NAME:~17,2.jpg" (when I ECHOed the variable I'm using, refer code below). As you notice, the weird behavior starts after the first period (in the time field), so I tried replacing it with a hyphen like in the date, but still a no-go; I get "2012-12-24 16-NAME:15,2NAME:~17,2.jpg" now.

I am completely at a loss why this is happening. The if condition is being satisfied fine, just the substrings don't expand after a certain point.

Any and all help will be highly appreciated. Thanks!

Here is the latest code I used for this task (to output the name list to a txt file to check).

@ECHO OFF>NUL

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%A IN (*.*) DO (
    SET NAME=%%A
    IF /I "!NAME:~0,3!"=="IMG" (
        ECHO !NAME:~4,4!-!NAME:~8,2!-!NAME:~10,2! !NAME:~13,2!-!NAME:15,2!-!NAME:~17,2!.jpg >> output.txt
    )
)

PAUSE

For those interested, this is the output file contents:

2012-12-24 16-NAME:15,2NAME:~17,2.jpg  
2012-12-25 17-NAME:15,2NAME:~17,2.jpg  
2012-12-25 17-NAME:15,2NAME:~17,2.jpg  
2012-12-25 19-NAME:15,2NAME:~17,2.jpg  
2012-12-25 21-NAME:15,2NAME:~17,2.jpg  
2012-12-25 21-NAME:15,2NAME:~17,2.jpg  
2012-12-25 21-NAME:15,2NAME:~17,2.jpg  
2012-12-27 18-NAME:15,2NAME:~17,2.jpg  
2012-12-27 22-NAME:15,2NAME:~17,2.jpg   
2012-12-27 22-NAME:15,2NAME:~17,2.jpg  
2012-12-28 00-NAME:15,2NAME:~17,2.jpg  
2012-12-28 00-NAME:15,2NAME:~17,2.jpg  
2012-12-28 02-NAME:15,2NAME:~17,2.jpg  
2012-12-28 03-NAME:15,2NAME:~17,2.jpg  
2012-12-28 03-NAME:15,2NAME:~17,2.jpg  
2012-12-30 08-NAME:15,2NAME:~17,2.jpg  
2012-12-30 08-NAME:15,2NAME:~17,2.jpg  
2012-12-30 08-NAME:15,2NAME:~17,2.jpg  
2012-12-30 08-NAME:15,2NAME:~17,2.jpg  
2012-12-30 08-NAME:15,2NAME:~17,2.jpg  
2012-12-31 03-NAME:15,2NAME:~17,2.jpg  
2012-12-31 03-NAME:15,2NAME:~17,2.jpg  
2012-12-31 03-NAME:15,2NAME:~17,2.jpg  
2012-12-31 03-NAME:15,2NAME:~17,2.jpg  
2012-12-31 21-NAME:15,2NAME:~17,2.jpg  
2012-12-31 21-NAME:15,2NAME:~17,2.jpg  
2012-12-31 21-NAME:15,2NAME:~17,2.jpg  
2012-12-31 21-NAME:15,2NAME:~17,2.jpg  
2012-12-31 21-NAME:15,2NAME:~17,2.jpg  
2013-01-01 14-NAME:15,2NAME:~17,2.jpg  
2013-06-10 09-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 10-NAME:15,2NAME:~17,2.jpg  
2013-06-10 11-NAME:15,2NAME:~17,2.jpg  
2013-06-10 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
2013-06-12 17-NAME:15,2NAME:~17,2.jpg  
War es hilfreich?

Lösung

ECHO !NAME:~4,4!-!NAME:~8,2!-!NAME:~10,2! !NAME:~13,2!-!NAME:15,2!-!NAME:~17,2!.jpg >> output.txt
                                                             ! ~ missing
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top