Powershell: Get inital character string from file name and create directory from string, then move files

parsingpowershellpowershell-2.0script

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:

  1. I need to do this in Powershell
  2. 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:

dir | %{ 
    $id = $_.Name.SubString(0,8); 
    if(-not (Test-Path $id)) {mkdir $id}; 
    mv $_ "$id\$_";}

Explanation:

foreach file in the directory (% is an alias for foreach):

  • Get the id from the first 9 characters. Note that the $_ variable is an automatic variable populated by powershell that represents the current file. The Name property of the object returns a .NET String object which has a SubString member function that you can use to return the portion of the filename you are interested in.
  • Check if the "id" directory already exists. If it doesn't, create it (mkdir is an alias for New-Item).
  • Then move the file into the directory (mv is an alias for Move-Item). Note that when powershell sees a variable inside a double quoted string, it automatically expands its value into the resulting string.

Note that the example I provide assumes that you are running it from the directory where your files are.

Related Question