topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 6:57 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Move/Organize Files Into Folders  (Read 10094 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Move/Organize Files Into Folders
« on: March 13, 2016, 07:58 PM »
I seem to recall a program for what I'm asking.  If you know of one, please mention it.  Any and all thoughts appreciated.

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.

Assume same drive for all actions.

Prefer to leave original filenames, though adding a prefix (suffix?) would be okay.  That's actually how I'll handle this little chore if I have to on my own.

Solution should be able to handle any sort of file, so others out there can find this topic and use solution for their own interests.

Thanks much!

Nicholas Kormanik


MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #1 on: March 17, 2016, 07:07 AM »
I would take a look at this free file manager, MultiCommander.

It has a built in scripting mechanism.  So I am sure it could do things like move files to folder by age/size as you require.  I have not used it other than to look around it and the documentation a bit since I have so much stuff that launches Explorer windows and moves them.  But it seems quite well done.  It has both native 32 and 64 bit builds.

Please do not ask me how to use the scripting.  I have been trying to figure that out in the documentation.  But it has online forums for guidance.  It is just that I haven't spent much time with it, being sort of locked into Explorer.


skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #2 on: March 17, 2016, 07:52 AM »
I can do this one a little later today.  Stay tuned.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #3 on: March 21, 2016, 05:09 PM »
Thanks to both of you!  I'll look into the program mentioned.  Always good to have another tool handy.  Skwire, if you do have the chance and inclination, go for it.

A friend of mind wrote some C-sharp code that does the job, too, more or less as requested.  But one must edit the code a tiny bit, and then compile.  Not sure if I should somehow post his code, or what.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #4 on: March 21, 2016, 06:39 PM »
Thanks to both of you!  I'll look into the program mentioned.  Always good to have another tool handy.  Skwire, if you do have the chance and inclination, go for it.

A friend of mind wrote some C-sharp code that does the job, too, more or less as requested.  But one must edit the code a tiny bit, and then compile.  Not sure if I should somehow post his code, or what.


Unless your friend posted the code somewhere publicly I would check with him/her before publishing it yourself.  If there are directories "hard wired" into the source code perhaps you can request the author get the directory paths from an .ini file.  That would remove the necessity to recompile.  I believe C# has Properties so it should be pretty easy to set default directories and other settings and make a small input box so that the user can change them while the program is running, and save for next time or whatnot.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #5 on: March 24, 2016, 10:15 AM »
Apologies, Nick, I got completely sidetracked.  Did you still need this?

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #6 on: March 24, 2016, 05:03 PM »
Skwire, if you want to make it.  Surprisingly dividing a sorted collection into sets, say, by size, is lacking presently.  Go figure.  I'd think there would be a desire to have such functionality.

There IS the ability to separate based on other criteria, such as extension.  I think you yourself made such a program already.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #7 on: March 25, 2016, 04:18 PM »
I notice MultiCommander has updated the online documentation.  There are step by step instructions for creating user defined commands etc..  One of these days I have to play with it in depth as it is supposed to have 64 bit builds with true multi-threading.

http://multicommander.com/docs

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #8 on: March 26, 2016, 09:48 AM »
I don't know if you are familiar with AutoHotkey, a useful script language, you can find it here http://autohotkey.com/

This script should work (be sure to make a backup and be careful which folders you use)

The DIR command (MS Dos) has a useful SORT by size option so we make use of that to create a list of files sorted by size first, then read the list and parse it and create the folders when we need them to move the files into it. Again use at your own risk and be careful which folders you try it out on. You can compile this script into a exe and also have it read some settings from an INI file if that is useful by using the AutoHotkey IniRead command. You can also dress it up with a GUI and progress bars and what not, this is just a bare bones script.

Code: Autohotkey [Select]
  1. SetWorkingDir, %A_ScriptDir%
  2.  
  3. Counter:=0
  4. FilesPerFolder:=100
  5. Folder=c:\test\ ; set folder here or uncomment the lines below by removing the ; so you can select a folder
  6.  
  7. ;FileSelectFolder, Folder, , 0, Select folder
  8. ;If (Folder = "")
  9. ;       ExitApp
  10.  
  11. Folder:=RegExReplace(Folder, "\\$")
  12.  
  13. ; make a list sorted by size, type DIR /? at a command prompt for more options.
  14. RunWait, %ComSpec% /c dir %Folder% /b /OS > $$filelistsortedbysize.tmp, , Hide ; use /O-S if you want to sort from large to small
  15. FileRead, FileList, $$filelistsortedbysize.tmp
  16. Sleep 100
  17.  
  18. TargetFolder:=SubStr("00000" ++Counter, -4)
  19. FileCreateDir, %Folder%\%TargetFolder%
  20.  
  21. Loop, Parse, FileList, `n, `r
  22.         {
  23.          ; MsgBox %Folder%\%A_LoopField%`n%Folder%\%TargetFolder% ; debug msgbox so we can see what is going on
  24.          FileMove, %Folder%\%A_LoopField%, %Folder%\%TargetFolder%
  25.          If (Mod(A_Index,FilesPerFolder) = 0)
  26.                 {
  27.                  TargetFolder:=SubStr("00000" ++Counter, -4)
  28.                  FileCreateDir, %Folder%\%TargetFolder%
  29.                 }
  30.         }
  31.  
  32. FileDelete, $$filelistsortedbysize.tmp
  33.  
  34. MsgBox Done!
  35.  
  36. ; Esc::ExitApp ; if you want to be able to stop the script at any time by pressing Esc remove the ;

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #9 on: March 29, 2016, 12:53 AM »
Thanks Lintalist, a lot of work you put in there.  Seems to do the job as advertised.

Skwire, unless you or someone else wants to add anything, this one looks solved.

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
PowerShell: Move/Organize Files Into Folders
« Reply #10 on: March 29, 2016, 04:15 AM »
Skwire, unless you or someone else wants to add anything, this one looks solved.

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.


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 [Select]
  1. 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 [Select]
  1. 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 [Select]
  1. # DIR folder:
  2. Get-ChildItem |
  3. # Files only, no directories:
  4. Where-Object{ -not $_.PSIsContainer} |
  5. # Sort by file size:
  6. Sort-Object -Property length |
  7. # For Each File from DIR Do:
  8. ForEach-Object
  9.     # One time, common for all files, at the beginning, set counter:
  10.     -Begin{ $FileCount=0; $LotFolder=1 }
  11.     # For each individual file:
  12.     -Process{ $FileCount++;
  13.             # break/reset at every max count, here 10:
  14.             if($FileCount -lt 10){
  15.                 # Create sub folder, suppress error message on existing folder (ErrorAction), suppress success message too (>NUL):
  16.                 New-Item -path $LotFolder -ItemType directory -ErrorAction SilentlyContinue|Out-Null;
  17.                 # Move current file to sub folder:
  18.                 Move $_ $LotFolder}
  19.             else{
  20.                 # On last file in set, Move current file to sub folder and adjust counter:
  21.                 Move $_ $LotFolder; $FileCount=0; $LotFolder++}
  22.             }
  23.     # One time, common for all files, at the very end:
  24.     -End {"Done!"}





.
« Last Edit: March 29, 2016, 04:24 AM by AbteriX »

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #11 on: March 29, 2016, 09:13 PM »
AbteriX, I had no idea PowerShell could be so powerful.  Thanks for a masterful demonstration!  And another solution.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Move/Organize Files Into Folders
« Reply #12 on: June 01, 2016, 07:49 PM »
Here, by the way, is the C# code someone else did to solve the issue.  Author gave permission to post.:

Code: C# [Select]
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace FileOrganizer
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // Options
  12.             var filePattern = "*.url";
  13.             var filesPerFolder = 10;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.             var currentDir = new DirectoryInfo(Environment.CurrentDirectory);
  20.  
  21.             var files = currentDir.GetFiles(filePattern, SearchOption.AllDirectories);
  22.  
  23.             var sortedFiles = files.OrderBy(x => x.Length).ToList();
  24.  
  25.             for (var i=0; i < sortedFiles.Count; i++)
  26.             {
  27.                 var sourceFilePath = sortedFiles[i].FullName;
  28.                 var targetFolderName = (i / filesPerFolder).ToString("X8");
  29.                 var targetFolderPath = Path.Combine(Environment.CurrentDirectory, targetFolderName);
  30.                 var targetFilePath = Path.Combine(targetFolderPath, sortedFiles[i].Name);
  31.  
  32.                 if (i % filesPerFolder == 0)
  33.                 {
  34.                     Directory.CreateDirectory(targetFolderPath);
  35.                 }
  36.  
  37.                 File.Move(sourceFilePath, targetFilePath);
  38.             }
  39.         }
  40.     }
  41. }