I have a folder with the following file names:
00150005D201110172338427995.vpf
00150005D201110180005318058.vpf
00150013D201110180014448082.vpf
00150013D201110180022268098.vpf
00150013D201110180056118137.vpf
00150004D201110180102008142.vpf
00150004D201110180105398145.vpf
00150016D201110180115378151.vpf
00150016D201110180122168161.vpf
00150003Z201110180143308169.vpf
00150050S201110180232190009.vpf
Each file begins with a 9 character string that is a unique identifier. I would like to be able to pars trhouhg these folders for each file and based on the 9 character prefix, create a folder with the prefix name, then move the files to the newly created folder.
Example:
Before:
f:\ION\2011291 Contains the following files
00150005D201110172338427995.vpf
00150005D201110180005318058.vpf
00150013D201110180014448082.vpf
00150013D201110180022268098.vpf
00150013D201110180056118137.vpf
00150004D201110180102008142.vpf
00150004D201110180105398145.vpf
00150016D201110180115378151.vpf
00150016D201110180122168161.vpf
00150003Z201110180143308169.vpf
00150050S201110180232190009.vpf
After:
F:\ION\2011291 contins only folders no files
F:\ION\2011291\00150005D contains
00150005D201110172338427995.vpf
00150005D201110180005318058.vpf
F:\ION\2011291\00150013D\ contains
00150013D201110180014448082.vpf
00150013D201110180022268098.vpf
00150013D201110180056118137.vpf
F:\ION\2011291\00150004D \contains
00150004D201110180102008142.vpf
00150004D201110180105398145.vpf
F:\ION\2011291\00150016D\ contains
00150016D201110180115378151.vpf
00150016D201110180122168161.vpf
F:\ION\2011291\00150003Z\ contains
00150003Z201110180143308169.vpf
F:\ION\2011291\00150050S
00150050S201110180232190009.vpf
Parameters:
- I need to do this in Powershell
- I would greatly appreciate direction on how this schould be accomplished, where I can read further or even some guidance on the actual scripting.
Best Answer
This script should do the job:
Explanation:
foreach file in the directory (% is an alias for foreach):
Note that the example I provide assumes that you are running it from the directory where your files are.