每年/每月将文件移动到新的子文件夹中

我正在尝试将每个subfolder下的所有文件移动到另一个具有subfoldername_mmm_yyyy名称的文件夹中。

下面的代码仅将所有子文件夹中的所有文件移动到一个名为subfoldername_subfoldername_mmm_yyyy的文件夹中。

我知道我的for each循环是不正确的,但我不知道如何修复它们,谁能帮帮忙?

$curr_date = Get-Date
$folder_path = "C:\Logs\"
$file_type = "C:\Logs\*.log*"
$destination = "C:\Archive\"

# delete tmp files if existed
$deletefile = Get-ChildItem $destination -recurse -include *.7z.tmp -force | remove-item

# set min age of files
$max_days = "-1"

# determine how far back we go based on current date
$zip_date = $curr_date.AddDays($max_days)

Get-ChildItem -Path $folder_path | Where-Object { $_.Attributes -eq "Directory" } | foreach {
    # obtain all subfolders
    $servername = Get-ChildItem -Path $folder_path | Where-Object { $_.Attributes -eq "Directory" }

    # move all files into servername_mmm_yyyy folder
    Get-ChildItem $file_type -Recurse | Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false)}| foreach {
        $x = $_.LastWriteTime.ToShortDateString()
        $month_year = Get-Date $x -Format MMM_yyyy
        $file_destination = ($destination) + ($servername) + "_" + ($month_year)
        if (test-path $file_destination) { 
            move-item $_.fullname $file_destination 
        }
        else {
            new-item -ItemType directory -Path $file_destination
            move-item $_.fullname $file_destination 
        }
    }
}

转载请注明出处:http://www.xingnongyuan.com/article/20230526/2151837.html