Powershell - IO.Directory - Find file types in all subdirectories

前端 未结 3 1136
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:39

I ran across this code in another post that almost does what I need, but can\'t figure out how to modify it to look for specific file types, i.e. *.bak, .txt, etc. I\'m

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 16:11

    Here's a simple PowerShell script to move many files from a directory into a structured directory by year, month and day. This is very fast, I had over 500K files.

    $listofFiles =   [System.IO.Directory]::EnumerateFiles("D:\LotsOfFiles","*.*","TopDirectoryOnly")
    
    $listofFiles |% {
       $file = New-Object System.IO.FileInfo($_)    
       $date = Get-Date ($file.CreationTime)
       $filepath = ("{0}\{1:00}\{2:00}-{3}\{4:00}\" -f "D:\LotsOfFiles", $date.year, 
       $date.month, $date.toString("MMM"), $date.day)
       Write-Output ("Move: {0} to {1}" -f $file.Fullname, $filepath)
    
       if (! (Test-Path $filepath)) {       
          new-item -type Directory -path $filepath      
       }
    
       move-item $file $filepath
    }   
    

    This probably can be improved but it works.

提交回复
热议问题