Results 1 to 1 of 1

Thread: [batch file] renaming files need help

  1. #1

    Thread Starter
    Junior Member Benuz's Avatar
    Join Date
    Jun 2007
    Posts
    23

    Exclamation [batch file] renaming files need help

    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
    Code:
    @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, which can identify the last modified date for me. Should be relevant to what I need.
    Code:
    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. It has the option for me to define 8 digits, so it seems useful.
    Code:
    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

    In their example, they compiled the following
    Code:
    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)
    Code:
    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.
    Code:
    @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,
    Code:
    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 . . .
    Last edited by Benuz; Nov 16th, 2011 at 09:02 PM. Reason: updating

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width