我的最终目标是通过仅复制扩展名为sln,vcxproj,cs,cpp,hpp,c和h的文件来限制文件夹及其子目录的镜像。子文件夹太多,无法手动管理。我尝试在powershell中使用robocopy命令,但是在Powershell 1.0中是不可能的。如何在Powershell中实现我的目标?
我设法借用了一些代码并对它进行了一些细微的调整。它本身不是robocopy,但它正在设法接近我所追求的。注意:这只是我复制的代码,而不是我修改的代码。
$Source = '\\ServerA\Folder\Root\'
$Destination = '\\ServerB\Folder\Root'
$SrcEntries = Get-ChildItem $Source -Recurse
foreach($Src in $SrcEntries)
{
$SrcPath = $Src.fullname
$DesPath = $SrcPath.Replace($Source, $Destination)
#Check is source exist in dest
if(test-Path $DesPath)
{
#if source was change
If(Compare-Object $SrcPath $DesPath)
{
#then replace
Copy-Item $SrcPath $DesPath -Force
}
}
else
{
#if dont exist then copy too
Copy-Item $SrcPath $DesPath
}
}
$DesEntries = Get-ChildItem $Destination -Recurse
foreach($Des in $DesEntries)
{
$DesPath = $Des.fullname
$SrcPath = $DesPath.Replace($Destination, $Source)
if((test-Path $SrcPath) -eq $false)
{
#if source dont exist then delete dest
Remove-Item $DesPath
}
}
Robocopy不是PowerShell命令。这是Windows命令行实用程序,XP不附带。
您需要获得Windows Server 2003资源工具包工具才能获得Robocopy for XP。拥有它之后,就可以在PowerShell中使用它了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句