Batchfile for backup with timestamp (1 Viewer)

Status
Not open for further replies.

razaqad

Raza
Local time
Today, 08:36
Joined
Mar 12, 2006
Messages
83
I got this searching on google

might be helpful

Code:
echo off

set file=%1


:: If no file is given on the command line then use 
:: this filename as an example 
if [%file%]==[] set file="Cabinet Budget.pdf" 


set amp=& 
if not "%amp%"=="&" set amp=^^^& 
set TmpFile="%temp%.\tmp.vbs" 
 >%TmpFile% echo n=now 





>>%TmpFile% echo 'Extract the date and time strings into d and t 
>>%TmpFile% echo s=Instr(1,n," ") 
>>%TmpFile% echo d=left(n,s-1) 
>>%TmpFile% echo t=mid(n,s+1,8) 
>>%TmpFile% echo 'Create time t variable in hhmmss format 
>>%TmpFile% echo 'The following line ensure leading zeros 
>>%TmpFile% echo t=((Hour(t)+100)*100+Minute(t))*100+Second(t) 
>>%TmpFile% echo t=right(t,6) 
>>%TmpFile% echo 'extract hour, minute, second variables 
>>%TmpFile% echo h=left(t,2) 
>>%TmpFile% echo m=mid(t,3,2) 
>>%TmpFile% echo s=right(t,2) 
>>%TmpFile% echo 'Create date d variable in yyyymmdd format 
>>%TmpFile% echo 'The following line ensure leading zeros 
>>%TmpFile% echo d=(Year(d)*100+Month(d))*100+Day(d) 
>>%TmpFile% echo 'extract year, month, day variables 
>>%TmpFile% echo y=left(d,4) 
>>%TmpFile% echo r=mid(d,3,2) 
>>%TmpFile% echo m=mid(d,5,2) 
>>%TmpFile% echo d=right(d,2) 
>>%TmpFile% echo WScript.Echo  "set hour=" %amp% h 
>>%TmpFile% echo WScript.Echo   "set min=" %amp% m 
>>%TmpFile% echo WScript.Echo   "set sec=" %amp% s 
>>%TmpFile% echo WScript.Echo  "set year=" %amp% y 
>>%TmpFile% echo WScript.Echo    "set yr=" %amp% r 
>>%TmpFile% echo WScript.Echo "set month=" %amp% m 
>>%TmpFile% echo WScript.Echo   "set day=" %amp% d 
>>%TmpFile% echo 'split the filename on the command line into 
>>%TmpFile% echo 'the name and the extension 
>>%TmpFile% echo f=%file% 
>>%TmpFile% echo s=len(f) 
>>%TmpFile% echo for c=1 to s 
>>%TmpFile% echo if mid(f,c,1)="." then x=c 
>>%TmpFile% echo next 
>>%TmpFile% echo if x=0 then x=s+1 
>>%TmpFile% echo n=left(f,x-1) 
>>%TmpFile% echo x=mid(f,x) 
>>%TmpFile% echo WScript.Echo   "set name=" %amp% n 
>>%TmpFile% echo WScript.Echo   "set ext="  %amp% x 


cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat" 
call "%temp%.\tmp.bat"
 
del  "%temp%.\tmp.bat" 
del  %TmpFile% 

set TmpFile= 
set amp= 

echo The year (YYyy) is "%year%" 
echo The year   (yy) is   "%yr%" 
echo The month       is   "%month%" 
echo The day         is   "%day%" 
echo. 
echo The hour   is "%hour%" 
echo The minute is "%min%" 
echo The second is "%sec%" 
echo. 


set stamp=%year%-%month%-%day%_%hour%.%min%.%sec% 


echo The date and time stamp is "%stamp%" 
echo. 
echo time   (hhmmss)   (%hour%%min%%sec%) 
echo. 
echo date A (yyyymmdd) (%year%%month%%day%) 
echo date B (mmddyyyy) (%month%%day%%year%) 
echo date C (ddmmyyyy) (%day%%month%%year%) 
echo. 
echo date D [yymmdd]   [%yr%%month%%day%] 
echo date E [mmddyy]   [%month%%day%%yr%] 
echo date F [ddmmyy]   [%day%%month%%yr%] 
echo. 
echo The filename is "%name%" and the extension is "%ext%" 
:: command to rename the file with the date-time stamp 
:: remove the echo to enable the rename command 
echo ren %1 "%name%_%stamp%%ext%" 
:: datetime.bat ::::::::::::::::::::::::::::::::::::::::::::::::::::::::

save the above code in a batchfile and see........... this file creats a vb script file in the temporary directory. To see that , just put REM before the line del "%temp%.\tmp.bat" and open yor temp folder by typing %temp% in the address bar of windows explorer
 

steevie_t

Registered User.
Local time
Today, 04:36
Joined
Feb 9, 2006
Messages
21
There is an alternative that doesn't need vbscript or temp files...

%DATE% and %TIME% are DOS environment variables and like other variables you can do simple string manipulation.

i.e.
Code:
SET CURHOUR=%TIME:~0,2%
Gives the current hour, but watch out before 10 am this will yield a space so use:
Code:
SET CURHOUR=%CURHOUR: =0%
To replace the spaces with zeros.

Code:
set year=%date:~6,4%
set yr=%date:~8,2%
set month=%date:~3,2%
set day=%date:~0,2%
set hour=%time:~0,2%
set hour=%hour: =0%
set min=%time:~3,2%
set sec=%time:~6,2%
echo year: %year% yr: %yr% month: %month% day: %day%
echo hour:%hour% min: %min% sec: %sec%

works a treat!
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom