PDA

Click to See Complete Forum and Search --> : [batch file] renaming files need help


Benuz
Nov 13th, 2011, 07:20 PM
First of all, I was looking for the right forum to post this, but I do not know where I should go. What is the type of this language called for writing batch files?

Anyway, I am trying to make a batch file that can rename all files in the folder with a new name which consist of the last modified date, time and 6 digits of randomly generated number, with the format [YY_MM_DD_HH_MM_SS_######.jpg]. I tried to google myself but I do not know what to do with the results. Here is what I have at the moment:

The following batch command renames my files with a bunch of random numbers. Used most of the code from here (http://www.howtogeek.com/57661/stupid-geek-tricks-randomly-rename-every-file-in-a-directory/)
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
SET Use=%%~xA
SET NewName=!random!_!random!_!random!_!random!_!random!!Use!
RENAME "%%A" "!NewName!"
)
)

Then I found the following from here (http://www.robvanderwoude.com/battech_fileproperties.php), which can identify the last modified date for me. Should be relevant to what I need.
FOR %%? IN (file_to_be_queried) DO (
ECHO File Name Only : %%~n?
ECHO File Extension : %%~x?
ECHO Name in 8.3 notation : %%~sn?
ECHO File Attributes : %%~a?
ECHO Located on Drive : %%~d?
ECHO File Size : %%~z?
ECHO Last-Modified Date : %%~t?
ECHO Parent Folder : %%~dp?
ECHO Fully Qualified Path : %%~f?
ECHO FQP in 8.3 notation : %%~sf?
ECHO Location in the PATH : %%~dp$PATH:?
)

Lastly, I found this from here (http://www.computing.net/answers/programming/creating-var-containing-8-random-digits/20261.html). It has the option for me to define 8 digits, so it seems useful.
set number=
for /L %%j in (1 1 8) do set number=!random:~0,1!!number!


So how to combine them and create what I am looking for? Please help.





UPDATE:

I just found this, which could be extremely useful
Rename Files to Match Date/Time Stamp | PCMag.com (http://www.pcmag.com/article2/0,2817,1386947,00.asp)

In their example, they compiled the following
FOR %%V IN (%1) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO IF EXIST %%L%%J%%K_%%M%%N%%~xV (ECHO Cannot rename %%V) ELSE (Rename "%%V" %%L%%J%%K_%%M%%N%%~xV)
with some rearrangement (if i got it right)
FOR %%V IN (%1) DO (
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
IF EXIST %%L%%J%%K_%%M%%N%%~xV (
ECHO Cannot rename %%V
) ELSE (
Rename "%%V" %%L%%J%%K_%%M%%N%%~xV
)
)
)

The code in the example above did no work right out of the box, so I used some of it, combined with what I had earlier and made the following. This one do not generate random numbers.
@ECHO OFF

FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO (
RENAME "%%A" %%L%%J%%K_%%M%%N%%~xV
)
)
)
PAUSE
but it did not work, and in return,
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .