/build/static/layout/Breadcrumb_cap_w.png

replace a text from particular files in a network

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   [ + ] Show comments

Answers (3)

Posted by: itolutions 12 years ago
Purple Belt
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
Posted by: anonymous_9363 12 years ago
Red Belt
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.
Posted by: MrKool 12 years ago
Yellow Belt
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

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ