none
Split filename and create folders with Powershell script (<--newbie user) RRS feed

  • Question

  • I have a folder with 1000's of files. Each file varies in length; typically between 9-14 characters. Example:

    • C:\workpics\1238955678.1
    • C:\workpics\744556224.1
    • C:\workpics\744556224.2
    • C:\workpics\8445655996.1

    I would like to run a script to split the filename and separate into folders. The filename would then consist of 5 characters, the foldername would be the first characters remaining.

    • C:\workpics\12389\55678.1
    • C:\workpics\7445\56224.1
    • C:\workpics\7445\56224.2
    • C:\workpics\84456\55996.1

    Any assistance to create this Powershell script would be awesome. Thank you!!

    Monday, July 7, 2014 10:14 PM

Answers

  • Hi dgates.  I've made a powershell script that does what you were looking for.  It has a simple explanation of each line but let me know if anything isn't clear. 

    Please give it a try and if you run into any issues just let me know.  Btw, your post made perfect sense.

    If everything works out, please mark my reply as the answer (It'll be my first solution given!).

    -Kevin

    $dir = "C:\workpics\"
    
    #Get all the objects in the directory specified above
    Get-ChildItem $dir |
    
    #Go through each object in that directory
    Foreach-Object{
        #If the object is a folder, skip it
        If ($_.PSIsContainer -eq $False)
        {
            #find out how long the file's name is
            $length = $_.BaseName.length
            # Create a variable containing the file name - 5 characters
            $first = $_.BaseName.SubString(0, $length - 5)
            # Create a variable containing the last 5 characters of the file
            $last = $_.BaseName.SubString($length - 5) # Check to see if we've made a folder of the first variable already, if not, make it If (!(Test-Path ($dir + $first))) {$newDir = New-Item -ItemType directory -Path ($dir + $first)} # Rename the file to contain the last variable as it's name Rename-Item ($dir + $_) ($dir + $last + $_.Extension) # Move the renamed item to the new directory Move-Item ($dir + $last + $_.Extension) ($dir + $first) } }



    • Edited by Kevin Denham Tuesday, July 8, 2014 2:05 AM Changed from splitting file name in half to leaving only 5 characters for file name.
    • Marked as answer by dgates12 Tuesday, July 8, 2014 3:54 PM
    Tuesday, July 8, 2014 12:24 AM

All replies

  • What have you tried so far and with what results?

    -- Bill Stewart [Bill_Stewart]

    Monday, July 7, 2014 10:29 PM
    Moderator
  • I have a folder with 1000's of files. Each file varies in length; typically between 9-14 characters. Example:

    • C:\workpics\1238955678.1
    • C:\workpics\744556224.1
    • C:\workpics\744556224.2
    • C:\workpics\8445655996.1

    I would like to run a script to split the filename and separate into folders. The filename would then consist of 5 characters, the foldername would be the first characters remaining.

    • C:\workpics\12389\55678.1
    • C:\workpics\7445\56224.1
    • C:\workpics\7445\56224.2
    • C:\workpics\84456\55996.1

    Any assistance to create this Powershell script would be awesome. Thank you!!

    Please reread your request.  It makes no sense.  Read each statement carefully and notice that the statements are in conflict.

    Start your scrip in PowerShell.  Look up things like how to list files and how to manage strings.  All of the information you need is here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Monday, July 7, 2014 10:33 PM
  • Being a beginner, my skills are minimal.  I've figured out how to rename a file for example, but I'd like to take it a step further and create these various "subfolders" and move the file into this new folder at the same time.
    Monday, July 7, 2014 10:39 PM
  • Made sense to me :)

    Thanks for the hyperlink.

    Monday, July 7, 2014 10:42 PM
  • Hi dgates.  I've made a powershell script that does what you were looking for.  It has a simple explanation of each line but let me know if anything isn't clear. 

    Please give it a try and if you run into any issues just let me know.  Btw, your post made perfect sense.

    If everything works out, please mark my reply as the answer (It'll be my first solution given!).

    -Kevin

    $dir = "C:\workpics\"
    
    #Get all the objects in the directory specified above
    Get-ChildItem $dir |
    
    #Go through each object in that directory
    Foreach-Object{
        #If the object is a folder, skip it
        If ($_.PSIsContainer -eq $False)
        {
            #find out how long the file's name is
            $length = $_.BaseName.length
            # Create a variable containing the file name - 5 characters
            $first = $_.BaseName.SubString(0, $length - 5)
            # Create a variable containing the last 5 characters of the file
            $last = $_.BaseName.SubString($length - 5) # Check to see if we've made a folder of the first variable already, if not, make it If (!(Test-Path ($dir + $first))) {$newDir = New-Item -ItemType directory -Path ($dir + $first)} # Rename the file to contain the last variable as it's name Rename-Item ($dir + $_) ($dir + $last + $_.Extension) # Move the renamed item to the new directory Move-Item ($dir + $last + $_.Extension) ($dir + $first) } }



    • Edited by Kevin Denham Tuesday, July 8, 2014 2:05 AM Changed from splitting file name in half to leaving only 5 characters for file name.
    • Marked as answer by dgates12 Tuesday, July 8, 2014 3:54 PM
    Tuesday, July 8, 2014 12:24 AM
  • Why not use real PowerShell.  If you are going to teach the OP to write PowerShell scripts then teach the correct techniques.

    $dir="c:\workpics\"
    Get-ChildItem $dir -File |
        Foreach-Object{
            $foldername=$_.BaseName.SubString(0,$_.Basename.length - 5)
            $filename=$_.Basename($_.Basename.length - 5)
            $targetdir="$dir$foldername"
            If(!(Test-Path $targetdir)){New-Item $targetdir -ItemType directory}
            Move-Item $_ "$targetedir\$filename.$($_.Extension)"
        }
    }

    Note that half of your code is redundant.

    Of course this and yours need to be debugged on user system.  The "what if" is what happens if the file lengths are not as stated.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, July 8, 2014 1:13 AM
    Tuesday, July 8, 2014 12:57 AM
  • I think you've got a typo on the $Foldername line (. after the 0 s/b a ,).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Tuesday, July 8, 2014 1:07 AM
    Moderator
  • I think you've got a typo on the $Foldername line (. after the 0 s/b a ,).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Like I said.  It needs to be debugged.

    (I constantly hit the wrong key for comma.  I hate laptop keyboards. Too small.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, July 8, 2014 1:13 AM
    Tuesday, July 8, 2014 1:12 AM
  • I've done it, too.  Just tryin' to not to confuse the newbies any more than they already are :).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, July 8, 2014 1:17 AM
    Moderator
  • I've done it, too.  Just tryin' to not to confuse the newbies any more than they already are :).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Yup.  That is why I proffered the learning link.  Knowledge is the best antidote for confusion.

    Kevin's  little tutorial was actually pretty good - I just wanted to tease him a bit.  As they always say -- no good dead should ever go unpunished.

    ;)


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, July 8, 2014 1:24 AM
    Tuesday, July 8, 2014 1:24 AM
  • Sorry Kevin but this is not scripts-on-demand.  It is made clear in the forum guidelines. THe point of the forum is also to encourage people to learn scripting. Giving a list of requirements is not asking a scripting question.


    ¯\_(ツ)_/¯

    Tuesday, July 8, 2014 1:50 AM
  • Sorry for all the drama here, but thank you for the assistance.  I'm definitely going to study this code and learn from it!
    Tuesday, July 8, 2014 3:56 PM
  • Sorry for all the drama here, but thank you for the assistance.  I'm definitely going to study this code and learn from it!

    Learning is more important than answers.  Once you see how PowerShell is put together all manner of things become much easier.


    ¯\_(ツ)_/¯

    Tuesday, July 8, 2014 4:27 PM