一個通用的解決方案:讓你的日誌文件提前!
讓我們去了所有的可能性,在接下來的批處理文件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嗎? –