我将如何使用 PowerShell 循环遍历目录以获取文本文件?

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

我正在尝试找出一种方法,使用循环遍历具有不同名称的目录,进入每个具有相同名称 (TSO) 的文件夹中,获取文本文件,将其用于函数(Extractor 和 VisioMaker),然后对下一个文件夹重复该过程。澄清一下,我只是循环部分有问题。这是目录格式:

主文件夹 (TestCircuits) >

  1. 文件夹 1 > TSO > Name1.txt
  2. 文件夹 2 > TSO > Name2.txt
  3. 文件夹 3 > 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
                                }
                }
}

如有任何帮助,我们将不胜感激。谢谢!

还有一些地方我不明白。例如,为什么要使用全局变量。全局变量仅在您最初需要设置它们并在许多函数中使用它们时使用,但通常您只需将正确的变量传递给每个函数。

我已对您的脚本进行了一些调整以使其正常运行。

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
    }
}

此功能仅处理每个 TSO 文件夹的每个最后写入的文本文件,但可以轻松调整以处理所有文本文件。

要循环遍历文件,您可以简单地调用该函数,该函数可以在循环之外。请参阅下面的示例,了解如何在没有 gloval 变量的情况下更好地使用它:

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
}