Renaming a file using a BAT command

By i.stedman ·

I have two files that are produced on a regular basis.

I should like to keep a copy of these for up to two months.

The first file is produced from or Production Monitoring System, and records the production per machine per shift, or until the job ends.

This is picked up by the BAT file every 24 minutes and the data is transferred into our stock system before the file is deleted ready for the next file. I want to keep a copy of each file and give it a distinct name, either a date and time or an incremental number.

The other file is a backup, which I want to copy off the server, and again retain a copy for up to two months.

I tried using the for / f as suggested elsewhere but that was unexpected.

The Servers is 2003 R2, whilst the PC is XP pro.

Can anyone help?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Renaming the file

by Bizzo In reply to Renaming a file using a B ...

The batch file below will rename the file to the format YYYY-MM-DD_hh-mm.txt (the current date and time not the file date and time)
I've tested it and it works on 2003 and XP.

@echo off
for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C

for /F "tokens=1-4 delims=: " %%A in ('time/t') do (
set TimeHour=%%A
set TimeMinute=%%B

set NewFileName=%DateYear%-%DateMonth%-%DateDay%_%TimeHour%-%TimeMinute%

rename oldfile.txt %NewFileName%.txt

Related Discussions

Related Forums