I am a forum lurker and have learned SO much from PowerShell.org about scripting. I’ve been able to cobble together some really useful scripts to help in my daily life, but I have run in to a roadblock and can’t seem to help myself out of it. I am NOT a programmer and really struggling to learn PowerShell to help me some routine but cumbersome tasks.
I put a script together to help me move a large number of movie and pic files (only png, jpg, gif and mpg formats) to subfolders within the same directory.
First Issue
I have to have two versions of the script to limit the move to 5000 files where total files => 5000. But can’t wrap my head around how to clean up the script to account for instances where there are <5000 files.
The change I now make manually is from this:
for ($i=0; $i -lt $files.Count; $i++) { $outfile = $files[$i].Name
To this for when there are >5000 files:
# Limit the move to the first 5000 items y changing "$files.count" to "5000". for ($i=0; $i -lt 5000; $i++) { $outfile = $files[$i].Name
What I would like is one script that (1) counts the files and then (2) moves them appropriately with multiple folders of 5000 items where total files >= 5000.
I want the ability to break up, say 22,000 files (of the same type) into five folders, To Sort 1, To Sort 2, etc. with To Sort 5 holding the last 2,000. What I am getting instead is just one folder without the numbering and subsequent folder creation (because I don’t know how to do that) and the script craps out with an exception error where total files < 5000.
Second Issue
I would like to run the script from current directly without having to copy it there manually. In other words, one script that runs in multiple directories (Jan, Feb Mar, or whatever). I have no idea how to grab the current directory and have the script execute only on those files. I was using the recurse function when running the script on multiple subdirectories, but that's not helpful given the above limitations.
If you could help (1) implement the folder creation and (2) suggest improvements, I would be greatly appreciative. I have really tried to solve this on my own, but I'm at a loss at this point.
$files = Get-ChildItem -File -Recurse
Here is my complete cobbled together script.
# Move downloads to folders to make sorting easier. ################ SET THE SCRIPT PROCESSOR PRIORITY ##################### (Get-Process -id $pid).PriorityClass = "High" ################# SORT THE FILES IN TO SUBDIRECTORIES ################### # Go through each file in the current working directory and run the script to move like files to a subdirectory. # # Uncomment "-Recurse" to run the routine on a batch of directories, not just the current one. $files = Get-ChildItem -File ##-Recurse # Limit the move to the first 5000 items y changing "$files.count" to "5000". for ($i=0; $i -lt $files.Count; $i++) { $outfile = $files[$i].Name # Move GIF's from the current directory to a new subdirectory called "GIF's to Sort" if ($outfile.EndsWith("gif")){ mkdir -Path "GIFs to Sort" -Force move $files[$i].FullName "GIFs to Sort" -Force } # Move MP4's from the current directory to a subdirectory called "Movies to Sort" if ($outfile.EndsWith("mp4")){ mkdir -Path "Movies to Sort" -Force move $files[$i].FullName "Movies to Sort" -Force } # Move PNG's from the current directory to a subdirectory called "Pics to Sort" if ($outfile.EndsWith("png")){ mkdir -Path "Pics to Sort" -Force move $files[$i].FullName "Pics to Sort" -Force } # Move JPG's from the current directory to a subdirectory called "Pics to Sort" if ($outfile.EndsWith("jpg")){ mkdir -Path "Pics to Sort" -Force move $files[$i].FullName "Pics to Sort" -Force } }