我很新手powershell,目前正在尝试编写一个脚本,该脚本查找文件中的引用文件路径,仅获取路径的最后一部分(文件名),并将其移动到包含它的文件夹相同的目标位置。
我有一个功能脚本可以实现我的要求,唯一剩下的问题是它不应该再寻找已经不正确的引用文件的整个路径。 因为路径不再正确,它应该只查找文件名并找到并移动它。
这是我的当前脚本:
这是参考位图文件:
我有一个功能脚本可以实现我的要求,唯一剩下的问题是它不应该再寻找已经不正确的引用文件的整个路径。 因为路径不再正确,它应该只查找文件名并找到并移动它。
这是我的当前脚本:
$source = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\Ursprung_test'
$destination = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\24BHD'
$toDelete = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\ToDelete'
$pattern1 = 'AmbulatoryBloodPressure'
$pattern2 = 'RuheEKG'
# Erstellt Array mit pfad und filename
$allFiles = @(Get-ChildItem $source -File | Select-Object -ExpandProperty FullName)
foreach($file in $allFiles) {
# Dateinhalt als Array
$content = Get-Content -Path $file
# Wählt Destinationspfad
if ($content | Select-String -Pattern $pattern1 -SimpleMatch -Quiet)
{
$dest = $destination
}
else {
$dest = $toDelete
}
# Prüft ob Datei einen Pfad enthält
$refCount = 0
$content | Select-String -Pattern '(^.*)([A-Z]:\\.+$)' -AllMatches | ForEach-Object {
$prefix = $_.Matches[0].Groups[1].Value
$refPath = $_.Matches[0].Groups[2].Value # Bitmap file Path wird geholt
if (Test-Path -Path $refPath -PathType Leaf) {
Write-Host "Moving referenced file '$refPath' to '$dest'"
Move-Item -Path $refPath -Destination $dest -Force
}
else {
Write-Warning "Referenced file '$refPath' not found"
}
}
$refPath -split "\"
$refPath[4]
write-host $refpath
# Bewegt die Files an die vorher festgelegte Destination.
Write-Host "Moving file '$file' to '$dest'"
Move-Item -Path $file -Destination $dest -Force
}
这是参考位图文件:
![输入图片描述](https://istack.dev59.com/33v7o.webp)
$allFiles
的路径转换为字符串。如果你保留路径作为路径对象,你可以更轻松地后续使用其成员,如.FullName
和.DirectoryName
。 - T-Me[A-Za-z]:(\\[\w\s-]*)+(\.\w{3})
怎么样?你不需要关心行开头的数字。 - T-Me