.bat file writing

G

Guest

I've got a simple batch file that copies files from one drive to another. And
I know how to rename the file in the new location... but my problem is that I
want to date/time stamp the new file name. For example, I want file test.txt
to be renamed test(currentdatecurrenttime).txt. Is there an entry that
accommodates this?

Thanks!
 
R

Rob Barbour

You'll need to create two BAT files.
Pass the file you want to rename with date and time to RENDT.BAT

RENDT.BAT
--------------
@echo off


if {%1}=={} goto syntax
if not exist %1 goto syntax
setlocal
set filename=%1
set dp=%~dp1
set name=%~n1
set ext=%~x1
call univdate

for /f "Tokens=1" %%i in ('time /t') do set tm=%%i
set tm=%tm::=_%
pushd %dp%

set newname=%name%_%yy%%mm%%dd%_%tm%
rename "%name%%ext%" "%newname%%ext%"
popd
endlocal
goto :EOF
:syntax
@echo Syntax: Call RenDT filename

REM End of Rendt.bat


UNIVDATE.BAT
------------------
@echo off
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))

REM End of Univdate.bat
 
T

Torgeir Bakken \(MVP\)

Rob said:
You'll need to create two BAT files.
Pass the file you want to rename with date and time to RENDT.BAT

RENDT.BAT
(snip)
Hi

You can do it with only one batch file as well, using e.g the batch
date time functions from Ritchie Lawrence batch library available at
http://www.commandline.co.uk/lib

This works for me:

RENDT.BAT
--------------------8<----------------------
@echo off

if {%1}=={} goto syntax
if not exist %1 goto syntax
setlocal

call :GetDate year month day
call :GetTime hours mins secs hsecs

set filename=%1
set name=%~n1
set ext=%~x1

set newname=%name%(%year%%month%%day%_%hours%%mins%%secs%)
rename %filename% "%newname%%ext%"
endlocal

goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3. For NT4/2K/XP
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetTime hh nn ss tt
::
:: By: Ritchie Lawrence, updated 2002-10-30. Version 1.2
::
:: Func: Loads local system time components into args 1 to 4. For NT4/2K/XP
::
:: Args: %1 Var to receive hours, 2 digits, 00 to 23 (by ref)
:: %2 Var to receive minutes, 2 digits, 00 to 59 (by ref)
:: %3 Var to receive seconds, 2 digits, 00 to 59 (by ref)
:: %4 Var to receive centiseconds, 2 digits, 00 to 99 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:syntax
echo/Syntax: Call RenDT filename

REM End of Rendt.bat

--------------------8<----------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top