I need to rename a file using a bat file

November 22, 2010 at 12:44:55
Specs: Windows XP
I need to move a file to an existing folder and rename it using the current date, time, and year. The file being moved will always have the same name and extension before being moved, and will always be moved to the same folder. I would like to use a bat file or VB script if possible so that the task can be automated to run every day.

Thanks

olddude


See More: I need to rename a file using a bat file

Report •

#1
November 22, 2010 at 12:57:32
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b newfile') do (
move myfile.ext "d:\%%~Ta.ext"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
November 22, 2010 at 13:14:38
May be this is what you are asking...


@echo off

set source=Your existing file name
set destination=Your destination. like

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

FOR /F %%A IN ('TIME/T') DO SET Now=%%A

move %source% %destination%\%Year%-%Month%-%Day%-%now%.ext

Subhash Chandra.


Report •

#3
November 22, 2010 at 13:18:12
No point trying to guess the date layout.


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
November 23, 2010 at 05:26:57
Thanks. That moves the file just fine. I also need to replace the name with the current date and time when it is moved. Any way to do that in the same batch file?

oldguy


Report •

#5
November 23, 2010 at 05:39:12
I am getting the error below

FOR /F %%A IN ('TIME/T') DO SET Now=%%A
%%A was unexpected at this time.

Below is the batch file with file names and location included. Any suggestions?


@echo off

set source=d:\itlab transfer\test.txt
set destination=d:\itlab transfer\temp\. like

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

FOR /F %%A IN ('TIME/T') DO SET Now=%%A


move %test.txt% %d:\itlab transfer\test%\%Year%-%Month%-%Day%-%now%.ext


Report •

#6
November 23, 2010 at 05:40:55
Date format is not critical. Since there will only be one file a day I'm looking to make it unique using the date it was created.

oldguy


Report •

#7
November 23, 2010 at 06:58:34
Seems you modified the code a little bit...
============================================================

@echo off

FOR /F "tokens=1,2 delims=:" %%A IN ('TIME/T') DO SET Now=%%A-%%B
set source=d:\1.txt
set destination=d:\temp
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

move %source% "%destination%\%Year%-%Month%-%Day%-%now%.txt"

===========================================================
Just change the value of below 2 lines.put the proper paths after the = sign.

set source=d:\1.txt
set destination=d:\temp

and in last line .txt (change .txt to the actual extention of the file.)

Subhash Chandra.


Report •

Ask Question