Search code examples
powershellsubdirectoryorganization

How to fix a working script to be able to use it in multiple directories?


Let me start off by saying this place is a great resource and it has helped me develop a well working script, I am looking into expanding it and I am having some trouble doing so.

So, basically I am able to organize a directory with log files into multiple directories based on year and month within those years. I can do this to when I point to those directories directory.

What I am looking to do is apply these script to multiple directories, basically hundreds of logs in (each) multiple directories but all those directories within one main directory.

I have tried to apply the script using the foreach cmdlet but it just doesn't seem to work.

#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------

foreach($folder in (Get-ChildItem '\\drive\software\logstoreage    \directories' -Directory)){
    New-Item -ItemType directory -Path ($folder.fullname+"\Archive")
}

#--------
# Organization of Logs into Yearly and Monthly directories.  
# Comments: Folders are created based on the Year capturing logs through the end of 2018. 
#--------

$date = (Get-Date -Month 1 -Day 1 -Year 2019).ToString("01-01-2019")

$files = Get-ChildItem '\\drive\software\logstoreage\directories\SoftwareA' -Recurse | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer} 

$files

$targetPath = '\\drive\software\logstoreage\directories\SoftwareA\Archive'

foreach ($file in $files){
    $year = $file.LastWriteTime.Year.ToString()
    $month = $file.LastWriteTime.Month.ToString()

    $file.Name
    $year
    $month

    $Directory = $targetPath + "\" + $year + "\" + $month

    if (!(Test-Path $Directory)){
        New-Item $directory -type directory
    }

    $file | Move-Item -Destination $Directory
}

Solution

  • So you have a loop to get all of the target folders, you just need to include the rest of your script within that loop, and make a few modifications based on it being inside a loop.

    foreach($folder in (Get-ChildItem '\\drive\software\logstoreage\directories' -Directory)){
        #--------
        # Creation of an "Archive" folder within the root directory of the targeted dir.
        # Comments: Archives Folder is created in each targeted directory.
        #--------
        $ArchiveFolder = New-Item -ItemType directory -Path ($folder.fullname+"\Archive") -Force
    
    
        #--------
        # Organization of Logs into Yearly and Monthly directories.  
        # Comments: Folders are created based on the Year capturing logs through the end of 2018. 
        #--------
    
        $date = Get-Date -Month 1 -Day 1 -Year 2019
    
        $files = Get-ChildItem $folder -Recurse -File | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer} 
    
        $files
    
        $targetPath = $ArchiveFolder.FullName
    
        foreach ($file in $files){
            $year = $file.LastWriteTime.Year.ToString()
            $month = $file.LastWriteTime.Month.ToString()
    
            $file.Name
            $year
            $month
    
            $Directory = $targetPath + "\" + $year + "\" + $month
    
            if (!(Test-Path $Directory)){
                New-Item $directory -type directory
            }
    
            $file | Move-Item -Destination $Directory
        }
    }