Change the name of the files(with extension *.txt)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
parthmittal2007
Posts: 1
Joined: 09 May 2013 04:16

Change the name of the files(with extension *.txt)

#1 Post by parthmittal2007 » 09 May 2013 05:25

Hi
Today is my first day in Batch Scripting, I got an issue which I want to share with you all
I have a text files in my directory. I Want to write a batch script which changes the name and add date stamp to it

For example -

1.txt - > should be renamed to -> 1_09_05_2013_.txt (1_mm_dd_yyyy_.txt)

Regards
Parth

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#2 Post by psychoid69 » 09 May 2013 06:42

This is how far I got... Maybe it'll be enough to help you out. I'll try and finish it...

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%F in ('dir /b /a-d') do echo %%~nF>> temp.tmp
<temp.tmp (
  for /f "tokens=1-3 delims=." %%A in ('forfiles /c "cmd /c echo @fdate"') do (
    set /p fname=
    echo !fname!_%%A_%%B_%%C_
  )
)
del temp.tmp

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#3 Post by psychoid69 » 09 May 2013 06:59

This works now, however it also "works" the files created by the script itself, so still some work to do... :)

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%F in ('dir /b /a-d') do echo %%~nF>> temp.tmp
<temp.tmp (
  for /f "tokens=1-3 delims=." %%A in ('forfiles /c "cmd /c echo @fdate"') do (
    set /p fname=
    echo !fname!_%%A_%%B_%%C_>>temp2.tmp
  )
)
<temp2.tmp (
  for /f "tokens=*" %%X in ('dir /b /a-d') do (
    set /p tname=
   echo %%X !tname!>>cmd.tmp
  )
)
<cmd.tmp (
  for /f "tokens=*" %%Z in ('dir /b /a-d') do (
  set /p cmd=
  ren !cmd!%%~xZ
  )
)
del temp.tmp
del temp2.tmp
del cmd.tmp

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#4 Post by psychoid69 » 09 May 2013 07:10

Haha, only now I see that all your files are *.txt, so fixed:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%F in ('dir *.txt /b /a-d') do echo %%~nF>> temp.tmp
<temp.tmp (
  for /f "tokens=1-3 delims=." %%A in ('forfiles /c "cmd /c echo @fdate"') do (
    set /p fname=
    echo !fname!_%%A_%%B_%%C_>>temp2.tmp
  )
)
<temp2.tmp (
  for /f "tokens=*" %%X in ('dir *.txt /b /a-d') do (
    set /p tname=
   ren %%X !tname!.txt
  )
)
del temp.tmp
del temp2.tmp

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

Re: Change the name of the files(with extension *.txt)

#5 Post by Squashman » 09 May 2013 07:37

The user did not specify what date stamp they wanted. Created date. Modified date. Today's date.

If the user wanted modified date there is no need to use the temp file and extra loops you have. You can just use the DATE Modifier %~tI.

What do you think will happen if a new text file is created in the directory in between your loops?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Change the name of the files(with extension *.txt)

#6 Post by Endoro » 09 May 2013 08:02

and why a temp file and why forfiles?

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#7 Post by psychoid69 » 09 May 2013 10:17

It's what I know and what works. You both can post your solutions, if I may say so... ;)
I never said it was optimal, but since no one has answered so far, I did my best.

As for %~t, I have tried with that, but I couldn't separate time from the date so to only use date; like I said, anyone with comments is welcome to post their solution.

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

Re: Change the name of the files(with extension *.txt)

#8 Post by Squashman » 09 May 2013 10:40

psychoid69 wrote:It's what I know and what works.

Until someone creates a text file while your batch file is running.


psychoid69 wrote:You both can post your solutions, if I may say so... ;)

I won't do anything until I have all the details of a problem. Coding for Created Date, Modified Date and Today's date are all completely different.

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#9 Post by psychoid69 » 09 May 2013 11:04

parthmittal2007 should really specify what he means by date. Like Squashman mentioned - are we talking about creation date of the file or last access date or last written date or maybe even just current date?

parthmittal2007 - I'm awaiting for your clarification. :)

In the meanwhile something more to play with:

Code: Select all

@echo off
for /f "tokens=1-3 delims= " %%X in ('dir *.txt /a-d ^| find ^":^" ^| find /V ^"^\^"') do echo %%X

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

Re: Change the name of the files(with extension *.txt)

#10 Post by Squashman » 09 May 2013 11:17

psychoid69 wrote:parthmittal2007 should really specify what he means by date. Like Squashman mentioned - are we talking about creation date of the file or last access date or last written date or maybe even just current date?

parthmittal2007 - I'm awaiting for your clarification. :)

In the meanwhile something more to play with:

Code: Select all

@echo off
for /f "tokens=1-3 delims= " %%X in ('dir *.txt /a-d ^| find ^":^" ^| find /V ^"^\^"') do echo %%X

Like your last code that still gets the modified date. You have to use /TC to get the created date.

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

Re: Change the name of the files(with extension *.txt)

#11 Post by psychoid69 » 09 May 2013 12:32

Yes, I know... I'm still learning and I have fun doing this, so I try all scenarios - for fun.

And because I was playing around again, I didn't wait for Parth to specify what date he wants, here's code which takes today's date and embeds it in the filename. :p

Code: Select all

@echo off
for /f "tokens=2 delims= " %%X in ('date /T') do set todayd=%%X
for /f "tokens=1-3 delims=." %%F in ("%todayd%") do set todayd=_%%F_%%G_%%H_.txt
for /f "tokens=*" %%K in ('dir *.txt /b /a-d') do ren %%K %%~nK%todayd%
Last edited by psychoid69 on 09 May 2013 13:56, edited 1 time in total.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Change the name of the files(with extension *.txt)

#12 Post by Endoro » 09 May 2013 12:53

I posted a /tc solution already

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Change the name of the files(with extension *.txt)

#13 Post by Aacini » 09 May 2013 15:18

This is my version:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set whichDate=date:/=_
if "%1" neq "" set whichDate=fileDate
for /F "tokens=1,4*" %%a in ('dir /A-D /T%1 *.txt') do (
   set fileDate=%%a
   set fileDate=!fileDate:/=_!
   if "!fileDate!" neq "%%a" (
      ECHO ren "%%c" "%%~Nc_!%whichDate%!_%%~Xc"
   )
)

Place in first parameter the first letter of the desired date: Creation, Access or Written. For example, for last Access date:

Code: Select all

rename.bat A

If no parameter is given, current date is used.

Antonio

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Change the name of the files(with extension *.txt)

#14 Post by Endoro » 09 May 2013 15:51

Nice :D - no find/findstr

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

Re: Change the name of the files(with extension *.txt)

#15 Post by foxidrive » 09 May 2013 19:12

Aacini wrote:This is my version:

Antonio


It looks like my 24 time confuses it in my locale settings.
My file is called "a good dog.txt" and this is printed.

ren "good dog.txt" "good dog_10_05_2013_.txt"

Post Reply