/bundles/itninjaweb/img/Breadcrumb_cap_w.png
I need your help.

1. I am look for a script to replace a text from particular files in a network.
2. This script will then rename all files in a folder. and add "- abc" in a file name eg: From file name is "abc.doc" to "abc - xyx.doc"

Thanks
0 Comments   [ - ] Hide Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
Answer this question or Comment on this question for clarity

Answers

0
Function reads filename1, replaces token, result writes to filename2:
Function works only for text files (for doc files you need to use Microsoft Office API).


Function ReplaceInFile(filename1,filename2,fromToken,toToken)
SET file=objFSO.OpenTextFile(filename1,1)
if err.number>0 then
MsgBox "Can't read file " & filename1, vbOKOnly + vbCritical, "Error"
WScript.Quit(1)
end if
contents=file.ReadAll
file.close

contents=replace(contents,fromToken,toToken)

SET file=objFSO.CreateTextFile(filename2,8)
if err.number>0 then
MsgBox "Can't write file " & filename2, vbOKOnly + vbCritical, "Error"
WScript.Quit(1)
end if
file.write(contents)
file.Close
End Function
Answered 06/02/2011 by: itolutions
Purple Belt

Please log in to comment
0
My reading is that the OP wants to rename the files not replace the content.

I didn't bother replying because there must be quadzillions of script samples out there for recursively walking through files in a folder (and sub-folders, if required) and renaming a file is simple, using the .Move method of the FileSystemObject, examples of which must again number in the quadzillions.
Answered 06/03/2011 by: VBScab
Red Belt

Please log in to comment
0
Thanks...

ORIGINAL: itolutions

Function reads filename1, replaces token, result writes to filename2:
Function works only for text files (for doc files you need to use Microsoft Office API).


Function ReplaceInFile(filename1,filename2,fromToken,toToken)
SET file=objFSO.OpenTextFile(filename1,1)
if err.number>0 then
MsgBox "Can't read file " & filename1, vbOKOnly + vbCritical, "Error"
WScript.Quit(1)
end if
contents=file.ReadAll
file.close

contents=replace(contents,fromToken,toToken)

SET file=objFSO.CreateTextFile(filename2,8)
if err.number>0 then
MsgBox "Can't write file " & filename2, vbOKOnly + vbCritical, "Error"
WScript.Quit(1)
end if
file.write(contents)
file.Close
End Function

Answered 06/03/2011 by: MrKool
Yellow Belt

Please log in to comment