Home > Mobile >  How would I loop through directories with PowerShell to grab text files?
How would I loop through directories with PowerShell to grab text files?

Time:02-10

I'm trying to find out a way use a loop to go through directories with different names, go into a folder in each of those with the same name (TSO), grab the text file, use it for functions (Extractor and VisioMaker), then repeat the process for the next folders. To clarify, I'm only having a problem with the loop portion. Here's the directory format:

Main Folder (TestCircuits) >

  1. Folder1 > TSO > Name1.txt
  2. Folder2 > TSO > Name2.txt
  3. Folder3 > TSO > Name3.txt
  4. ...
Set-Location " \TestCircuits"


       #Testing

       $location =  " \TestCircuits"

       Foreach-Object {

       
       #select only the latest text file
       $Global:fileM = Get-ChildItem "*\TSO\*.txt" | sort LastWriteTime | select -last 1 | Get-Item

       #set new variable for clarity in later function
       $Global:ccsd = $fileM

       #use recurse for the loop? Seems to work fine
       $Global:files = Get-ChildItem " \TestCircuits" -Recurse
       
       #path format for the loop might be janky, but seems to work
       $Global:textFileData = Get-ChildItem " \TestCircuits\*\TSO\$ccsd" | Get-Content
       $Global:textFileDataRaw = Get-ChildItem " \TestCircuits\*\TSO\$ccsd" | Get-Content -raw

 
              foreach ($fileM in $files) {
                   #need to use $fileM but not sure how

                  Function Extractor {
                      #code that sets variables from text in each file
                      #uses $ccsd variable to get text
                                }

                  Function VisioMaker {
                      #code that makes Visio doc from variables each time
                      #variables used are from Extractor function
                                }
                }
}

Any help would be appreciated. Thanks!

CodePudding user response:

There are still a few things I don't understand. For instance why you would use global variables. Global variables are only used when you initially need to set them and use them in numerous functions, but normally you would just pass the correct variables to each function.

I have made a few adjustments for your script to make it work.

Set-Location " \TestCircuits"


#Testing

$location = " \TestCircuits"


#Get all folder recursively that are named TSO
$tsoFolders = Get-ChildItem -Recurse -Directory -Filter 'TSO'

#Loop through each folder, get the latest written text file per folder and use the functions foreach latest file
foreach($tsoFolder in $tsoFolders){
    #select only the latest text file
    $Global:fileM = Get-ChildItem -Path $tsoFolder.FullName -Filter "*.txt" | sort LastWriteTime | select -last 1 | Get-Item

    #set new variable for clarity in later function
    $Global:ccsd = $fileM

    #path format for the loop might be janky, but seems to work
    $Global:textFileData =  Get-Content $ccsd.FullName
    $Global:textFileDataRaw = Get-Content $ccsd.FullName -Raw


    Function Extractor {
        #code that sets variables from text in each file
        #uses $ccsd variable to get text
    }

    Function VisioMaker {
        #code that makes Visio doc from variables each time
        #variables used are from Extractor function
    }
}

This function only processes each last written text file per TSO folder, but can easily be adjusted to process all text files.

To loop through the files, you could simply call the function and the function can be outside the loop. See the below example of how to use it better without the gloval variables:

Function Extractor ($FileData, $RawFileData){
    #$FileData is available here and $RawFileData
    
    #code that sets variables from text in each file
    #uses $ccsd variable to get text
}

Function VisioMaker ($FileData, $RawFileData){
    #$FileData is available here and $RawFileData
    
    #code that makes Visio doc from variables each time
    #variables used are from Extractor function
}

Set-Location " \TestCircuits"


#Testing

$location = " \TestCircuits"


#Get all folder recursively that are named TSO
$tsoFolders = Get-ChildItem -Recurse -Directory -Filter 'TSO'

#Loop through each folder, get the latest written text file per folder and use the functions foreach latest file
foreach($tsoFolder in $tsoFolders){
    #select only the latest text file
    $fileM = Get-ChildItem -Path $tsoFolder.FullName -Filter "*.txt" | sort LastWriteTime | select -last 1 | Get-Item

    #set new variable for clarity in later function
    $ccsd = $fileM

    #path format for the loop might be janky, but seems to work
    $textFileData =  Get-Content $ccsd.FullName
    $textFileDataRaw = Get-Content $ccsd.FullName -Raw

    #call the extractor function with the file data
    Extractor -FileData $textFileData -RawFileData $textFileDataRaw

    #call the VisioMaker function with the file data
    VisioMaker -FileData $textFileData -RawFileData $textFileDataRaw
}
  • Related