Re: Searching & editing all text files in a folder



> I need a script which would have a folder passed as an argument, and would
> then search all txt files in the folder for a specific string and then
> delete the string if it exists.

> The folders contain several thousand small text files.

> Is this possible? Any pointers would be much appreciated.

You don't say exactly how the target folder is "passed" to the script, so I just
defined it as a text string at the start of the script, but I think the "meat"
of the script is what you want.

'---------------------------------
target="d:\folder"
Set fso = CreateObject("Scripting.FileSystemObject")
Set root=fso.getFolder(target)
someString="Text you want to delete"

for each file in root.files
if file.type="Text Document" then
Set textStream=file.OpenAsTextStream(1)
data=textStream.readAll
textStream.close
data=replace(data,someString,"")
Set textStream= file.OpenAsTextStream(2)
textStream.write data
textStream.close
end if
next
'-----------------------------------

I am assuming that all the "small text files" have the ".txt" extension and will
be classified as "Text Document." If not, you'll have to do some other extension
testing to verify the files.
--
Crash


.



Relevant Pages

  • Re: recursive file editing
    ... > 2) each file examined should be edited; IF a string of this type is found ... > the edited output file should also be renamed according to the same rule ... script with the directory you want to process. ... for folder, folders, files in os.walk: ...
    (comp.lang.python)
  • Re: OT:macros or software?
    ... when a .PDF file is stored there, ... words and depending on weather those words are found in that document, move it to a predetermined folder. ... Easy enough if it were a text document, or even Word, as you could write a DOS script, or a VBS script if you had to load Word to read Word documents, although writing such a script can be harder to a beginner than some might think. ... If the string was in the name of the file it would be less work - you can set up the rather marvellous "robocopy" but that won't read inside a file, and certainly not a pdf! ...
    (uk.comp.homebuilt)
  • Re: rename files in directory
    ... When I run your script I get an error: " Microsoft VBScript runtime error: ... contain a " - " string. ... as part of the query. ... if the target folder has files with names in a format other ...
    (microsoft.public.scripting.vbscript)
  • Re: User account and Personal folder mismatch script
    ... I believe you want a script that can be run by an administrator that will ... The script above only documents if a home folder has been assigned. ... > End Sub ...
    (microsoft.public.scripting.vbscript)
  • Input on CleanUp script
    ... Since I'm going to be using this script in a production environment, ... If the script is used with a backup folder ... var strTargetFolder, strBackupFolder, args0, args1, args2, noArgs; ...
    (microsoft.public.scripting.wsh)

Loading