Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Find & Replace Script for Text Files with Environment Variables

Posted on 2012-09-06
2
Medium Priority
?
616 Views
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 "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
0
Comment
Question by:BzowK
2 Comments
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 38373150
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
 
LVL 72

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 38373596
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

580 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