locked
Flattening a file structure and truncating file names RRS feed

  • Question

  • I am new to PowerShell and was hoping for some assistance... the following script works well to flatten folders for a project I am working on but I have not found a way to truncate the file name when it is moved to the parent folder. I am sure it is an easy fix but I am struggling.

    For instance I need this:

    CONTRACT 1

      SUBFOLDER A

          FILE NAME 20 CHARACTERS

      SUBFOLDER B

         FILE NAME 25 CHARACTERS

    to become this:

    CONTRACT 1

      FILE NAME ONLY 10 CHARACTERS

      FILE NAME ONLY 10 CHARACTERS

    This is what I have so far (does exactly what I need it to in order to flatten the folders, but not sure how to truncate the moved files to 10 characters)

    $Rootfolder = Dir X:\

    ForEach($folder in $Rootfolder)

    {

        Dir $folder.fullname -Recurse -File | Copy-Item -Destination $folder.fullname

        Dir $folder.fullname -Recurse -Directory | Remove-Item -Force -Recurse

    }

    • Moved by Bill_Stewart Wednesday, May 9, 2018 2:36 PM User should proceed with great caution - moving to prevent others from seeing and trying this
    Tuesday, March 13, 2018 6:59 PM

All replies

  • This is what you are trying to do. It will cause errors if duplicate names exist.

    Get-ChildItem x:\ -Recurse -Directory | 
        Get-ChildItem -file |
        Move-Item -Destination 'your destination folder name' -WhatIf
    


    \_(ツ)_/

    Tuesday, March 13, 2018 7:16 PM
  • Thank you for the quick reply. I may not have been clear with my question. To clarify, how do I truncate the name of the files in the new destination during the following process?

    $Rootfolder = Dir X:\

    ForEach($folder in $Rootfolder)

    {

        Dir $folder.fullname -Recurse -File | Copy-Item -Destination $folder.fullname

        Dir $folder.fullname -Recurse -Directory | Remove-Item -Force -Recurse

    }

    Tuesday, March 13, 2018 8:02 PM
  • What do you mean by "truncate"?  The file name does not contain the folder name.  "FullName" contains the folder name.  My code  does not require altering the file name. Windows manages that correctly.  Using Move-Item safely accomplishes this and protects you from deleting files that have failed to copy.


    \_(ツ)_/


    • Edited by jrv Tuesday, March 13, 2018 8:07 PM
    Tuesday, March 13, 2018 8:06 PM
  • Thanks for the reply... I am needing to shorten the file name when it is moved...basically, I need something like this...

           Folder/Subfolder/Subfolder/Somewhere_over_the_Rainbow_way_up_high.doc

    to become:

           Folder/Somewhere_Over.doc

    This is not limited to doc files, just an example.

    And I am doing it for multiple file folders in a SharePoint library so the script I mentioned above works well to knock out all folders at once (we have thousands), but I need to find a way to shorten the file names during this process.

    Friday, March 16, 2018 3:25 PM
  • To further clarify, we need to keep the naming convention of the parent folder if we move the files... so something like this...

        Folder1/Subfolder/Subfolder/Somewhere_over_the_Rainbow_way_up_high.doc

        Folder2/Subfolder/Subfolder/Another_Long_File_Name.doc

        Folder3/Subfolder/Subfolder/A_third_Long_File_Name.doc

    to become:

        Folder1/Somewhere_Over.doc

        Folder2/Another_Long_Fil.doc

        Folder3/A_third_long_File.doc

    Thanks!

    Friday, March 16, 2018 3:30 PM
  • To shorten just the name use the "Basename" property and construct a custom file name.

    $basename = $file.BaseName.SubString(0,10)

    This will return the first 10 characters of the base name of the file.


    \_(ツ)_/

    Friday, March 16, 2018 6:50 PM