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

25,291

Solution 1

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.

Solution 2

You could do something like this:

$Directory = "f:\ION\2011291";
$AllFiles = Get-ChildItem $Directory | where {$_.extension -eq ".vpf"};
$FileNames = New-Object System.Collections.ArrayList;
foreach($File in $AllFiles) 
{
    $FileNames.Add($File.Name.SubString(0,9));  
}
$UniqueNames = $FileNames | get-unique;
foreach($Name in $UniqueNames)
{
    New-Item $Directory\$Name -type directory
}
foreach($File in $AllFiles)
{
    $Dir = $File.Name.SubString(0,9);        
    Move-Item $Directory\$File $Directory\$Dir\$File;
}

Explanation:

  1. First find all files in $Directory with the ".vdf" extension
  2. Add the first 9 letters from all file names to an $FileNames
  3. Store each unique name from $FileNamesinto $UniqueNames
  4. Create new directories the the names in $UniqueNames
  5. Move each file in to corresponding directory, based on first 9 characters

If the script will always be run in the directory where the files are stored, and you don't want to filter on extension, look to @zdans answer for a more compact and elegant solution.

Share:
25,291

Related videos on Youtube

JRG
Author by

JRG

Updated on September 18, 2022

Comments

  • JRG
    JRG over 1 year

    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.
  • Tamara Wijsman
    Tamara Wijsman over 12 years
    Can you provide some guidance?