我有批处理脚本,该脚本每3秒从Unix共享目录( X )中读取一个文件,并将所有文件复制到Windows 7 PC上的本地目录中。
文件成功复制后,脚本会删除Unix共享目录的内容,因此不会再次复制相同文件。
问题是脚本偶尔无法复制文件。 这似乎是随机的。
该脚本应该引发所有遇到的错误,但是当这些事件发生时,我在输出日志上看不到任何错误。
所以我的问题是: 1.我的脚本是否存在有关错误捕获的问题。即使在Unix目录中没有文件的情况下,日志也只能基于errorLevel 0进行输出(请参见脚本),而要记录的errorLevel 1输出应根据代码执行。
任何输入表示赞赏。
echo off
:START
rem /Y removes prompt fro do you want to overwrite?
xcopy /Y X:\ C:\Users\histology\Desktop\PrintMateCopy
rem for debugging missed cassettes, copy to a new directory and don't delete
xcopy /Y X:\ C:\Users\histology\Desktop\PrintmateCopy2
echo %errorLevel%
if %errorLevel% equ 5 (
echo Disk write error occurred.: Error %errorLevel% >> PrintMateLog.log
)
if %errorLevel% equ 4 (
echo %date% : %time% : Error: %errorLevel% : Initialization error occurred. There is not
enough memory or disk space, or you entered
an invalid drive name or invalid syntax on
the command line. >> C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
)
if %errorLevel% equ 2 (
echo Error: %errorLevel% user terminated the copy with Crtl C >> C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
)
if %errorLevel% equ 1 (
echo Error: %errorLevel% No files found >> C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
)
if %errorLevel% equ 0 (
echo %date% : %time% : Files were copied without error. Deleting Unix share files from dir..Errorlevel : %errorLevel% >> C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
rem /F Force del read only files. /Q force del without prompt
del /F /Q X:\
if %errorLevel% equ 1 (
echo No Unix share dir files found. error %errorLevel% C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
)
if %errorLevel% equ 0 (
echo Files deleted.>> C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log
)
)
添加:用于在复制之前计算目录中文件数量的脚本。
问题是在目录为空时脚本暂停吗,有没有办法在执行代码之前检查目录是否为空?
SET /A "N=0"
FOR /F %%f IN ('DIR /S /B /A:-D "*"') DO (
SET /A "N=!N!+1"
)
ECHO N is %N%
答案 0 :(得分:1)
也许尝试这个版本。我们不需要用单个命令括起来,如果语句,最后,您的printmate.log
有2个不同的版本,一个有路径,一个没有路径,让我们尝试保持一个标准。请完全按原样复制代码:
echo off
rem /Y removes prompt fro do you want to overwrite?
xcopy /Y X:\ C:\Users\histology\Desktop\PrintMateCopy
rem for debugging missed cassettes, copy to a new directory and don't delete
xcopy /Y X:\ C:\Users\histology\Desktop\PrintmateCopy2
echo %errorLevel%
if %errorLevel% equ 5 echo Disk write error occurred.: Error %errorLevel%>>.>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
if %errorLevel% equ 4 echo %date% : %time% : Error: %errorLevel% : Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
if %errorLevel% equ 2 echo Error: %errorLevel% user terminated the copy with Crtl C>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
if %errorLevel% equ 1 echo Error: %errorLevel% No files found>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
if %errorLevel% equ 0 goto :delfiles
Goto :EOF
:delfiles
echo %date% : %time% : Files were copied without error. Deleting Unix share files from dir..Errorlevel : %errorLevel%>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
del /F /Q X:\
If %errorlevel% equ 0 (
echo files deleted successfully>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
) else (
echo error deleting files>>"C:\Users\histology\Desktop\PrintmateCopy2\PrintMateLog.log"
)
修改
根据您的评论,也许我们可以只检查x:\
中的文件数加上c:\
中的文件数,然后移动所有文件并检查c:
\
中的文件数是否为如果不能重试,则在移动后保持不变。
@echo off
:start
set "home_path=C:\Users\histology\Desktop\PrintMateCopy"
for /f %%i in ('dir /a-d-s-h /b X:\ ^| find /v /c ""') do echo set xcnt=%%i
for /f %%i in ('dir /a-d-s-h /b "%home_path%" ^| find /v /c ""') do echo set ccnt=%%i
set /a compcount=xcnt+ccnt
move /Y X:\* "%home_path%
for /f %%i in ('dir /a-d-s-h /b %home_path% ^| find /v /c ""') do echo set mvcnt=%%i
if "%mvcnt%"=="%compcont%" (
echo All files moved
) else (
echo not all files moved %mvcnt% files remains
goto :start
)