Exclude Sub Folders From Get-ChildItem

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of random commandline random commandline 3 weeks ago.

  • Author
    Posts
  • #69883
    Profile photo of Chris Cass
    Chris Cass
    Participant

    Trying to write a script to migrate a web based application from one server to another. The three things I need to change in all the configuration files are any references to the hostname, the IP address, and drive letter that the application resides on (in the event that changes too). I've figured out how to use get-childitem to give me all the files in the main directory and all subfolders, but there are a few folders where permissions are intentionally blocking me from changing the configurations, and I need to figure out how to exclude these subfolders from my search. To give a bit on the setup, we'll say the application is housed at X:\Website. Within that folder, depending on how many times the site has been upgraded in the past, there can be multiple folders called X:\Website\ConfigBackup_XX (where XX is the previous version number before they upgraded). I need exclude all of these folders from my search. There is also a folder called "X:\Website\Website\Json" that I need to exclude. Finally, with every new version of the software there is a database upgrade script. This results in multiple folders at X:\Website\Website\Tools\Database\UpgradeXX (again, XX is the version number for that upgrade script). I really don't need to drill down that far, and can basically ignore everything at X:\Website\Website\Tools. I've also realized I can skip any *.log or *.bak file type.

    So first I ran:

    $exclude = @("*.log*", "*.bak*", "*configbackup*", "*tools*", "*Json*")
    $Files = Get-ChildItem -path "\\$newip\$newdriveletter$\WebSite\*.*" -recurse -exclude $exclude
    ForEach ($file in $files){$content = gc $($file.FullName) -Raw
    $content -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Website", "${newdriveletter}:\Website" | out-file $($file.fullname)}
    

    When I did this it seems to have worked for everything except the "*tools*" which is still throwing an error saying I don't have permissions to edit. I tried changing that to "*Website\Tools\Database\Upgrade*" and still saw the same errors. I found someone else suggesting that this would only work with files, and if I wanted it to work with subfolders I had to format it a bit differently. Based on what they used I came up with:

    $Files = Get-ChildItem -path "\\$newip\$newdriveletter$\WebSite\*.*" -recurse | where {$_.fullname -notlike "*configbackup*", "*tools*", "*Json*", "*Website\Tools\Database\Upgrade*"}
    ForEach ($file in $files){$content = gc $($file.FullName) -Raw
    $content -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Website", "${newdriveletter}:\Website" | out-file $($file.fullname)}
    

    When I ran this I was back to square one and it was trying to hit all of these folders again (and yes, I'm also still missing the *.bak and *.log files here).

    Any idea how to get these to all be skipped?

  • #69981
    Profile photo of random commandline
    random commandline
    Participant

    Try without the wildcard characters.

    Get-ChildItem -path "\\$newip\$newdriveletter$\WebSite\" -recurse -exclude $exclude

You must be logged in to reply to this topic.