Renaming directories within ForEach loop

Welcome Forums General PowerShell Q&A Renaming directories within ForEach loop

This topic contains 1 reply, has 1 voice, and was last updated by

 
Participant
6 months, 4 weeks ago.

  • Author
    Posts
  • #99213

    Participant
    Points: 0
    Rank: Member

    Hi All,

    I have been working on a script to move files based on two parts of their filename. For example I have a directory with 4 folders

    M100
    M105
    M110
    M115

    within each folder contains 1-4 files that use the following naming convention

    M100_Installation Report_yyyymmdd
    M100_Service Report_yyyymmdd
    M100_Other Documents_yyyymmdd
    M100_Specifications_yyyymmdd

    The script below will create a "M100" directory in the output folder if it does not exist along with an "Install Report" sub folder (and so on for each document type). It will then also move the files into the matching directories.

    It is all working. However, I need the sub folder names to be the following

    Install Report = Install Reports
    Service Report = Service Reports
    Specifications = Specifications
    Other Documents = Other Doc

    I tried adding an if statement to update a variable for each record but could not get it working (its currently commented out)

    Any help on this is appreciated!

    $dir = "C:\Test Indexed Files\docs to be reviewed"
    $output = "C:\Test Indexed Files\output"
    Set-Location $dir
    
    $filelist = @(Get-ChildItem -include *.pdf -Recurse -path $dir)
    
    ForEach ($file in $filelist){
        # splits the file
        $folder1 = $file.Name.Split("_")[0]
        $folder2 = $file.Name.Split("_")[1] 
        $folder3 = $file.Name.Split("_")[2]
    
        #check if folder exists
        Set-Location ($output+'\'+$folder1+'\'+$folder2)
    
        #create folder if doe snot exist
        if(!$?) {mkdir ($output+'\'+$folder1+'\'+$folder2)}
    
        #Move files to sub folders
        Move-Item $file.FullName ($output+'\'+$folder1+'\'+$folder2+'\'+$file.Name)
      }
    
  • #99216

    Participant
    Points: 0
    Rank: Member

    I found the issue with my code and was able to get it working! turns out I had the if/elseif syntax incorrect. Here is my end result.

    $dir = "C:\Test Indexed Files\docs to be reviewed"
    $output = "C:\Test Indexed Files\output"
    Set-Location $dir
    
    $filelist = @(Get-ChildItem -include *.pdf -Recurse -path $dir)
    
    ForEach ($file in $filelist){
        # splits the file
        $folder1 = $file.Name.Split("_")[0]
        $folder2 = $file.Name.Split("_")[1] 
        $folder3 = $file.Name.Split("_")[2]
    
    If ($folder2 -eq "Installation Report") {
        $folder22 = "Install Reports"
    }   
    ElseIf ($folder2 -eq "Service Report") {
            $folder22 = "Service Reports"
    }
    ElseIf ($folder2 -eq "Specifications") {
            $folder22 = "Specifications"
    }
    ElseIf ($folder2 -eq "Other Documents") {
            $folder22 = "Other Doc"
    }
    Else {
    'Error'
    }
    
        #check if folder exists
        Set-Location ($output+'\'+$folder1+'\'+$folder22)
    
        #create folder if doe snot exist
        if(!$?) {mkdir ($output+'\'+$folder1+'\'+$folder22)}
    
        #Move files to sub folders
        Move-Item $file.FullName ($output+'\'+$folder1+'\'+$folder22+'\'+$file.Name)
        
      }
    

The topic ‘Renaming directories within ForEach loop’ is closed to new replies.