Jump to content

Scripting Help


zorg2000k

Recommended Posts

Hi,

Not sure if this is the best section in this forum for help. Here's hoping :thumbup

Looking for some help in scripting a batch file to do the following.

I have a dir full of folders that contain lots of txt files. The naming conversion for the txt files are all different.

The txt file contents look like this :

DAMREM004202

FOUSAF0000022

CRDCRR000010P1

What I want the batch file to do is look for say "DAMREM004202" and update all the txt files that contain that reference to say "DAMREM004203"

This is an application stack that we use with OSD to deploy applications. Problem we have found is when it comes to update the software to a new version we will have to update all txt files so the new software version is used if we need to rebuild the machine from OSD.

Thanks for you help in advance

Zorg

Link to comment
Share on other sites


so you want a way to rename a folder of text files to the first line contained in the text file?

how about a vbscript to do that:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Log.txt", ForReading)
strCharacters = objFile.Readline
objFile.Close

strNewName = "C:\Scripts\" & strCharacters & ".txt"

objFSO.MoveFile "C:\Scripts\Log.txt", strNewName

Link to comment
Share on other sites

If I correctly understand what you're asking, your best bet is to get a utility such as Search and Replace. I can do it with KiXtart (which means it can probably be done in VBS as well, I just don't code VBS)...but Search and Replace will do exactly what you want (again, assuming I correctly understand your question).

Link to comment
Share on other sites

so you want a way to rename a folder of text files to the first line contained in the text file?

how about a vbscript to do that:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Log.txt", ForReading)
strCharacters = objFile.Readline
objFile.Close

strNewName = "C:\Scripts\" & strCharacters & ".txt"

objFSO.MoveFile "C:\Scripts\Log.txt", strNewName

Thanks for the reply,

What I am looking for is a script that will search through all *.txt files in c:\ for a txt value within the .txt file and replace it with another text value.

Something like this.

Within the follow 1-3.txt files I want to search and replace say "DAMREM004202" to "DAMREM004203"

1.txt

DAMREM004202

FOUSAF0000022

CRDCRR000010P1

2.txt

DAMREM004202

POLLH00005502

CRDCRR000010P1

3.txt

DAMREM004202

DRTCA0000022

CRDCRR000010P1

Hpe this helps.

Zorg

Link to comment
Share on other sites

All you need is a simple search and replace utility. Although this can be done using scripting, there are already some fantastic tools out there for this type of task. My suggestion would be to use rpl.exe.

Download the zip binary package, place the exe from the bin directory somewhere within your %path%. Within your console navigate to C: and run the following line of code:

rpl -R -q -x .txt DAMREM004202 DAMREM004203 *

Link to comment
Share on other sites

All you need is a simple search and replace utility. Although this can be done using scripting, there are already some fantastic tools out there for this type of task. My suggestion would be to use rpl.exe.

Download the zip binary package, place the exe from the bin directory somewhere within your %path%. Within your console navigate to C: and run the following line of code:

Location=Inputbox("Please type the location of the file you wish to run below, then press OK.", "Runas")

If IsEmpty(Location) Then
Wscript.Quit
End If

Confirm=MsgBox("You entered the following location:" & vbCrLf & vbCrLf & Location & vbCrLf & vbCrLf & "Click ok to make the changes.", vbOkCancel+vbInformation, "Runas")

If Confirm=vbOk Then

Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")
oShell.Run "rpl -R -q -x .txt DAMREM004202 DAMREM004203 " & Location
Wscript.Quit

Else Confirm=vbCancel
Wscript.Quit

End If

This should work, but may need to be played around with a little as i didnt have time to check it thoroughly.

Link to comment
Share on other sites

In addition to that line of code you could add the following, which is part of a script i created to to run programs as an admin:
From the information we were supplied, I can see no reason why the task would be performed by anyone other than an administrator and there shouldn't even be a need to use a script to deploy the command.

However I'm sure your addition may be useful to those capable of breaking down your code for other tasks.

<Edit>

BTW, the 'wildcard' asterisk at the end of my line of code was a requirement!

</Edit>

Link to comment
Share on other sites

In addition to that line of code you could add the following, which is part of a script i created to to run programs as an admin:
From the information we were supplied, I can see no reason why the task would be performed by anyone other than an administrator and there shouldn't even be a need to use a script to deploy the command.

However I'm sure your addition may be useful to those capable of breaking down your code for other tasks.

<Edit>

BTW, the 'wildcard' asterisk at the end of my line of code was a requirement!

</Edit>

It was snippet of code and not the full runas code. The snippet would allow him to enter the old information and the information to be replaced with and then click ok. Then when he clicks ok, the information he entered would be placed after the run code for the file and then the file would run with the information he entered.

I hope this makes sence? But basically, this code "should" work, but may need a little "tweaking" in places.

Link to comment
Share on other sites

All you need is a simple search and replace utility. Although this can be done using scripting, there are already some fantastic tools out there for this type of task. My suggestion would be to use rpl.exe.

Download the zip binary package, place the exe from the bin directory somewhere within your %path%. Within your console navigate to C: and run the following line of code:

rpl -R -q -x .txt DAMREM004202 DAMREM004203 *

Works a treat thanks :rolleyes:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...