一个通用的解决方案:让你的日志文件提前!
让我们去了所有的可能性,在接下来的批处理文件copy sourcefile targetfolder
指令(S)命名30332428copier.bat
:
@echo off
copy "D:\bat\Unusual Names\1exclam!ation.txt" "D:\test\a b"
copy "D:\bat\Unusual Names\2exc!lam!ation.txt" "D:\test\b a"
copy "D:\bat\Unusual Names\3exc!lam!ati!on.txt" "D:\test\a b"
copy "D:\bat\Unusual Names\4exc!lam!ati!on!.txt" "D:\test\n n"
该文件涵盖了所有组合:
- 的和第二源文件存在为第3个和第4个个没有。
- 目标文件夹
a b
(在和命令)存在,而文件夹b a
和n n
没有。
然后,运行下一个脚本
@ECHO OFF
SETLOCAL enableextensions
for /F "usebackq skip=1 tokens=1*" %%F in (
"D:\bat\StackOverflow\30332428copier.bat"
) do (
set "sourcefile="
set "targetfldr="
set "firstGtoken=TRUE"
for %%H in (%%G) do (
if defined firstGtoken (
if exist "%%~H" (
set "sourcefile=%%~H"
) else (
echo source not found "%%~H"
)
set "firstGtoken="
) else (
rem destination folder
if exist "%%~H\\\" (
set "targetfldr=%%~H"
) else (
echo target not found "%%~H"
rem instead echo, we could make it as follows:
rem md "%%~H"
)
)
)
if defined sourcefile if defined targetfldr (
rem all ok?
rem What if sourcefile exists in targetfldr?
echo %%F %%G
rem in addition to (or instead of) echo, we could `execute` the command
rem removing leading `rem` from next line
rem %%F %%G
)
)
我们得到下一输出:
==>D:\bat\StackOverflow\30332428.bat
copy "D:\bat\Unusual Names\1exclam!ation.txt" "D:\test\a b"
target not found "D:\test\b a"
source not found "D:\bat\Unusual Names\3exc!lam!ati!on.txt"
source not found "D:\bat\Unusual Names\4exc!lam!ati!on!.txt"
target not found "D:\test\n n"
==>
最后,我们可以运行30332428.bat>copylog.txt
,从而获得所需的记录。
还有最后一个问题:如果sourcefile
存在于targetfldr
会怎么样?如果出现问题并且30332428copier.bat
脚本因某种原因挂起而应该重复运行,该怎么办?
您使用的是Windows吗? –