header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Script to display folders and sub folders search & replace
Last Post 14 Oct 2009 12:17 PM by Jason Archer. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Informative
tough_timeUser is Offline
New Member
New Member
Posts:2
Avatar

--
07 Oct 2009 01:49 PM
    Hi All,

    I am new on the forum.I have a little problem, I have a series of SQL files in folders and subfolders, and my objective is to write a script that will loop through all folders and subfolders, search and replace all the files using a specified string pattern.

    So I have sql files in numerous folders and sub folders, search for the word SalesDB and replace it with ProfileDB.

    I have managed to put together a powershell script that will do the search and replace, but the problem is that the folder path needs to be specified all the time, but If I have a script that loops through folders, I can just provide the folder path to my existing script in the form of a variable.

    Any ideas guys.
    PoshoholicUser is Offline
    PowerShell MVP, Community Director
    Basic Member
    Basic Member
    Posts:111
    Avatar

    --
    07 Oct 2009 07:53 PM
    Something like this should do the trick:

    Get-ChildItem -LiteralPath $rootFolder -Filter *SalesDB*.sql -Recurse -Force | Where-Object {-not $_.PSIsContainer} | Rename-Item -NewName {$_.Name -replace 'SalesDB','ProfileDB'}
    Kirk Munro [MVP]
    Poshoholic

    My blog: http://poshoholic.com
    Follow me on Twitter: http://twitter.com/poshoholic
    tough_timeUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    09 Oct 2009 08:04 AM
    Posted By Poshoholic on 07 Oct 2009 08:53 PM
    Something like this should do the trick:

    Get-ChildItem -LiteralPath $rootFolder -Filter *SalesDB*.sql -Recurse -Force | Where-Object {-not $_.PSIsContainer} | Rename-Item -NewName {$_.Name -replace 'SalesDB','ProfileDB'}

    Thanks for the reply.

    I wrote this script, it will replace strings in a .sql file and replace it with another string literal. At the moment, I need to place this in every folder and call the powershell script, I am looking for a way where I can have one script, provide it with a folder path and the script will loop through all folders and sub-folders.

    ls -recurse -filter *.sql |% { $text = (get-content $_.FullName) -replace "USE CompanyDB", "USE ProfileDB "; $text | out-file -encoding utf8 $_.FullName }

    seaJhawkUser is Offline
    Basic Member
    Basic Member
    Posts:191
    Avatar

    --
    09 Oct 2009 10:30 AM
    All you need to do is add Path parameter:

    ls -path c:\temp -recurse -filter *.sql |% { $text = (get-content $_.FullName) -replace "USE CompanyDB", "USE ProfileDB "; $text | out-file -encoding utf8 $_.FullName }
    Jason ArcherUser is Offline
    New Member
    New Member
    Posts:13
    Avatar

    --
    14 Oct 2009 12:17 PM
    You should get in the habit of using Set-Content instead of Out-File. Out-File is ok for strings (if you don't mess with the formatting rules for strings), but other data can get cut off.

    Use Out-File when you want to send Format-* data to a file.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer