验证 Xcopy 是否复制了任何内容
Verify if Xcopy did Copy Anything
我正在尝试在 powershell 中验证 xcopy 是否复制了某些内容。
感谢您的帮助。
xcopy /D /S /E "C:\folder1\*.*" "C:\folder2" /y
IF %CopiedFilesCount% >0 {
Start-Process C:\folder3\execute.bat
}
else{
"0 file copied"
}
在 bat 文件中,这段代码几乎可以满足我的要求。尝试将 "SourceFile" 更改为 "SourceFolder" 并将 "DeleteFile" 更改为 "execute command or file"
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=C:\folder1\file2.txt"
set "DeleteFile=test.txt"
set "DestinationDirectory=C:\folder2\"
for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%SourceFile%" "%DestinationDirectory%" /C /D /Q /Y 2^>nul') do set "CopiedFilesCount=%%I"
if %CopiedFilesCount% GTR 0 del "%DeleteFile%"
PowerShell 会将任何命令的标准输出捕获为字符串或字符串数组。
从那里您可以使用正则表达式在使用 -match 创建的 $Matches 变量中捕获文件副本计数。注意 - $Matches 变量仅在您传入单个字符串时填充。您可以使用 -match 从数组中获取正确的行,但随后您需要再次匹配以获取捕获组。 “?”创建一个命名的捕获组,我们可以作为 $Matches 的 属性 访问。
$result = xcopy /D /S /E "C:\folder1\*.*" "C:\folder2" /y
($results | Where-Object {$_ -match "(\d+) File"}) -match "(?<Count>\d+) File"
if ([int]$Matches.Count -gt 0) {
# Do Stuff
}
else {
# Write a message, write to a log, whatever
}
我正在尝试在 powershell 中验证 xcopy 是否复制了某些内容。 感谢您的帮助。
xcopy /D /S /E "C:\folder1\*.*" "C:\folder2" /y
IF %CopiedFilesCount% >0 {
Start-Process C:\folder3\execute.bat
}
else{
"0 file copied"
}
在 bat 文件中,这段代码几乎可以满足我的要求。尝试将 "SourceFile" 更改为 "SourceFolder" 并将 "DeleteFile" 更改为 "execute command or file"
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=C:\folder1\file2.txt"
set "DeleteFile=test.txt"
set "DestinationDirectory=C:\folder2\"
for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%SourceFile%" "%DestinationDirectory%" /C /D /Q /Y 2^>nul') do set "CopiedFilesCount=%%I"
if %CopiedFilesCount% GTR 0 del "%DeleteFile%"
PowerShell 会将任何命令的标准输出捕获为字符串或字符串数组。
从那里您可以使用正则表达式在使用 -match 创建的 $Matches 变量中捕获文件副本计数。注意 - $Matches 变量仅在您传入单个字符串时填充。您可以使用 -match 从数组中获取正确的行,但随后您需要再次匹配以获取捕获组。 “?”创建一个命名的捕获组,我们可以作为 $Matches 的 属性 访问。
$result = xcopy /D /S /E "C:\folder1\*.*" "C:\folder2" /y
($results | Where-Object {$_ -match "(\d+) File"}) -match "(?<Count>\d+) File"
if ([int]$Matches.Count -gt 0) {
# Do Stuff
}
else {
# Write a message, write to a log, whatever
}