Page 1 of 2

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

Posted: 09 May 2013 05:25
by parthmittal2007
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

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

Posted: 09 May 2013 06:42
by psychoid69
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

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

Posted: 09 May 2013 06:59
by psychoid69
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

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

Posted: 09 May 2013 07:10
by psychoid69
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

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

Posted: 09 May 2013 07:37
by Squashman
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?

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

Posted: 09 May 2013 08:02
by Endoro
and why a temp file and why forfiles?

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

Posted: 09 May 2013 10:17
by psychoid69
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.

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

Posted: 09 May 2013 10:40
by Squashman
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.

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

Posted: 09 May 2013 11:04
by psychoid69
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

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

Posted: 09 May 2013 11:17
by Squashman
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.

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

Posted: 09 May 2013 12:32
by psychoid69
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%

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

Posted: 09 May 2013 12:53
by Endoro
I posted a /tc solution already

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

Posted: 09 May 2013 15:18
by Aacini
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

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

Posted: 09 May 2013 15:51
by Endoro
Nice :D - no find/findstr

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

Posted: 09 May 2013 19:12
by foxidrive
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"