0

I have a directory of information that is separated into document numbers so each folder that contains documents starts with DOC-######-NameOfDocument. The thing I am trying to do is create a PowerShell script that will search a directory for any folders with a specified document number and then take the contents of that folder, move it up one level, and then delete the original folder (which should now be empty).

Below is the closest I have gotten to my intended result.

$Path = "filepath"
$Folders = Get-ChildItem -Filter "DOC-#####*" -Recurse -Name -Path $Path
$companyID = "######"


    foreach ($Folder in $Folders){
        $filepath = $Path + $Folder
        $Files = Get-ChildItem -Path $filepath

        $imagesourc = $filepath + $companyID
        $imageDest = $filepath.Substring(0, $filepath.LastIndexOf('\'))

            if (Test-Path -Path $imagesourc){
                Copy-Item -Path $imagesourc -Destination $imageDest -Recurse
            }
        

        foreach ($File in $Files){
        
            $Parent_Directory = Split-Path -Path $File.FullName
            $Destination_Path = $filepath.Substring(0, $filepath.LastIndexOf('\'))

            Copy-Item -Path $File.FullName -Destination $Destination_Path -Recurse
               if ($null -eq (Get-ChildItem -Path $Parent_Directory)) {  
            }
        }
        Remove-Item $filepath -Recurse
    }

This does what I need but for whatever reason I can't Devine, it will not work on .HTM files. Most of the files I am moving are .html and .htm files so I need to get it to work with .htm as well. The files with .HTM will not move and the folder won't be deleted either which is good at least.

Patrick
  • 1
  • 1
  • 1
    Does this answer your question? https://stackoverflow.com/questions/51346965/move-files-up-one-folder-level – Santiago Squarzon Jun 29 '21 at 22:11
  • The code from this question is where I got started and I have I added the following code to line 3. | Move-Item -Destination { $_.Directory.Parent.FullName } I now have it to where it will work if there are only files in the folder it is looking at but if there are subfolders they won't move and it will crash and close the powershell window. – Patrick Jun 30 '21 at 13:23
  • With some additional testing the error that is crashing is that the destination is NULL. With the same code it works if there is not a subfolder in the target folder that is found however. – Patrick Jun 30 '21 at 14:25
  • I have made adjustments to the code based on further research and testing. Now working mostly but unable to move .htm files. – Patrick Jun 30 '21 at 18:52

1 Answers1

0

Try using this:

$ErrorActionPreference = 'Stop'
$fileNumber = '1234'
$initialFolder = 'X:\path\to\folders'

$folders = Get-ChildItem -Path $initialFolder -Filter DOC-$fileNumber* -Force -Directory -Recurse

foreach($folder in $folders)
{
    try
    {
        Move-Item $folder\* -Destination $folder.Parent.FullName
        Remove-Item $folder
    }
    catch [System.IO.IOException]
    {
        @(
            "$_".Trim()
            "File FullName: {0}" -f $_.TargetObject
            "Destination Folder: {0}" -f $folder.Parent.FullName
        ) | Out-String | Write-Warning
    }
    catch
    {
        Write-Warning $_
    }
}

Important Notes:

  • Move-Item $folder\* will move all folder contents recursively. If there are folders inside $folder, those will also be moved too, if you want to target folders which only have files inside, an if condition should be added before this cmdlet.
  • Try {...} Catch {...} is there to handle file collision mainly, if a file with a same name already exists in the parent folder, it will let you know and it will not be moved nor will the folder be deleted.
  • -Filter DOC-$fileNumber* will capture all the folders named with the numbers in $fileNumber however, be careful because it may capture folders which you may not intent to remove.
    • Example: If you want to get all folders containing the number 1234 (DOC-12345-NameOfDocument, DOC-12346-NameOfDocument, ...) but you don't want to capture DOC-12347-NameOfDocument then you should fine tune the filter. Or you could add the -Exclude parameter.
  • -Force & -Directory to get hidden folders and to target only folders.
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • So the above script worked on all but 1 folder in the directory I ran it in. The folder that didn't work seems to have failed because a subfolder had the same name as a subfolder of a folder that had already processed. It had the error that that sub-folder already existed and failed. It didn't continue past that for that folder either. – Patrick Jul 01 '21 at 15:45
  • @Patrick well yes, that is expected which is why I added the `try {...} catch {...}` statement, right now it is so it will stop processing the folder if file / folder collision is there, you'll need to add an if statement to handle / decide what to do when there is file / folder collision. – Santiago Squarzon Jul 01 '21 at 18:03