Powershell 验证Xcopy是否复制了任何内容

Powershell 验证Xcopy是否复制了任何内容,powershell,xcopy,Powershell,Xcopy,我试图在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”

我试图在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或file”


PowerShell将任何命令的标准输出捕获为字符串或字符串数组

从这里,您可以使用regex捕获$Matches变量中的文件副本计数,该变量是使用-match创建的。注意-$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
}
$result=xcopy/D/S/E“C:\folder1\*.*”C:\folder2”/y
($results | Where对象{$\匹配“(\d+)文件”})-match“(?\d+)文件”
如果([int]$Matches.Count-gt 0){
#做事
}
否则{
#写消息,写日志,随便什么
}

我不相信
xcopy
提供了日志功能,但是,如果您能够使用
robocy
您可以使用内置的日志功能,例如
/log:“c:\path to logs files\some log file.txt”
即使没有复制任何文件,它也会创建日志。我无法发布。为什么要使用xcopy而不是复制项目?现在您混合使用cmd和powershell命令,这会导致额外的复杂性,因为您必须解析字符串。使用Get-Childitem获取文件列表,然后使用copy-item复制这些文件,通过使用对象而不是字符串,使您能够以Powershell的方式工作。感谢您的帮助。它总是给别人。@alanbr00我解决了问题并更新了答案。@alanbr00我在if评估中添加了
[int]
。正则表达式生成字符串,因为您正在匹配字符串。我认为它足够聪明,可以自己转换谢谢这帮了大忙。
$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
}