簡體   English   中英

根據文件名中的奇數移動文件 - powershell

[英]Move files based on odd number in file name - powershell

我試圖將文件名末尾帶有奇數的文件從一個文件夾移動到另一個文件夾,但它似乎不起作用。 我試過使用 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
    }
}

我相信-match運算符僅在字符串中找到所有匹配項時才返回 true,如果僅找到其中一個匹配項則不會。

如果文件名只包含一位數字,您可以嘗試使用類似的方法。 如果文件名中的任何其他 position 中有數字,這將失敗。

$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
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM