Discussion Locked

Use bat file to create unique file names

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Can a batch file be used to rename a file that is created daily to a unique file name, so that the files will not be overwriten. If so, what command/wildcard is used and can I have an example? I am planning to run this in event manager on an NT machine. Thanks!

4 replies

by syscokid

This code will add the current date to the name of the file:

REM file_rename.bat

@echo off
cls

echo.|date /t >holdstr
for /f "tokens=2,3,4*" %%a IN (holdstr) do echo %%a%%b%%c >holdstr

for /f "tokens=1 delims=/" %%a IN (holdstr) do set month=%%a
for /f "tokens=2 delims=/" %%a IN (holdstr) do set day=%%a
for /f "tokens=3 delims=/" %%a IN (holdstr) do set year=%%a

set day=%day:~0,2%
set year=%year:~0,4%

if %month%==01 set month2=Jan
if %month%==02 set month2=Feb
if %month%==03 set month2=Mar
if %month%==04 set month2=Apr
if %month%==05 set month2=May
if %month%==06 set month2=Jun
if %month%==07 set month2=Jul
if %month%==08 set month2=Aug
if %month%==09 set month2=Sep
if %month%==10 set month2=Oct
if %month%==11 set month2=Nov
if %month%==12 set month2=Dec

REM ** NOW, rename your file. I did not specify a specific drive and directory.
REM ** So, run this in the current directory OR include the proper path

ren yourfile.txt yourfile%day%%month2%%year%.txt

del holdstr
set day=
set month=
set year=

by michelle

I tried to use your code but got some syntax errors and ended up using code from another Technical Q&A. But you definitely got me on the right track! Thanks a bunch!

This discussion is closed to new replies.