ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Post New Requests Here

Move/Organize Files Into Folders

<< < (3/3)

AbteriX:
Skwire, unless you or someone else wants to add anything, this one looks solved.-nkormanik (March 29, 2016, 12:53 AM)
--- End quote ---

Anyway  :D, here a script, done with PowerShell, just for fun and because PoSh is there already.

(Tested with PoSh v4, but forced to behave like v2. Write $PSVersionTable to get your version)



I have 600,000 midi files, in a single folder. 
I'd like to move/organize into folders.  100 midi files per new folder.  Sorted by size of files.
So, the smallest 100 midi files will go into, say, folder 0001.
Second 100 smallest midi files will go into folder 0002.
Etc.
Will need to create all the required folders.
-nkormanik (March 13, 2016, 07:58 PM)
--- End quote ---


With PowerShell:


Open a PowerShell console in your "single folder" with the  "600,000 midi files".


---

If wanted, Create some test files (with random names and different size) to test my script:

--- Code: PowerShell ---1..200|%{ fsutil file createnew ([System.IO.Path]::GetRandomFileName()) $_ | Out-Null}
---

Move every ten files (sorted by size) to a new created sub folder:
(Change "-lt 10" to the wanted amount, like "-lt 100")


--- Code: PowerShell ---ls|?{!$_.PSIsContainer}|sort length|%{$C=0;$I=1}{$C++;if($C-lt10){mkdir $I -ea 0|Out-Null;Move $_ $I}else{Move $_ $I;$C=0;$I++}}

---

The same Move-script, but as long version, with official command names and such:

--- Code: PowerShell ---# DIR folder:Get-ChildItem | # Files only, no directories:Where-Object{ -not $_.PSIsContainer} |# Sort by file size:Sort-Object -Property length |# For Each File from DIR Do:ForEach-Object     # One time, common for all files, at the beginning, set counter:    -Begin{ $FileCount=0; $LotFolder=1 }     # For each individual file:    -Process{ $FileCount++;             # break/reset at every max count, here 10:            if($FileCount -lt 10){                # Create sub folder, suppress error message on existing folder (ErrorAction), suppress success message too (>NUL):                New-Item -path $LotFolder -ItemType directory -ErrorAction SilentlyContinue|Out-Null;                # Move current file to sub folder:                Move $_ $LotFolder}            else{                # On last file in set, Move current file to sub folder and adjust counter:                Move $_ $LotFolder; $FileCount=0; $LotFolder++}            }     # One time, common for all files, at the very end:    -End {"Done!"}




.

nkormanik:
AbteriX, I had no idea PowerShell could be so powerful.  Thanks for a masterful demonstration!  And another solution.

nkormanik:
Here, by the way, is the C# code someone else did to solve the issue.  Author gave permission to post.:


--- Code: C# ---using System;using System.IO;using System.Linq; namespace FileOrganizer{    class Program    {        static void Main(string[] args)        {            // Options            var filePattern = "*.url";            var filesPerFolder = 10;                 var currentDir = new DirectoryInfo(Environment.CurrentDirectory);             var files = currentDir.GetFiles(filePattern, SearchOption.AllDirectories);             var sortedFiles = files.OrderBy(x => x.Length).ToList();             for (var i=0; i < sortedFiles.Count; i++)            {                var sourceFilePath = sortedFiles[i].FullName;                var targetFolderName = (i / filesPerFolder).ToString("X8");                var targetFolderPath = Path.Combine(Environment.CurrentDirectory, targetFolderName);                var targetFilePath = Path.Combine(targetFolderPath, sortedFiles[i].Name);                 if (i % filesPerFolder == 0)                {                    Directory.CreateDirectory(targetFolderPath);                }                 File.Move(sourceFilePath, targetFilePath);            }        }    }}

Navigation

[0] Message Index

[*] Previous page

Go to full version