Rename mmdd.txt to ddmmyyyy.txt

January 29, 2013 at 22:32:30
Specs: Windows 7
I WANT TO RENAME TXT FILE ON DAILY BASIS FROM BACKUP FOLDER WHICH IS IN C:\BACKUP

WHICH IS C:\BACKUP\0825TRD.TXT TO C:\BACKUP\ABCD_25082012TRD.TXT

WHERE OLD FILE NAME IS MMDDTRD.TXT & NEW REQUIRED IS ABCD_DDMMYYYYTRD.TXT

PLEASE HELP TO SOLVE THIS PROBLEM WITH BATCH FILE


See More: Rename mmdd.txt to ddmmyyyy.txt

Report •

#1
January 30, 2013 at 19:04:30
That's quite a strange configuration of dmy! (mmdd to ddmmyyyy) but here goes:
::======= begin batch
@echo off & setlocal
pushd c:\backup
for %%a in (????TRD.TXT) do set x=%%a
set new=ABCD_%x:~2,2%%x:~0,2%%date:~10,4%
echo rename %x% %new%.txt
rename %x% %new%.txt
popd
::===== end batch

Report •

#2
January 31, 2013 at 20:14:52
Sir, Thanks for your reply.

but its not working , giving Error.

for

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\chetan>cd\

C:\>::======= begin batch
C:\>@echo off & setlocal
pushd c:\backup
for %%a in (????TRD.TXT) do set x=%%a
%%a was unexpected at this time.
set new=ABCD_%x:~2,2%%x:~0,2%%date:~10,4%
echo rename %x% %new%.txt
rename %x% ABCD_%x:~2,2%%x:~0,2%.txt
rename %x% %new%.txt
The syntax of the command is incorrect.
popd
::===== end batch


Report •

#3
January 31, 2013 at 20:50:56
If you're running this straight from cmd, (which is looks like you are), then you need to replace all double percents with single percents: %%a --> %a
but I would strongly advise running from the script (saved as a file with extension .bat):
copy (cut/paste) the original script into notepad, (make no changes) then save as \windows\system32\xxx.bat
To execute the script: xxx.bat

I'm not good at instructions, I hope this gets you there.



Report •

Related Solutions

#4
February 3, 2013 at 21:00:01
thanks for your reply..

It works as per your sggestion on cmd, but only now problem is file renamed to ABCD_2508.TXT instead of ABCD_25082013.TXT

This year (2013) to be taken from current date

I ll extremly thankful if i get this solution.


Report •

#5
February 3, 2013 at 21:20:26
this attempts to extract the year from your current system date (%date:~10,4%):
set new=ABCD_%x:~2,2%%x:~0,2%%date:~10,4%

If you will post the output of:
echo %date%
I can maybe fix it. I need to know what your date-format looks like, apparently I'm off the mark (i used: dow mm/dd/yyyy like "Thu 12/24/2012" where year starts at position 10)


Report •

#6
February 7, 2013 at 19:40:45
for my pc output of echo %date% is dd-mm-yyyy i.e.08-02-2013

Report •

#7
February 7, 2013 at 19:55:56
Ah, I should have used the -4 version (3rd line down from "echo off"):
set new=ABCD_%x:~2,2%%x:~0,2%%date:~-4%
Sorry. I thought of this shortly after posting, and forgot to fix it. hopefully this wraps it.

Report •

#8
February 7, 2013 at 20:27:19
NO DEAR I AM NOT GETTING AS PER MY REQUIRED OUTPUT.

SEE FINAL COMMAND WHICH I RUN

C:\>::======= begin batch
C:\>@echo off & setlocal
pushd c:\backup
for %a in (????TRD.TXT) do set x=%a
set new=ABCD_%x:~2,2%x:~0,2%date:~-4%
echo rename %x% %new%.txt
rename %x% ABCD_%x:~2,2%%x:~0,2%.txt
rename %x% %new%.txt
popd
::===== end batch

It rename my file name from 0207TRD.TXT to ABCD_0702.TXT, but i want this renamed file output to ABCD_07022013.TXT


Report •

#9
February 7, 2013 at 21:49:35
I'm sorry for making so many mistakes. Please have patience to try this one:
::==== begin BATCH SCRIPT chetan.bat
@echo off & setlocal
pushd c:\backup
for %%a in (????TRD.TXT) do call :xx %%a
popd
goto :eof

:xx
set x=%1
set new=ABCD_%x:~2,2%%x:~0,2%%date:~-4%
echo rename %x% %new%.txt
echo rename %x% %new%.txt
::===== END BATCH
I left safety on (considering my track-record to date). If it looks right, remove "echo" from the last line of active code and run it.


Report •

#10
February 7, 2013 at 22:04:51
NO, NOT RUNNING , GIVING ERROR

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\chetan>CD\

C:\>::==== begin BATCH SCRIPT chetan.bat
C:\>@echo off & setlocal
pushd c:\backup
for %a in (????TRD.TXT) do call :xx %a
Invalid attempt to call batch label outside of batch script.
popd
goto :eof
:xx
set x=%1
set new=ABCD_%x:~2,2%%x:~0,2%%date:~-4%
echo rename %x% %new%.txt
rename %1 ABCD_%12013.txt
::===== END BATCH


Report •

#11
February 8, 2013 at 17:19:48
Oh, I thought you had gone away from command-line scripting (it would be way better if you did. I won't nosily ask why you feel compelled to run batch from the prompt, so...) Ok:
C:\>     setlocal enabledelayedexpansion
C:\>     for %a in (????trd.txt) do (
more? set x=%a
more? set new=ABCD_!x:~2,2!!x:~0,2!%date:~-4%
more? ren !x! !new!
more? )

Of course, columns 1-6 above are prompts, don't type them, they appear by themselves, as if by majik ;-) This worked on my end, from the prompt.

Report •

#12
February 8, 2013 at 21:45:32
deart nbrane,

Thanks for your given efforts

I m not getting the solution for this. leave it. i ll do it manually on daily basis.

thanks once again


Report •

#13
February 9, 2013 at 10:04:06
Sorry. I think it would fly if you would run from script instead of commands, so it would have saved you work. You're welcome to start a new thread to get some other helpers on it. I'm not very good but there are some excellent people contributing on here.

Report •

Ask Question