Windows – Recursively move thousands of files into subfolders windows

scriptingwindows

I have a Windows Server with >300,000 images in a single folder.

I am feeling a performance hit on the filesystem and I would like to move them into subfolders as has been suggested by many others.

Most of my files are 10 characters + .jpg – beginning with a 1 (eg: 1234567890.jpg)

I would like to recursively create subfolders based on file name (groups of 3 characters) – and move the files into them. So 1234567890.jpg would be moved from c:\images into c:\Images\1\234\567\890.

How can I do this with a batch file or wscript?

Best Answer

PowerShell is really a good tool for this. I'm not going to write a script for you, but I will point you in the right direction.

You can list everything in the directory using get-childitem and them pass it to a foreach loop. Then, use .substring(0,2) to get the first three letters. Save those three to a variable and create the folder using new-item -Name <foldername> -ItemType folder replacing with the variable holding the first three numbers. It would be a good idea to test for the existence of this folder and only do it if it doesn't exist. Lather rinse repeat adding some logic that your unique situation will likely dictate.

Then, you can just use move-item to move the files into the folders based on pattern matching that looks at the first three letters of the file.


I know you wanted someone to write a script for you, but give a man a fish, teach a man to fish...

Related Topic