Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search String and rename

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a ton of files from reports I run every day that I have to manually rename from a string found in the file. what I am looking for is a way to run a script that will go through all the files in a directory look for the
string and rename the file to that string. My file looks like this.

CONSUMPTION UNITS REPORT MAR 20, 2002 11:14 AM PAGE 1
01/23/02 00:01
CLOCK: 15 MINS STOP TIME: 02/19/02 13:15
DEVICE ID: 338283
ACCOUNT #: 338283-001


I want to rename by account #

any help would be appreciated.
 
digital-dawg,

This script will replace strings in all files with certain file types.

fengshui_1998

' Copy and save to ReplaceTxt.VBS script file
' -------------------------------------------------
'
' This script will replace text in all the file of file types .XXX
'
prompt = "Select file types to match" & vbcrlf & _
"Do not include '.'"

infile = InputBox(prompt,"Get Files")

findstr = InputBox("Enter string to find> ","Find String")
replstr = InputBox("Enter string to substitute> ","Substitute String")

If infile = "" or findstr = "" or replstr = "" then wscript.quit

Set fso = CreateObject("Scripting.FileSystemObject")
' Get current directory
path = ScriptPath()

Set fldr = fso.GetFolder(path)
Set filelist = fldr.Files
' num_files = filelist.count ' COUNT the number of files
For Each file in filelist
fname = file.ParentFolder & "\" & file.name

If TypeMatch( file.name, infile) Then
ret = FileTxtReplace(fname, findstr, replstr)
If ret <> 0 then msgbox &quot;Warning: &quot; & file.name
End If
Next


' *********************
Function ScriptPath()
' *********************
ScriptFullName = Wscript.ScriptFullName ' path & filename
ScriptFileName = Wscript.ScriptName ' just filename
intFile = Instr(ScriptFullName, ScriptFileName) ' Locate end of path
ScriptPath = Left(ScriptFullName, intFile - 1) ' Path includes &quot;\&quot;
End Function

' ********************************
Function TypeMatch(f1tmp, f2tmp)
' ********************************
If instrRev(f1tmp, &quot;.&quot;) > 0 then
ftype = right(f1tmp, len(f1tmp) - instrRev(f1tmp, &quot;.&quot;) )
if instr(1, ftype, f2tmp, 1) then
TypeMatch = True
else
TypeMatch = False
end if
Else
TypeMatch = False
End If

End Function

' ****************************************
Function FileTxtReplace(theFilename,findThis,replaceThis)
' ****************************************
Const ForReading = 1, ForWriting = 2
Dim fso, f, fileContents
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If not fso.FileExists(theFilename) then 'error trap for file doesn't exist
FileTxtReplace=53
Exit Function
End If
Set f = fso.GetFile(theFilename)
If f.size=0 then ' error. file is 0 bytes
FileTxtReplace=75
Exit Function
End If
Set f = fso_OpenTextFile(theFilename, ForReading)
fileContents= f.ReadAll
Set f = fso_OpenTextFile(theFilename, ForWriting, True)
f.Write Replace(fileContents, findThis, replaceThis,1,-1,1)
FileTxtReplace=0 'return ok error code
End Function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top