I’ve got a folder that gets filled with a bunch of log files that need to be moved into sub-folders every so often. For example, I need to get the following files into the directories at the arrow.
SOME_FILE_341213.txt –> SMPROD
SOME_FILE_341242.txt –> SMPROD
OTHER_FILE_13423.log –> SSBRPRD
ALTER_FILE_13423.log –> SSBRPRD
geofile12321 –> REGIONPROD
I’ve seen lots of solutions that will parse out part of a file name and move it into a directory containing that parse of the file name. In my case, the destination directories will not really match up to a parsed part of the file names. I was thinking I could use a switch statement to match the first 4 or 5 letters to cases that would move files into the appropriate directories but I’m not sure that’s the most efficient way to go about it. I would have about 25 cases to match to. For files that didn’t match any case I would leave them where they are. Any advice?
I would go with the switch statement in a for-each loop. Something like this:
On the
substring(x,y)command, the overload is:Obviously replace the
write-hostwith what you actually want to do. The switch statement can span multiple lines. Don’t forget thebreakat the end, so you don’t go through all 25 options for every file.