我希望根據檔案名的后半部分移動檔案。檔案看起來像這樣
43145123_Stuff.zip
14353135_Stuff.zip
2t53542y_Stuff.zip
422yg3hh_things.zip
我只想移動以 Stuff.zip 結尾的檔案,到目前為止我在 PowerShell 中有這個,但它只會根據檔案名的前半部分移動檔案。
#set Source and Destination folder location
$srcpath = "C:\Powershelltest\Source"
$dstpath = "C:\Powershelltest\Destination"
#Set the files name which need to move to destination folder
$filterLists = @("stuff.txt","things")
#Get all the child file list with source folder
$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
#loop the source folder files to find the match
foreach ($file in $fileList)
{
#checking the match with filterlist
foreach($filelist in $filterLists)
{
#$key = $file.BaseName.Substring(0,8)
#Spliting value before "-" for matching with filterlists value
$splitFileName = $file.BaseName.Substring(0, $file.BaseName.IndexOf('-'))
if ($splitFileName -in $filelist)
{
$fileName = $file.Name
Move-Item -Path $($file.FullName) -Destination $dstpath
}
}
}
uj5u.com熱心網友回復:
狀態目標與代碼實際執行的操作之間似乎存在一些差異。這會將檔案移動到目標目錄。當您確信檔案將被正確移動時,請-WhatIf從Move-Item命令中洗掉。
$srcpath = "C:\Powershelltest\Source"
$dstpath = "C:\Powershelltest\Destination"
Get-ChildItem -File -Recurse -Path $srcpath |
ForEach-Object {
if ($_.Name -match '.*Stuff.zip$') {
Move-Item -Path $_.FullName -Destination $dstpath -WhatIf
}
}
uj5u.com熱心網友回復:
實際上,這可以非常有效地用 PowerShell 撰寫(我希望我得到了正確的細節,讓我知道):
Get-ChildItem $srcpath -File -Force -Recurse |
where { ($_.Name -split "_" | select -last 1) -in $filterLists } |
Move-Item $dstpath
或者,如果你只是想看看這一個特定的過濾器,可以直接指定,使用通配符:
Get-ChildItem $srcpath -Filter "*_Stuff.zip"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338508.html
標籤:电源外壳
