Archive Directory Slow to Open – Powershell Moves Files to Dated Subdirectories

With BizTalk, we often archive files in a sub directory. Personally, I would rather archive to a SQL database with a database column, but that takes a little more architecture and sales. So in the meantime, many clients continue to write files to disk. There are frequently clean-up jobs that delete files over x days old.

However, when there are multiple thousands of a files in any directory, it can take a long time to open that directory, and display the files, especially when you are accessing it from a remote computer.

The $DaysBack parameter can be set to non-zero if you want to only group files over x days old into sub-directories.

{!{code}!}czo4NDE6XCINCiNNb3ZlIGZpbGVzIChmb3IgZXhhbXBsZSBCaXpUYWxrIGFyY2hpdmUgWE1MIGZpbGVzKSANCiNpbnRvIHN1YmZvbGR7WyYqJl19ZXJzIGJhc2VkIG9uIGRhdGUgDQpjbHMNCg0KJFNvdXJjZURpciA9IFwiQzpcXFRlc3RGaWxlc1xcXCINCiREZXN0aW5hdGlvbkRpciA9IFwie1smKiZdfUM6XFxUZXN0RmlsZXNcXFwiDQokRGF5c0JhY2sgPSAwIA0KDQokZmlsZXMgPSBnZXQtY2hpbGRpdGVtICRTb3VyY2VEaXIgKi4qIA0KV3J7WyYqJl19aXRlLUhvc3QgXCJGaWxlIENPdW50PSAkKCRmaWxlcy5Db3VudClcIg0KDQpmb3JlYWNoICgkZmlsZSBpbiAkZmlsZXMpIA0Kew0KICAge1smKiZdfSAkTmV3U3ViRGlyZWN0b3J5ID0gJERlc3RpbmF0aW9uRGlyICsgXCJcIiArICRmaWxlLkxhc3RXcml0ZVRpbWUuRGF0ZS5Ub1N0cmluZ3tbJiomXX0oXCd5eXl5LU1NLWRkXCcpDQoNCiAgICAjQ3JlYXRlICROZXdTdWJEaXJlY3RvcnkgaWYgaXQgZG9lc25cJ3QgYWxyZWFkeSBleGlzdCANe1smKiZdfQogICAgaWYgKCEoVGVzdC1QYXRoICROZXdTdWJEaXJlY3RvcnkpKQ0KCSAgICB7DQoJICAgIE5ldy1JdGVtICROZXdTdWJEaXJlY3R7WyYqJl19b3J5IC10eXBlIGRpcmVjdG9yeQ0KCSAgICB9DQoNCiAgICBpZiAoJERheXNCYWNrIC1ndCAwKQ0KICAgICAgIHsNCiAgICAgICBJZntbJiomXX0oJGZpbGUuTGFzdFdyaXRlVGltZSAtbHQgKEdldC1EYXRlKS5hZGRkYXlzKC0xICogJERheXNCYWNrKS5kYXRlKQ0KICAgICAgICAge1smKiZdfSB7DQoJICAgICAgIE1vdmUtSXRlbSAkZmlsZS5mdWxsbmFtZSAkTmV3U3ViRGlyZWN0b3J5DQogICAgICAgICAgIH0NCiAgICAgICB7WyYqJl19fQ0KICAgIGVsc2UgDQogICAgICAgew0KICAgIAkgICBNb3ZlLUl0ZW0gJGZpbGUuZnVsbG5hbWUgJE5ld1N1YkRpcmVjdG9yeQ0KIHtbJiomXX0gICAgICB9DQp9DQpcIjt7WyYqJl19{!{/code}!}

 

You will need full write/rename access to run this script. You can specify a UNC name in the $SourceDir and $DestinationDir variables.

Code above based on code sample found here http://www.marcvalk.net/2012/06/powershell-moving-files-into-subfolder-based-on-date/

Before – could be thousands of files

Powershell_MoveFilesSubDir_Before

After – Files in neat subdirectories by date

Powershell_MoveFilesSubDir_After

Uncategorized  

Leave a Reply