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
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)
Please log in to answer
Posted by:
itolutions
13 years ago
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 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
13 years ago
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.
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
13 years ago
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.
so that the conversation will remain readable.