Find & Replace Script for Text Files with Environment Variables

Posted on 2012-09-06
Last Modified: 2012-09-20
Good Morning -

I've got an issue with many text based files from SCCM 2012 which I'm trying to find a workaround for.  

Long story short, it's Software Metering function often creates a text based file per machine and is placed in a "process" folder for processing.  All of these files are not being processed due to the error of my domain's netbios name having an "&" in it.  Therefore the files is moved to a "corrupt" folder indefinatly.  However, I think if I find a way to change all netbios references to FQDNs, then it will work.  

Therefore, I need to find a way to do the following:

Original file
- Single (but long) text based file with .MUX extension
- Has many netbios references like "UserName="AB&C_CN\variable"
- The variable can be hostname or username, but is always one complete word

Target File
- All instances of "AB&C_CN\variable" need to be replaced with the domain's FQDN of "" throughout file

- Each individual file has the same variable after the netbios name, but the variable is different per file.
- Once find / replace is complete, the file would need to be moved from it's current path (D:\Program Files\Microsoft Configuration Manager\inboxes\\corrupt) to the "process" folder (D:\Program Files\Microsoft Configuration Manager\inboxes\\process)

Any suggestions on how this could be automated via script or other process?  Thanks
Question by:BzowK
    LVL 9

    Expert Comment

    by:Derek Jensen
    What OS are you working in? What scripting languages do you know? Based on the OS, the solution would be different.

    If it's a webserver running Apache, it'd be a simple matter to make a PHP script that ran as a cron that checked all the files; if it's Windows...I'd love to know if it's possible myself, as I haven't found anything yet that can do this.
    LVL 67

    Accepted Solution

    Using .Net languages it is easy. This is the PowerShell way:
    $src = 'D:\Program Files\Microsoft Configuration Manager\inboxes\\corrupt\'
    $dst = 'D:\Program Files\Microsoft Configuration Manager\inboxes\\process\'
    $pattern = '"AB&C_CN\\(.*)"'
    $replace = '$'
    gci $src | % {
      (gc $_.FullName) -replace $pattern, $replace |
        out-file -encoding ANSI "$dst\$_.Name"
      remove-item $_.FullName

    Open in new window

    All dynamic stuff is located in the first 4 lines - just set that up to your needs.
    Note the \\ as backslash has a special meaning in regular expressions, and needs to be "escaped".

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now