Batch file with time issue before 10 AM

戏子无情 提交于 2019-12-08 13:20:41

问题


I have created a batch script to take a file, copy it to a folder with the date and time as the folder name and then to rename the file also based off the date and time. My issue is that any time before 10 AM, the command fails:

c:\ICVERIFY\ICWin420\DATADIR>md C:\SettlementReports\Settlement_Reports\DATADIR\
06-25-2014_ 709_08.24.txt
A subdirectory or file C:\SettlementReports\Settlement_Reports\DATADIR\06-25-201
4_ already exists.
Error occurred while processing: C:\SettlementReports\Settlement_Reports\DATADIR
\06-25-2014_.

Here is my batch file, I have put a time out right before it errors out.

cd c:\
cd icverify
cd icwin420
cd DATADIR
:: this is Regional settings dependent so tweak this according your current settings
Echo %DATE% %Time%
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2% FOR %%V IN (%1) DO Rename %%V %%V%TDate%
md C:\SettlementReports\Settlement_Reports\DATADIR\%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
timeout /T 1
copy ICRPT*.txt C:\SettlementReports\Settlement_Reports\DATADIR\%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
cd c:\
cd Settlementreports
cd settlement_reports
cd datadir
cd %date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
ren ICRPT*.TXT ICRPT_%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt

Any help is greatly appreciated.


回答1:


Your %time% variable contains a space instead of an initial 0 left padding the hours for hours less than 10:00. You can use

set "$time=%time: =0%"

to replace the space with a 0, storing the resulting value in a new variable. Then replace the references to %time% with the new variable %$time% as

md C:\ ....... \%date: ... %_%$time:~0,2%%$time:~3,2%_%$time:~6,5%.txt

Or instead, if the space is not a problem in your file names, quote all the file references to avoid the errors

md "C:\Settlem .... _%time:~6,5%.txt"

For any of the two options, do the change in all the commands in your file referencing files/folders




回答2:


replacing the space with a 0 (see MC ND's answer) is one possibility.

But you should get used to put any <path>\file.ext into doublequotes.

copy file.txt new path\file with spaces.txt

is interpreded as copy file.txt to new, and there are additional parameters path\file, with and spaces.txt, which are not expected.

This syntax works better:

copy "file.txt" "new path\file with spaces.txt"

Here there are only two parameters, each enclosed in doublequotes.



来源:https://stackoverflow.com/questions/24407233/batch-file-with-time-issue-before-10-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!