PowerShell Remove-Item不会删除文件

时间:2019-01-23 23:37:44

标签: powershell

我正在尝试使用此Remove-Item,以删除Powershell脚本的Source路径中的所有xml文件。它不会检索任何错误。。但是它也不会删除文件。

 Remove-Item –path $SourcePath* -include *.xml –recurse -force

SourcePath是正确的,因为脚本在其上一部分中运行良好。

如果它直接复制到目标文件夹结果文件中,为什么不删除文件?

Param (
    [Parameter(Mandatory = $True)][String]$SourcePath,
    [Parameter(Mandatory = $True)][String]$DestinationPath,
    [Parameter(Mandatory = $True)][ValidateSet("utf8")][String]$Encoding
)

$Files = Get-ChildItem $SourcePath -include *.xml -Recurse 

If ($Files.Count -gt 0) {

    If (!($DestinationPath.Substring($DestinationPath.Length - 1) -eq "\")) {
        $DestinationPath = $DestinationPath + "\" + $Files[0].Directory.Name + "\"
    }
    Else {
        $DestinationPath = $DestinationPath + $Files[0].Directory.Name + "\"
    }

    If (!(Test-Path -Path $DestinationPath)) {New-Item -ItemType Directory -Path $DestinationPath | Out-Null}

    ForEach ($File in $Files) {
        Write-Host "Read and Convert $($File.Name)" -ForegroundColor Cyan
        Get-Content $File.FullName  | Set-Content -Encoding $Encoding ($DestinationPath + $File.Name) -Force -Confirm:$false
        Remove-Item –path $SourcePath* -include *.xml –recurse -force
    }

    Write-Host "Conversion of $($Files.Count) Files to $Encoding in $DestinationPath completed" -ForegroundColor Green
}
Else {
    Write-Host "Source-Directory empty or invalid SourcePath." -ForegroundColor Red
}

0 个答案:

没有答案