| 1. |
Solve : rename file to date and timstamp? |
|
Answer» I have a batch script as follows.
The above post (reply 1) did not include the original file name as part of the final file name. The following code corrects that mistake. ___________________________________ C:\test>type bang.bat Code: [Select]echo off rem cd D:\data\processed_Delivery\ cd c:\test rem dir /b *.csv > csvfiles.txt set MM=%date:~4,2% set DD=%date:~7,2% set YYYY=%date:~10,4% echo MM=%MM% echo DD=%DD% echo YYYY=%YYYY% for /f %%i in (csvfiles.txt) do ( copy %%i %%i_processed_%MM%%DD%%YYYY% dir /b %%i_processed_%MM%%DD%%YYYY% rem del %%i rem cd d:\data\scripts\ echo PROCESSED THE FILE %%i AT %date% %time% rem *.csv_processed_mm/dd/yyyy format. ) Output: C:\test>bang.bat MM=06 DD=29 YYYY=2010 1 file(s) copied. sdate.csv_processed_06292010 PROCESSED THE FILE sdate.csv AT Tue 06/29/2010 19:31:04.06 1 file(s) copied. stime.csv_processed_06292010 PROCESSED THE FILE stime.csv AT Tue 06/29/2010 19:31:04.06 1 file(s) copied. txtfile.csv_processed_06292010 PROCESSED THE FILE txtfile.csv AT Tue 06/29/2010 19:31:04.06 C:\test> p.s. I was unable to edit the above post ( reply one). HelloHi marvinengland, Thanks for the update. It works. I have one more problem. I am generating log file with the sqlloader. ie., sqlldr USERID=config/config control=D:\data\Scripts\loaddata_del.ctl skip=2 log=d:\log\log01.log data=%%f Now for every loop, there is log01.log file getting GENERATED, and hence it is getting overwritten. So, I tried to rename it to log01.log__. (I am using SIMPLE command copy *.log *.log.%DATE:/=%_%time::=%.processed del *.log ) But, For every loop iteration., ie., once the loop starts the TIME STAMP remains constant for all the loop items. How can i handle this ? thx. C:\test>type bang.bat echo off rem echo. > time.log rem dir /b > csvfiles.txt rem cd D:\data\processed_Delivery\ cd c:\test set MM=%date:~4,2% set DD=%date:~7,2% set YYYY=%date:~10,4% echo MM=%MM% echo DD=%DD% echo YYYY=%YYYY% for /f %%i in (csvfiles.txt) do ( copy %%i %%i_processed_%MM%%DD%%YYYY% dir /b %%i_processed_%MM%%DD%%YYYY% call :mklog %%i rem del %%i ) echo time.log type time.log goto :eof :mklog %1 sleep 6 rem echo PROCESSED THE FILE %1 AT %date% %time% echo PROCESSED THE FILE %1 AT %date% %time% >> time.log rem C:\Program Files\Windows RESOURCE Kits\Tools\sleep.exe rem If a time delay is needed for each timestamp, use ping or sleep rem The %time% variable will only change with a call to a label or another batch Output: C:\test>bang.bat MM=07 DD=01 YYYY=2010 1 file(s) copied. timefile1.csv_processed_07012010 1 file(s) copied. timefile2.csv_processed_07012010 1 file(s) copied. timefile3.csv_processed_07012010 1 file(s) copied. timefile4.csv_processed_07012010 time.log PROCESSED THE FILE timefile1.csv AT Thu 07/01/2010 10:39:26.15 PROCESSED THE FILE timefile2.csv AT Thu 07/01/2010 10:39:32.17 PROCESSED THE FILE timefile3.csv AT Thu 07/01/2010 10:39:38.19 PROCESSED THE FILE timefile4.csv AT Thu 07/01/2010 10:39:44.21 C:\test>Don't use sleep or ping. There is at least 1/100 of a second between each timestamp. When there are many *.csv files, the batch will run much faster C:\test>type bang.bat echo off echo. > time.log rem dir /b *.csv > csvfiles.txt rem cd D:\data\processed_Delivery\ cd c:\test set MM=%date:~4,2% set DD=%date:~7,2% set YYYY=%date:~10,4% echo MM=%MM% echo DD=%DD% echo YYYY=%YYYY% for /f %%i in (csvfiles.txt) do ( copy %%i %%i_processed_%MM%%DD%%YYYY% dir /b %%i_processed_%MM%%DD%%YYYY% call :mklog %%i rem del %%i ) echo time.log type time.log goto :eof :mklog %1 echo PROCESSED THE FILE %1 AT %date% %time% >> time.log rem The %time% variable will only change with a call to a label or another batch Output: C:\test>bang.bat MM=07 DD=01 YYYY=2010 1 file(s) copied. timefile1.csv_processed_07012010 1 file(s) copied. timefile2.csv_processed_07012010 1 file(s) copied. timefile3.csv_processed_07012010 1 file(s) copied. timefile4.csv_processed_07012010 time.log PROCESSED THE FILE timefile1.csv AT Thu 07/01/2010 15:53:36.94 PROCESSED THE FILE timefile2.csv AT Thu 07/01/2010 15:53:36.95 PROCESSED THE FILE timefile3.csv AT Thu 07/01/2010 15:53:36.97 PROCESSED THE FILE timefile4.csv AT Thu 07/01/2010 15:53:36.98 C:\test> |
|