every day i get a file Test.rej generated in C:\logs folder, i need to
move this file to C:\backup\ folder with a date and time stamp attached
to it so that it will not overwrite the previous day file.
can anyone help me out how do it in batch file.
Regards
The syntax required varies with OS. For example, in Windows
95/98/ME, you can use the following _DTM Subroutine to
extract time and date tokens from the Real Time Clock that
are stored in CMOS RAM.
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 {Subroutine-Handler}
CALL %0 GOTO: _DTM
:: To execute (instead of view) command, remove ECHO/{demo} below
ECHO/{demo}REN TEST.REJ %DT%.REJ
SET DT=
GOTO EOF {=Subroutine-section-below=}
:_DTM (Usage: CALL %0 GOTO: _DTM)
%COMSPEC%/c %0 GOTO: 2_DTM ECHO. REM:|debug|find ":">%TEMP%.\_DTM.BAT
%COMSPEC%/c %0 GOTO: 2_DTM REM: ECHO.|debug %TEMP%.\_DTM.BAT>NUL
CALL %TEMP%.\_DTM.BAT %0 GOTO: 1_DTM
DEL %TEMP%.\_DTM.BAT
GOTO EOF
:1_DTM
SET DT=%9%8%5%4
GOTO EOF
:2_DTM
%3e100 e8 0 0 80 c3 2 88 dc cd 1a 89 97 1e 1 89 8f 22 1 c3
FOR %%F IN (g d120L8 q) DO %3%%F
FOR %%F IN ("e100'CALL %%1 %%2 %%3'" w q) DO %4%%F
:EOF {End-of-file}
====End cut-and-paste (omit this line)
For Win95/98/ME study/demo use. Cut-and-paste as plain-text Batch file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 95
C:\WORK>demo.bat
{demo}REN TEST.REJ 20051209.REJ
C:\WORK>============End screen capture
Note: In the Batch file shown, some commands generated are merely
ECHOed, not executed. This is so that you can see them, and judge
whether or not commands you see are indeed doing exactly what you
want. When you are satisfied with the inactive Batch commands you
see ECHOed with a "{demo}" prefix (to make it clear that they are
NOT being executed), remove the ECHO/{demo} to activate commands.
These commands will do nothing until this ECHO/{demo} is removed.
For Windows 2000/XP, the code is far simpler, since you can use
FOR /f to extract the required tokens from the system %DATE%
variable. Exact code and order of tokens depends on locale.
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/
Try this. It require Windows W2K/XP etc.
@echo off
setlocal
set year=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%yy:~2,2%
echo Today is %year%%mm%%dd%
And here's something I prepared earlier:
::GetStamp.bat::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
::
:: uses Windows Scripting Host
:: to set variables to the current date/time
:: for Win9x/ME/NT/W2K/XP
::
:: The date format is yyyy-mm-dd
:: The time format is hh.mm.ss
::
set amp=&
if not "%amp%"=="&" set amp=^^^&
set OF="%temp%.\tmp.vbs"
>%OF% echo n=now
>>%OF% echo p=Instr(1,n," ")
>>%OF% echo d=left(n,p-1)
>>%OF% echo t=mid(n,p+1,8)
>>%OF% echo 'The following 3 lines ensure leading zeros
>>%OF% echo t=((Hour(t)+100)*100+Minute(t))*100+Second(t)
>>%OF% echo d=(Year(d)*100+Month(d))*100+Day(d)
>>%OF% echo t=right(t,6)
>>%OF% echo t=left(t,2)+"."+mid(t,3,2)+"."+right(t,2)
>>%OF% echo d=left(d,4)+"-"+mid(d,5,2)+"-"+right(d,2)
>>%OF% echo WScript.Echo "set datenow=" %amp% d
>>%OF% echo WScript.Echo "set timenow=" %amp% t
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %OF%
set stamp=%datenow% %timenow%
:: example command for renaming a file in NT, with the date time
for %%a in ("file.txt") do echo ren %%a "%%~na_%stamp%_%%~xa"
:: cleanup
set OF=
set amp=
set stamp=
set datenow=
set timenow=
::GetStamp.bat::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Environement variables
rem ======================
SET APP_HOME=C:\test_ogl_FORCURR\APPHOME
SET EIS_HOME=h:\hyperion\EIS
SET ESSBASE_HOME=h:\hyperion\essbase
Rem Batch File to move reject file to backup folder
Rem ========================================
Rem moving reject file to back up folder
Rem ========================================
echo ==================================================== >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
echo moving file process started...>>
%APP_HOME%\FORCURR\logs\move_reject.log
*-->>> MOVE/y
"%APP_HOME%\FORCURR\logs\move_reject.rej"%date%"_"%time%""
"%APP_HOME%\FORCURR\backup\"
if %errorlevel% NEQ 0 GOTO MOVERROR
echo moving file process Finished...>>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
:MOVERROR
echo Error in moving reject file to backup folder. >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
exit;
thanks
below is the code ...... i dont understand how to modify it , can you
please modify it for me .
thanks
below is the code i am using :
--------------------------------------------------------------------------------------------------
REM Environement variables
rem ======================
SET APP_HOME=C:\test_ogl_FORCURR\APPHOME
SET EIS_HOME=h:\hyperion\EIS
SET ESSBASE_HOME=h:\hyperion\essbase
Rem Batch File to move reject file to backup folder
Rem ========================================
Rem moving reject file to back up folder
Rem ========================================
echo ==================================================== >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
echo moving file process started...>>
%APP_HOME%\FORCURR\logs\move_reject.log
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject.rej"_%date%"_"%time%" "
"%APP_HOME%\FORCURR\backup\"
if %errorlevel% NEQ 0 GOTO MOVERROR
echo moving file process Finished...>>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
:MOVERROR
echo Error in moving reject file to backup folder. >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
exit;
-------------------------------------------------------------------------------------------------------------------------
earliest response would be appriciated.
thanks
Raj,,
below is the code i am using :
--------------------------------------------------------------------------------------------------
REM Environement variables
rem ======================
SET APP_HOME=C:\test_ogl_FORCURR\APPHOME
SET EIS_HOME=h:\hyperion\EIS
SET ESSBASE_HOME=h:\hyperion\essbase
Rem Batch File to move reject file to backup folder
Rem ========================================
Rem moving reject file to back up folder
Rem ========================================
echo ==================================================== >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
echo moving file process started...>>
%APP_HOME%\FORCURR\logs\move_reject.log
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject.rej"_%date%"_"%time%" "
"%APP_HOME%\FORCURR\backup\"
if %errorlevel% NEQ 0 GOTO MOVERROR
echo moving file process Finished...>>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
:MOVERROR
echo Error in moving reject file to backup folder. >>
%APP_HOME%\FORCURR\logs\move_reject.log
echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
exit;
> every day i get a file Test.rej generated in C:\logs folder, i need to
> move this file to C:\backup\ folder with a date and time stamp attached
> to it so that it will not overwrite the previous day file.
*** If the DATE variable is defined in your DOS version, try:
MOVE C:\LOGS\TEST.REJ C:\BACKUP\TEST%DATE%.REJ
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/
the below code dint work
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject.rej"
"%APP_HOME%\FORCURR\backup\move_reject%date%.rej"
> hey Allen and foxidrive,
His name is William, just to let you know.
> can you please modify the code i
> have sent to you.
>
>
> below is the code i am using :
> --------------------------------------------------------------------------------------------------
> REM Environement variables
> rem ======================
> SET APP_HOME=C:\test_ogl_FORCURR\APPHOME
> SET EIS_HOME=h:\hyperion\EIS
> SET ESSBASE_HOME=h:\hyperion\essbase
>
>
> Rem Batch File to move reject file to backup folder
> Rem ========================================
> Rem moving reject file to back up folder
> Rem ========================================
>
> echo ==================================================== >>
> %APP_HOME%\FORCURR\logs\move_reject.log
> echo %date% %time% >> %APP_HOME%\FORCURR\logs\move_reject.log
> echo moving file process started...>>
> %APP_HOME%\FORCURR\logs\move_reject.log
>
Add this here. It's untested:
::
:: uses Windows Scripting Host
:: to set variables to the current date/time
:: for Win9x/ME/NT/W2K/XP
::
:: The date format is yyyy-mm-dd
:: The time format is hh.mm.ss
::
set amp=&
if not "%amp%"=="&" set amp=^^^&
set OF="%temp%.\tmp.vbs"
>%OF% echo n=now
>>%OF% echo p=Instr(1,n," ")
>>%OF% echo d=left(n,p-1)
>>%OF% echo t=mid(n,p+1,8)
>>%OF% echo 'The following 3 lines ensure leading zeros
>>%OF% echo t=((Hour(t)+100)*100+Minute(t))*100+Second(t)
>>%OF% echo d=(Year(d)*100+Month(d))*100+Day(d)
>>%OF% echo t=right(t,6)
>>%OF% echo t=left(t,2)+"."+mid(t,3,2)+"."+right(t,2)
>>%OF% echo d=left(d,4)+"-"+mid(d,5,2)+"-"+right(d,2)
>>%OF% echo WScript.Echo "set datenow=" %amp% d
>>%OF% echo WScript.Echo "set timenow=" %amp% t
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %OF%
set stamp=%datenow%_%timenow%
:: example command for renaming a file in NT, with the date time
for %%a in ("%APP_HOME%\FORCURR\logs\move_reject.rej") do (
ren "%%a" "%%~na_%stamp%_%%~xa"
)
MOVE /y "%APP_HOME%\FORCURR\logs\move_reject_%stamp%_.rej" "%APP_HOME%\FORCURR\backup\"
:: cleanup
set OF=
set amp=
set stamp=
set datenow=
set timenow=
>
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject_%stamp%_.rej"
:: command for renaming a file in NT, with the date time
for %%a in ("%APP_HOME%\FORCURR\logs\move_reject.rej") do (
ren "%%a" "%%~na_%stamp%_%%~xa"
)
everything seems to be fine......in below statement i could move the
file with out
%stamp%, ..but once i attch %stamp% as it is in the below statement .
its not working.
is this format correct ???
everything seems to be fine......in below statement i could move the
file with out
%stamp%, ..but once i attch %stamp% as it is in the below statement .
its not working.
is this format correct ???
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject_%stamp%_.rej"
"%APP_HOME%\FORCURR\backup
everything seems to be fine......in below statement i could move the
file with out
%stamp%, ..but once i attch %stamp% as it is in the below statement .
its not working.
is this format correct ???
MOVE/y "%APP_HOME%\FORCURR\logs\move_reject_%stamp%_.rej"
"%APP_HOME%\FORCURR\backup
[after observing the attempts to solve the problem & OP's responses...]
Ah - the oft-repeated "date and time" problem that has been answered a
thousand times before...
Since you give us your OS (W2K) we know that we can use %date% and %time% to
solve the problem, and that had you looked in alt.msdos.batch.nt you would
probably have encountered a solution.
You don't give us a format for the date and time as it appears for you (it
changes according to system settings) neither do you give us the format that
you WANT to use, so we can but publish the formula for you to alter to your
particular requirements.
In your batch at a convenient point (directly after the "@echo off" line is
good...) insert the following lines:
set ydt=%date:~-A%%time:~0,B%
set ydt=%ydt: =0%
echo %ydt%
pause
set ydt=%ydt:~C,4%%ydt:~D,2%%ydt:~E,2%_%ydt:~F,2%%ydt:~G,2%%ydt:~H,2%
Where :
A is preceded by "-" which means 'the LAST "A" characters of the DATE
string'
* eg : your date string may be "Fri 10/12/2005" so you'd want the last 10
characters, and A=10
B is the number of characters required from your TIME string.
* eg : your time string may be " 4:02:16.20" You need the " 4:02:16" part
(presumably) which is 8 characters so B=8
The next SET substitutes 0 (the character-string after the "=") for the
space (the character-string before the "=")
The echo and pause simply show the string constructed; they can be removed
after testing.
This should show "10/12/200504:02:16"
The final SET switches the character-sequence around. Each "%ydt:~X,Y%"
means "the substring of ydt, length Y starting at X" where the first
character is character 0 (see "SET/?" from the prompt for more detail)
If your string in ydt is "10/12/200504:02:16" then selecting
C=6, D=3, E=0, F=10, G=13, H=16
will re-set ydt to "20051210_040216" which is perfect for proper sorting on
a chronological basis.
The reason that incorporating "%date%_%time%" in your filename doesn't work
is that %date% may contain "/" or %time% a colon, neither of which may occur
in a directory or filename. Replacing "%date%_%time%" in your filename with
%ydt% after the above manipulation should solve your problem.
Another way would be
set ydt=%date:~-A%%time: =0%
echo %ydt%
pause
set ydt=%ydt:~C,4%%ydt:~D,2%%ydt:~E,2%_%ydt:~F,2%%ydt:~G,2%%ydt:~H,2%
which would generate a report of
"10/12/200504:02:16.20"
the final SET would remain the same - just another way to do the same thing.
HTH
...Bill
34) Enticing the current date into an environment variable
59) How get today's date elements into a environment variables?
116) How can I get yesterday's date into an environment variable?
128) How can I add date and time to each line of a text file?
144) How do I rename myfile.txt to myYYMMDD.txt with current date?
254761 Nov 11 2005 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi
1} How to get today's date elements into environment variables?
6} How does one get yesterday's date?
31} How many days ago was 31.12.2003? What date was it 100 days ago?
144994 Dec 7 2005 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
Try placing an ECHO in front of the command and a PAUSE on next line so you
can see what is actually expanding. ISTM, the "\FORCURR" is being repeated
after the "%APP_HOME%" is expanded. In effect, the command is expanding to
the following.
MOVE /y C:\test_ogl_FORCURR\APPHOME\FORCURR\logs\move_reject
_2005-12-09_06.02.26_.rej" "C:\test_ogl_FORCURR\APPHOME\FORCURR\backup"
Is this what you really expected it to expand to?
You might even post what the command actually expands to on your system and
state what you expected it to be.
--
Todd Vargo (double "L" to reply by email)
thanks and regards
Raj
*** Then %DATE% may not be set in your OS. What is your OS?
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/