Rename files and add a suffix with TimeStamp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Rename files and add a suffix with TimeStamp

#1 Post by ceccozambu » 16 Mar 2018 03:48

Hi,
I have this code to rename file without spaces and move from C:\Test\aa to C:\Test\aa\tmp

I need to add at the end the suffix with time stamp setted in _YYYYMMDD_HHMMSS.
For example:

C:\Test\aa\FileA.dat
C:\Test\aa\FileB.txt
C:\Test\aa\FileC.jpg

to
C:\Test\aa\tmp\FileA_20180316_104210.dat
C:\Test\aa\tmp\FileB_20180316_104233.txt
C:\Test\aa\tmp\FileC_20180316_104300.jpg


Can you help me?

Thanks,
Francesco

Code: Select all

setlocal enabledelayedexpansion

PUSHD "C:\Test\aa"
REM Get all files with an extension
for %%G in (*.*) do (

set "name=%%~G"
	REM remove spaces from name
	set "name=!name: =!"
	
	REM move file to temp with new name
	move "%%~G" "tmp\!name!" 
)


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Rename files and add a suffix with TimeStamp

#2 Post by Squashman » 16 Mar 2018 07:38

Getting the date and time and using it to rename files has been talked about dozens of times on this forum.

The most trouble free way to get the date and time into a variable is to use WMIC. Talked about here.
viewtopic.php?t=4555#p26102

To rename your files you will want to use the FOR commands modifiers to split apart the file name from the file extension, which is explained at the very end of the help file for the FOR command.

Code: Select all

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Rename files and add a suffix with TimeStamp

#3 Post by ceccozambu » 16 Mar 2018 08:29

Hi,
thanks, for reply.
I have read the post you have linked but I'm not an expert :(

It's ok put the date and time like a suffix or the data and time of creation of the file. (that maybe it's easier)

The only important thing that all the file have a specific name before moving into other folder.

If you have other suggest will be great. Thanks in advance.

Bye,
Francesco

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Rename files and add a suffix with TimeStamp

#4 Post by Squashman » 16 Mar 2018 09:50

ceccozambu wrote:
16 Mar 2018 08:29
Hi,
thanks, for reply.
I have read the post you have linked but I'm not an expert :(
It is literally the first example that Foxidrive (RIP) provided which means your requirements. You can literally copy and paste that code to the top of your batch file and use the variable that is created.
ceccozambu wrote:
16 Mar 2018 08:29
It's ok put the date and time like a suffix or the data and time of creation of the file. (that maybe it's easier)
It can be done. But it would be more code then just getting the current date and time.

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Rename files and add a suffix with TimeStamp

#5 Post by ceccozambu » 16 Mar 2018 10:17

Hi,
i found the solution!! :-)
Thanks!


Code: Select all

@ECHO OFF
setlocal enabledelayedexpansion
REM Change the working directory
PUSHD "C:\Test\aa"
REM Get all files with an extension


for %%G in (*.*) do (
	REM add prefix with time and date
	set "name=%%G%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%"
	
	REM remove spaces from name
	set "name=!name: =!"
	REM move file to temp with new name
	move "%%~G" "tmp\!name!"
)


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Rename files and add a suffix with TimeStamp

#6 Post by Squashman » 16 Mar 2018 10:47

You are putting the date and time after the file extension.

Use the FOR command modifiers to your advantage.

Code: Select all

set "name=%%~nG%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%%%~xG"
Just so you know your code is region and user dependent. It may not display the same for other peoples computers.

Also you are outputting the date and time in a different format then the example you provided in your question.

If you use the code Foxidrive posted you could have literally just copied and pasted it into the top of your code and then used the %dt% variable.

Post Reply