Welcome, Guest. Please Login
vTask Homepage
 
 
Home Help Search Login


Page Index Toggle Pages: 1
Send Topic Print
Create a simple "Replace in Multiple Files" program (Read 974 times)
vTask Support
Moderator
*
Offline



Posts: 3596
Tucson, AZ
Create a simple "Replace in Multiple Files" program
Oct 20th, 2009 at 11:51am
 
The following script provides Search & Replace capabilities across multiple files.  Most programs only provide a replace feature for a single file, so this utility can be very helpful when globally updating text or numbers.

You can choose files based on wildcards such as *.* or *.html, or you can pick individual files for the replace operation.  An actual program that would do this same job would be a significant amount of code & testing, but the vTask script is only 29 steps long, including comments and user prompts.

Caution!!
 There is no undo after running this script, so always be sure you have current backups of any files to be modified!  (a vTask script would work great for automatically creating backups, too)


...


...


...


...


VXM Script:   (Copy and Paste into vTask)
<step>
<action>Comment</action>
<comment>PURPOSE: Replace text in a group of files.</comment>
</step>
<step>
<action>Set Variable</action>
<output value="%file_list">Variable</output>
<comment>Erase previous</comment>
</step>
<step>
<action>Set Variable</action>
<text>0</text>
<output value="%total_files">Variable</output>
<comment>To track changes</comment>
</step>
<step>
<action>Set Variable</action>
<text>0</text>
<output value="%total_files_updated">Variable</output>
<comment>To track changes</comment>
</step>
<step>
<action>Comment</action>
<comment>Input the search/replace text</comment>
</step>
<step>
<action>Custom Dialog</action>
<text>|Search Text:||Replace With:|</text>
<options>Center</options>
<value1>Search/Replace in Files</value1>
<value3>EDIT</value3>
<value5>EDIT</value5>
<output value="%input">Variable</output>
<comment>"%input" contains everything from the dialog</comment>
</step>
<step>
<action>IF Expression</action>
<text>{command}!=OK</text>
<comment>Did user press something other than OK? Then exit</comment>
</step>
<step>
<action>Exit Run</action>
<indents>1</indents>
</step>
<step>
<action>END IF</action>
</step>
<step>
<action>Set Variable</action>
<text>=CHOOSE(1, %input, ";");</text>
<output value="%search">Variable</output>
<comment>Get the SEARCH string</comment>
</step>
<step>
<action>Set Variable</action>
<text>=CHOOSE(2, %input, ";");</text>
<output value="%replace">Variable</output>
<comment>Get the REPLACE string</comment>
</step>
<step>
<action>Comment</action>
<comment>Choose which files to scan (can be single or multiple)</comment>
</step>
<step>
<action>Select File</action>
<text>All Files (*.*)</text>
<options>File Open Multiple</options>
<value1>Replace in Multiple Files</value1>
<output value="%file_list">Variable</output>
<comment>Which files to scan?  (Allows multiple selections)</comment>
</step>
<step>
<action>IF Expression</action>
<text>{command}!=OK</text>
<comment>Did user press something other than OK? Then exit</comment>
</step>
<step>
<action>Exit Run</action>
<indents>1</indents>
</step>
<step>
<action>END IF</action>
</step>
<step>
<action>Comment</action>
<comment>Start scanning files...  We're using "Start TEXT Loop" since we have a text list of arbitrary file names</comment>
</step>
<step>
<action>Start Text Loop</action>
<text>%file_list</text>
<value1>;</value1>
<output value="%next_file">Variable</output>
<comment>Loop thru each of the files</comment>
</step>
<step>
<action>Status Popup</action>
<text>Scanning "%next_file"...</text>
<value1>600</value1>
<value2>120</value2>
<timeout>5</timeout>
<indents>1</indents>
<comment>Show the user what is happening</comment>
</step>
<step>
<action>Read File</action>
<value1>%next_file</value1>
<indents>1</indents>
<output value="%text">Variable</output>
<comment>Read the contents of the next file</comment>
</step>
<step>
<action>IF Text</action>
<text>%text</text>
<options>Contains Substring (Insensitive)</options>
<value1>%search</value1>
<indents>1</indents>
<comment>Does this file have the search string?</comment>
</step>
<step>
<action>Replace</action>
<text>%text</text>
<options>Case Insensitive</options>
<value1>%search</value1>
<value2>%replace</value2>
<indents>2</indents>
<output value="%fixed">Variable</output>
<comment>If yes: Do the actual replace!</comment>
</step>
<step>
<action>Write/Create File</action>
<text>%fixed</text>
<options>Erase</options>
<value1>%next_file</value1>
<indents>2</indents>
<comment>Write out the updated file, with new text</comment>
</step>
<step>
<action>Increment Variable</action>
<indents>2</indents>
<output value="%total_files_updated">Variable</output>
<comment>Keep track of how many we have modified</comment>
</step>
<step>
<action>END IF</action>
<indents>1</indents>
</step>
<step>
<action>Increment Variable</action>
<indents>1</indents>
<output value="%total_files">Variable</output>
<comment>Keep track of how many we have checked</comment>
</step>
<step>
<action>NEXT LOOP</action>
</step>
<step>
<action>Status Popup</action>
<value1>600</value1>
<value2>120</value2>
<timeout>5</timeout>
<comment>We're done; Get rid of our status popup</comment>
</step>
<step>
<action>Display Message</action>
<text>Job is done!

Total Files Checked: %total_files

Total Files Updated: %total_files_updated
</text>
<value1>Replace in Multiple Files</value1>
<comment>Report the results</comment>
</step>
Back to top
« Last Edit: Oct 20th, 2009 at 12:54pm by vTask Support »  

Thank you for using vTask Studio!
WWW  
IP Logged
 
HappyFace
Full Member (10+)
*
Offline



Posts: 16
Re: Create a simple "Replace in Multiple Files" program
Reply #1 - Oct 20th, 2009 at 8:48pm
 
Nice... very usefull! Smiley
Back to top
 
 
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print