繁体   English   中英

根据年月LastWriteTime持久化子文件夹将文件移动到子文件夹

[英]Move files to sub folders based upon year and month LastWriteTime persevering subfolder

您可以帮我修改此脚本吗?

我有一个带有几个子文件夹的根文件夹

c:// root / many-subfolders / many-logfiles。

我希望sctipt对长/年/月的长文件进行排序,然后输出到原始子文件夹(不包括当年的文件)

c:// root / many-subfolders / year / month / logfiles

$Filepath  = ""
$file      = ""
$date      = ""
$month     = ""
$year      = ""
$MonthPath = ""

$FilePath = Read-Host "Place the directory which contains the files."

Write-Warning "Note: This action can take several minutes, depending on the amount of files in $FilePath."

Get-ChildItem $FilePath | % {
    $file = $_.FullName 
    $date = Get-Date ($_.LastWriteTime)

    $month = $date.Month
    $year = $date.Year
    $MonthPath = "$FilePath\$year\$month"
    Write-Verbose "month = $month"
    Write-Verbose "Date = $date"
    Write-Verbose "year = $year"
    Write-Verbose "FilePath = $FilePath" 
    Write-Verbose "Filename = $file"
    Write-Verbose "MonthPath = $MonthPath"

    if (!(Test-Path -Path "$MonthPath" )) {
        Write-Verbose "Creating log location $MonthPath."
        #Write-Host -BackgroundColor green -ForegroundColor black "Creating log location $MonthPath."
        Write-Verbose "MonthPath inside path test = $MonthPath"
        New-Item -ItemType Directory -Path $MonthPath | Out-Null
    } else {
        #Write-Host -BackgroundColor green -ForegroundColor black "Log location exists already exist $MonthPath"
        Write-Verbose "Log location exists already exist $MonthPath" 
    }
    Move-Item "$file" "$MonthPath" | Out-Null
}

Write-Warning "All files are sorted now based upon year and month."

据我了解,您需要过滤c:\\ root \\中的所有文件,然后将它们移动到新目录。

    # Set the $Filepath you are going to search for the files, if you have a specific extension you may filter that for better results and performance.
    $FilePath = "C:\setup\testlogs"

        # Put all the files to an variable with some of their attributes you may need the CreationTime or the LastWriteTime depends on what you need!!!!
        $AllFiles = Get-ChildItem -Path C:\Setup\TestLogs -File -Recurse |select Name, FullName, CreationTime, LastWriteTime

        Foreach ($File in $AllFiles) 
            {
            # Put an if with less than the date you want the logs to be moved.
            If ($File.LastWriteTime -lt "11/07/2018")
                {
                    Write-Host "The file is ready for archive"
                    $Date = Get-Date ($File.LastWriteTime) -Format dd/MM/yyyy
                    $Month = $File.LastWriteTime.Month
                    $Year = $File.LastWriteTime.Year
                    $NewPath = "$FilePath\$year\$month"
                    $CheckExist = Test-Path $FilePath\$year\$month
                    # Check if the path already exist and move the file to the folder Else create the new folder and move the file there
                    If ($CheckExist -eq $true)
                        {
                        Move-Item -Path $File.FullName -Destination $FilePath\$year\$month\
                        }
                    Else
                        {
                        New-Item -Path $Filepath\$year\$month -ItemType Directory
                        Move-Item -Path $File.FullName -Destination $FilePath\$year\$month\
                        }
                }
            Else 
                {
                    Write-Host "The File is not that old to be archived"
                }
            }

希望这就是您要寻找的!

让我知道,以便为您解决问题提供更多帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM