我正在嘗試將檔案名末尾的奇數檔案從一個檔案夾移動到另一個檔案夾,但它似乎不起作用。我試過使用 if 陳述句和 for 回圈無濟于事。一些指標將不勝感激。我的嘗試如下..
$srcpath = "C:\Folder\SubFolder3"
$dstpath = "C:\Folder\SubFolder2"
Get-ChildItem -File -Recurse -Path $srcpath |
ForEach-Object {
if($_.Name -match '*1.txt', '*3.txt', '*5.txt', '*7.txt', '*9.txt') {
Move-Item -Path $_.FullName -Destination $dstpath
}
}
$srcpath = "C:\Folder\SubFolder3"
$dstpath = "C:\Folder\SubFolder2"
$files = $srcpath
foreach ($file in $files) {
if ($file -like '*1.txt', '*3.txt', '*5.txt', '*7.txt', '*9.txt') {
Move-Item -Destination $dstpath
}
}
uj5u.com熱心網友回復:
我相信-match只有在字串中找到所有匹配項時,運算子才會回傳 true,而不是只找到其中??一個。
如果檔案名僅包含一位數字,您可以嘗試使用類似的方法。如果檔案名中的任何其他位置有數字,這將失敗。
$srcpath = "C:\Folder\SubFolder3"
$dstpath = "C:\Folder\SubFolder2"
Get-ChildItem -File -Recurse -Path $srcpath |
ForEach-Object {
# look for a digit
if($_.Name -match '[\d]') {
# is that digit odd?
if([int]($Matches[0]) % 2 -eq 1) {
Move-Item -Path $_.FullName -Destination $dstpath
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495161.html
下一篇:回圈管道引數-有什么意義?
