none
Looping through folders in a directory and subfolders RRS feed

  • Question

  • I have a directory with the following folder structure:

    • BookingService
      • DEV
        • BookingService.config
      • UAT
        • BookingService.config
      • LIVE
        • BookingService.config
    • DriverService
      • DEV
        • DriverService.config
      • UAT
        • DriverService.config
      • LIVE
        • DriverService.config
    • AccountService
      • DEV
        • AccountService.config
      • UAT
        • AccountService.config
      • LIVE
        • AccountService.config

    I want to loop through each folder and the underlying DEV, UAT, LIVE subfolder to extract the contents of the *.config file.

    I have tried the following so far, but keep getting the error message: Cannot process argument because the value of argument "path" is null. How do I fix it?

    $branch = "C:\work\Services"
    $targetEnv = "DEV"
    
    Get-ChildItem -Path $branch | % { 
        cd $_.DirectoryName 
        . $_.FullName 
        ($path = Resolve-Path "$($_.DirectoryName\($targetEnv)*\*.config") 2> $null
        if ($path -ne $null)
        {
            # do some stuff here
        }
    }
    • Moved by Bill_Stewart Wednesday, October 18, 2017 8:07 PM Abandoned
    Friday, September 1, 2017 9:16 AM

All replies

  • $files = Get-ChildItem -Path C:\work\Services -Filter *.config -Recurse -ErrorAction SilentlyContinue -Force
    foreach ($file in $files)
    {
      $text = get-content $file.fullname | out-file c:\test\results.txt -append
      }
      
    Friday, September 1, 2017 9:58 AM
  • If I got you right you could start twith something like this:
    Get-ChildItem -Path 'C:\work\Services\*Service\*\*service.config' | 
        ForEach-Object {
            "Do somthing with '$($_.FullName)'"
        }


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, September 1, 2017 10:12 AM
    Friday, September 1, 2017 10:11 AM
  • yes but I have the -recurse so it looks in all subfolders and the -ErrorAction SilentlyContinue - force 

    in case you dont have permission to a particular folder

    Friday, September 1, 2017 10:15 AM
  • $files = Get-ChildItem -Path C:\work\Services -Filter *.config -Recurse -ErrorAction SilentlyContinue -Force
    foreach ($file in $files)
    {
      $text = get-content $file.fullname | out-file c:\test\results.txt -append
      }
      
    What if I am only interested in a batch of .config files from a particular environment ie DEV, UAT or LIVE?
    Friday, September 1, 2017 10:25 AM
  • yes but I have the -recurse so it looks in all subfolders and the -ErrorAction SilentlyContinue - force 

    The OP exactly defined the folders/level so you don't need the recurse parameter and if you don't have access to the folders/files you want to treat you don't need to run a script, right?  ;-)

    We might wait until he tested our recommendations.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, September 1, 2017 10:28 AM
    Friday, September 1, 2017 10:27 AM
  • Just turn it into a function

     

    Function Getfilecontents{
    Param (
    [String] $Path,
    [String] $Filter,
    [String] $Outfile
    )
    $files = Get-ChildItem -Path $Path -Filter $filter -Recurse -ErrorAction SilentlyContinue -Force
    foreach ($file in $files)
    {
      $text = get-content $file.fullname | out-file $Outfile -append
      }
      }

    #Usage

    Getfilecontents -Path c:\copied -filter *.txt -outfile c:\test\myfile.txt

    Friday, September 1, 2017 10:36 AM
  • Then this should work:
    Get-ChildItem -Path 'C:\work\Services\*Service\DEV\*service.config' | 
        ForEach-Object {
            "Do somthing with '$($_.FullName)'"
        }
    or this:
    Get-ChildItem -Path 'C:\work\Services\*Service\UAT\*service.config' | 
        ForEach-Object {
            "Do somthing with '$($_.FullName)'"
        }
    or this:
    Get-ChildItem -Path 'C:\work\Services\*Service\LIVE\*service.config' | 
        ForEach-Object {
            "Do somthing with '$($_.FullName)'"
        }
    It would be helpful when you already know what you're after.  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, September 1, 2017 10:40 AM
    Friday, September 1, 2017 10:39 AM