Move files over a certain size to specific directory (and exclude specifc sub-directories)

StackOverflow https://stackoverflow.com/questions/21379562

  •  03-10-2022
  •  | 
  •  

Question

Background

Having recently signed up for a 50GB box account I went to upload my pc's photo collection. But, most of my directories contained sub-directories with files greater than the allowed single file upload limit of 250MB. So some preparation work was needed

I googled the problem expecting to see a robocopy solution, but came across this intriguing looking powershell script by Jugal Shah instead. Building on my previously non-existent knowledge of powershell, I installed the requisite files, googled a little further, then bashed together the script below which worked on my basic testing.

Questions

I have a couple of questions before I run this on my precious actual files (backed up yes, but always cautious).

  1. Any major issues with my approach below, can and should it be improved?
  2. By chance on my testing I discovered that the script wont work on hidden folders - is this feature the best method to exclude specific directories (say 2013 and 2014) from the script beforehand, or can this be done directly in powershell?

Script

#Mention the path to search the files
$path = "c:\temp"
##Find out the files greater than equal to below mentioned size
$size = 249MB
##Limit the number of rows
$limit = 10000
##Find out the specific extension file
$Extension = "*.*"
##script to find out the files based on the above input
get-ChildItem -path $path -recurse -ErrorAction "SilentlyContinue" -include $Extension | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt $size} | Move-Item -Destination C:\misc

My directory structure (first level) enter image description here

Was it helpful?

Solution

A few suggestions.

  1. You need to pipe the results to Foreach-object so that you can move each file.
  2. You can use -whatif to test the move operation.
  3. I like to test by writing output to double check what I am doing
  4. The common way of check if an object is a file or dir is to use .PSIsContainer
  5. When using pipe you can go to the next line for readability.

    get-ChildItem $path -recurse -ErrorAction "SilentlyContinue" -include $Extension | 
        Where-Object { !($_.PSIsContainer) -and $_.Length -gt $size } | 
        ForEach-Object {
            Write-Output "$($_.fullname) $($_.Length / 1Mb)"
            Move-Item $_.fullname C:\misc -whatif
        }
    

OTHER TIPS

I am a huge proponent of doing everything I can with PowerShell. However, nothing beats robocopy on these types of operations! Here is the TechNet Article on the syntax for Robocopy. Pay close attention to the File Selection Options section where you are introduced to the /max: argument where you can specify the maximum file size. So in your instance you will want to indicate this: /max: 262144000 so that you get all files smaller than 250 MB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top