computing
  • 0

batch file to rename files by date.

  • 0

Help! I’m have been trying to figure a way to write a batch file that will automatically rename a specific file by the date and time.

example: skidog.txt ==> jan191230.txt

Hmmm.
The below will rename a file in dirtest
to todays date. Just change the path.

CLS
@ECHO OFF
IF NOT “%1″==”date” ECHO Program will rename ECHO a filewith todays date.
IF “%3″==”Mon” SET Day=Monday
IF “%3″==”Tue” SET Day=Tuesday
IF “%3″==”Wed” SET Day=Wednesday
IF “%3″==”Thu” SET Day=Thursday
IF “%3″==”Fri” SET Day=Friday
IF “%3″==”Sat” SET Day=Saturday
IF “%3″==”Sun” SET Day=Sunday
Set FullDate=%4
goto getdate
:start2
echo rename test*.* %fulldate%.* > try.bat
call try
rem del try.bat
goto end
:getdate
ECHO.|DATE>t.bat
CALL t.bat
DEL t.bat
goto start2
:end
echo A file called %fulldate% has been ECHO created.

Easiest approach:

Requires the ability to use the DOS extensions (NT4 SP3 or SP4 onwards I think???)

rem Rename File

@echo off

rem Enable Extensions
verify other 2>nul
setlocal enableextensions
if errorlevel 1 goto ErrNoExtensions

rem Set current Date & Time variables. Change the following lines to get the correct info out. For more info, use the DOS command: help set
set SHORTDATE=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
set SHORTTIME=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

rem Rename file
ren %1 %SHORTDATE%_%SHORTTIME%.*

endlocal

goto end

:ErrNoExtensions
echo Extensions not enabled. Cannot continue.

:End

I am looking to make a similar script, but mine needs to capture the output of a small dos application i have created and rename the file to that output.
c:test.exe file.txt
OUTPUT

I want file.txt to be renamed to OUTPUT, but i cant figure out how. Thanks in advance.

Share