编写脚本,以按月从LastModificationDate属性对文件进行排序:

  • 脚本为每个文件创建“月”目录,并将其移动到那里
  • 输入参数是文件集的路径(强制性,接受管道)
  • 脚本不返回任何内容

  • 此功能仅在当前目录上有效。需要它带有文件夹参数。如何更改脚本?
    function Get-FileMonth {
    
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true)]
            [string]$Folder
        )
            $Files = Get-ChildItem -Path $Folder -File
            Get-ChildItem -Path $Folder -File | Where-Object {$_.LastWriteTime.Month}`
            | New-Item -Name {$_.LastWriteTime.Month} -ItemType Directory
            ForEach ($File in $Files)
                {
                Copy-Item $File -Destination "$($File.LastWriteTime.Month)"
                }
        }
    

    最佳答案

    这就是我要执行的任务的方式:

    function Format-Files ($folder) {
    
        #Get all files in the folder
        $files = Get-ChildItem -Path $folder -File
    
        foreach($file in $files){
            #Create a new path using the LastWriteTime.Month property
            $fullPath = (Join-Path -Path $folder -ChildPath $file.LastWriteTime.Month)
    
            if (!(Test-Path $fullPath)){
                #The directory needs to be created
                New-Item -Name $file.LastWriteTime.Month -Path $folder -ItemType Directory
            }
    
            #Copy the item to the new directory
            Copy-Item -Path $file.FullName -Destination $fullPath
        }
    }
    

    如果您有任何疑问或问题,请告诉我。您应该这样使用:
    Format-Files("C:\Path\To\Directory")
    

    关于powershell - 编写脚本,以按月从LastModificationDate属性对文件进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60042238/

    10-13 07:46
    查看更多