String 拆分路径并仅取出最后一部分(文件名)Powershell
我是powershell的新手,目前正在尝试编写一个脚本,在文件中查找引用的文件路径,只取出路径的最后一部分(文件名),并将其移动到与包含它的文件夹相同的目标位置 我有一个函数脚本,它可以完成我想要的,唯一剩下的是它不应该寻找引用文件的整个路径。 因为路径不再正确。 它应该只查找文件名,然后找到并移动它 这是我当前的脚本:String 拆分路径并仅取出最后一部分(文件名)Powershell,string,powershell,parsing,split,String,Powershell,Parsing,Split,我是powershell的新手,目前正在尝试编写一个脚本,在文件中查找引用的文件路径,只取出路径的最后一部分(文件名),并将其移动到与包含它的文件夹相同的目标位置 我有一个函数脚本,它可以完成我想要的,唯一剩下的是它不应该寻找引用文件的整个路径。 因为路径不再正确。 它应该只查找文件名,然后找到并移动它 这是我当前的脚本: $source = 'Z:\Documents\16_Med._App\Aufträge\RuheEKG_24HBP_Skript\Ursprung_tes
$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
}
这是引用的位图文件:
您有几个选项可以在PowerShell中保存文件路径和文件名 内置cmdlet
# Get the file name
$fileName = Split-Path $refPath -Leaf
# Get the directory path
$dirPath = Split-Path $refPath -Parent
# Get the file name
$fileName = [System.IO.Path]::GetFileName($refPath)
# Get the directory path
$dirPath = [System.IO.Path]::GetDirectoryName($refPath)
.NET方法
# Get the file name
$fileName = Split-Path $refPath -Leaf
# Get the directory path
$dirPath = Split-Path $refPath -Parent
# Get the file name
$fileName = [System.IO.Path]::GetFileName($refPath)
# Get the directory path
$dirPath = [System.IO.Path]::GetDirectoryName($refPath)
如果要在另一个目录中查找文件名,可以构建如下新路径:
# built-in version
$otherPath = Join-Path $otherDir $fileName
# .NET version
$otherPath = [System.IO.Path]::Combine($otherDir, $fileName)
要获取要移动的文件名,请按照marsze的建议使用
分割路径
$FileName = Split-Path $refPath -Leaf
要定位文件,请使用Get ChildItem
。($SearchBase
是要搜索的路径)
现在要移动文件,请使用move Item
,然后再次使用Split Path
查找目标
Move-Item -Path $FileLocation -Destination $(Split-Path $file -Parent)
您的正则表达式似乎不正确(^.*)将匹配以任意字符数量不限开头的任何行。。。顺便说一句,我不希望过早地将
$allFiles
的路径转换为字符串。如果将路径保留为路径对象,您可以稍后更轻松地使用其成员,如.FullName
和.DirectoryName
。如何使用[A-Za-z]:(\\[\w\s-]*)+(\.\w{3})
作为路径的正则表达式?你不必在意行首的数字。他正在文本中搜索文件路径。路径已更改,但文件位于某个位置。所以他想把文件移到文本文件的位置。是的,我假设文本文件的位置存在。