Batch file 批处理脚本随机无法将文件从Unix共享目录复制到Windows
我有一个批处理脚本,它每3秒钟读取一个Unix共享目录(X),并将所有文件复制到Windows 7 PC上的本地目录 成功复制文件后,脚本将删除Unix共享目录的内容,以便不再复制相同的文件 问题是脚本有时无法复制文件。 这似乎是相当随机的 脚本应该抛出任何遇到的错误,但是当这些事件发生时,我在输出日志上没有看到任何错误 因此,我的问题是: 1.我的脚本在错误捕获方面是否存在问题。日志仅具有基于errorLevel 0的输出(请参见脚本),即使Unix目录中没有文件,其中errorLevel 1输出到日志的输出也应根据代码执行Batch file 批处理脚本随机无法将文件从Unix共享目录复制到Windows,batch-file,copy,Batch File,Copy,我有一个批处理脚本,它每3秒钟读取一个Unix共享目录(X),并将所有文件复制到Windows 7 PC上的本地目录 成功复制文件后,脚本将删除Unix共享目录的内容,以便不再复制相同的文件 问题是脚本有时无法复制文件。 这似乎是相当随机的 脚本应该抛出任何遇到的错误,但是当这些事件发生时,我在输出日志上没有看到任何错误 因此,我的问题是: 1.我的脚本在错误捕获方面是否存在问题。日志仅具有基于errorLevel 0的输出(请参见脚本),即使Unix目录中没有文件,其中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%
也许可以试试这个版本。我们不需要插入单个命令,如果语句,最后,您的
printmate.log
有两个不同的版本,一个有路径,一个没有,让我们尝试保持一个标准。请按原样复制代码:
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
)
您的意思是,ever errorLevel应该被倒逗号包围?第一个errorLevel 0包含删除目录的代码(因为从目录复制时没有错误)。以下错误级别0和1是从del命令中捕获的,因此如何检查正确复制的文件?我想我已经在上面的脚本中捕获了xcopy命令中的所有错误代码,不知道如何在执行删除命令之前检查它们是否正确复制请根据我的回答尝试下面的代码“,它是一个引号;
”
-符号被称为撇号…感谢您尝试一下,我想计算复制的文件和从相关目录中删除的文件的数量,我在原始问题中添加了代码,但是脚本在目录中找不到任何文件时会暂停,有没有办法在执行计数之前检查dir是否为空?为什么不直接执行移动呢?移动前先计算文件数,然后检查目录是否为emtry,也就是说,如果是emtry,它将移动所有文件?让我对我的答案进行编辑,并向您展示一种方法。感谢您进行了一些小的自定义并正在工作