Need some code to add a suffix to the end of file names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Need some code to add a suffix to the end of file names

#1 Post by OM3 » 01 Jul 2015 19:55

I need some code to add a files own date and stamp to its end and then move it to another directory.

I only want the code to work on CSV or txt files say.

I'm not sure how to get the files own date. BUT... I definitely need the time as well.
I need the time because when moving to the other location, if not renamed, there is a chance there will be an existing file with the same name.

I already have some code to add the system current date to a file - but am not sure how to adapt and instead get the files own date and time.

The code I have is:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "stamp=%DD%%MM%%YYYY%.%HH%%Min%"

:loop
ren "%~1" "%~n1.%stamp%%~x1"
shift
if not "%~1"=="" goto :loop


EDIT: I also need to check that a file hasn't already been renamed and has the correct format date and time added to the end. I'm lost with this one - how will I do string checking using DOS commands?

Any help to start me off would be great

Thanks


OM
Last edited by OM3 on 01 Jul 2015 20:11, edited 1 time in total.

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

Re: Need some code to add a suffix to the end of file names

#2 Post by Squashman » 01 Jul 2015 20:09

Read the help for the FOR command.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Need some code to add a suffix to the end of file names

#3 Post by Yury » 02 Jul 2015 12:56

OM3 wrote:...how to... get the files own date and time.


OM3 wrote:I also need to check that a file hasn't already been renamed and has the correct format date and time added to the end.




Code: Select all

@echo off

for %%I in (%*) do (
 set n=%%~nI
 for /f "tokens=1-5 delims=./-: " %%J in ("%%~tI") do (
  set h=0%%M
  (
  for /f %%O in ('cmd/v/c echo !n!^| findstr/erv \.[0-3][0-9][0-1][0-9][0-9][0-9][0-9][0-9]\.[0-2][0-9][0-5][0-9]') do (
   cmd/v/c ren "%%~I" "%%~nI.%%J%%K%%L.!h:~-2!%%N%%~xI"
  )
  )|| (
  for /f %%O in ('cmd/v/c echo !n!^^^| findstr/elv .%%J%%K%%L.!h:~-2!%%N') do (
   for %%P in ("%%~nO") do (
    cmd/v/c ren "%%~I" "%%~nP.%%J%%K%%L.!h:~-2!%%N%%~xI"
   )
  )
  )
 )
)

exit/b

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need some code to add a suffix to the end of file names

#4 Post by foxidrive » 03 Jul 2015 06:06

It works here too Yury.

IMG_0001.29062015.2156.jpg

I think the format of the date and time will vary depending on regional settings etc.

Post Reply