Find & Replace Script for Text Files with Environment Variables

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 "variable.mynetwork.com" throughout file

Overall
- 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\swmproc.box\corrupt) to the "process" folder (D:\Program Files\Microsoft Configuration Manager\inboxes\swmproc.box\process)

Any suggestions on how this could be automated via script or other process?  Thanks
BzowKAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Using .Net languages it is easy. This is the PowerShell way:
$src = 'D:\Program Files\Microsoft Configuration Manager\inboxes\swmproc.box\corrupt\'
$dst = 'D:\Program Files\Microsoft Configuration Manager\inboxes\swmproc.box\process\'
$pattern = '"AB&C_CN\\(.*)"'
$replace = '$1.mynetwork.com'

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".
0
 
Derek JensenCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.