Script to edit multiple files [Solved/Closed]

Posts
1
Registration date
Wednesday March 11, 2009
Last seen
March 12, 2009
- - Latest reply:  New2This - Oct 29, 2010 at 03:51 AM
Hello,

How do I go about writing a script that will edit multiple text files? This is to be performed on 4 different servers that are Dell 1850's running Win Server 2003. Files extensions such as *.htm, *.ini, and *.xml. Actions would be mostly find/replace text strings and adding/removing comments. Some I'm sure there is a means to accomplish this and I am hoping this is the right place to ask.

Holler if more info is needed.

Thanks in advance
Fred
See more 

6 replies

Best answer
Posts
1
Registration date
Friday July 31, 2009
Last seen
August 1, 2009
4
Thank you
Hi Freondude,

If you are still looking for an answer, then try this:

1.Open Notepad, Copy and paste the below script :

@echo off
REM — Prepare the Command Processor –
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude – parses a File line by line and replaces a substring”
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] – string to be replaced
:: NewStr [in] – string to replace with
:: File [in] – file to be parsed
if “%*”==”" findstr “^::” “%~f0″&GOTO:EOF
for /f “tokens=1,* delims=]” %%A in (’”type %3|find /n /v “”"‘) do (
set “line=%%B”
if defined line (
call set “line=echo.%%line:%~1=%~2%%”
for /f “delims=” %%X in (’”echo.”%%line%%”"‘) do %%~X
) ELSE echo.
)

2. Save the file as “xyz.bat”

The above script will parses a file line by line and will replaces a desired substring.

3. Open cmd prompt >> type the following command:

c:\xyz.bat “text-to-replace” text-to-replace-with c:\FileToEdit.txt > c:\Edited_file.txt

For more info, visit http://winitpro.wordpress.com/2009/07/31/batchscript/

Hope this helps :)

Say "Thank you" 4

A few words of thanks would be greatly appreciated. Add comment

CCM has helped 2743 users this month

Posts
15
Registration date
Friday May 22, 2009
Last seen
February 2, 2010
3
Thank you
Thank you for pointing out the misspelled variable in the script. The variable $content was misspelled as $coontent. Here is the corrected script.


# Get a list of .html files. 
var str list ; lf -n "*.html" > $list 
# File loop. 
while ($list <> "") 
do 
# Read in the next file. 
var str file ; lex "1" $list > $file 
var str content ; cat $file > $content 
# Replace "ABC" to "XYZ". 
sal "^ABC^" "XYZ" $content > null 
# Write file back. 
echo $content > { echo $file } 
done



Sen
Posts
15
Registration date
Friday May 22, 2009
Last seen
February 2, 2010
2
Thank you
Hi Newboy:

Here is the script for you. I have added comments (#) so it will be easy for you to understand what it does.


# Script Microsoft2Yahoo.txt
# .html files are in folder C:/abc. Collect a list of them.
var str list ; lf -n "*.html" "C:/abc" > $list
# Process files one by one.
while ( $list <> "")
do
    # Get the next file.
    var str file ; lex "1" $list > $file
    # Read file contents into a string variable.
    var str content ; cat $file > $content
    # Keep replacing "www.micosoft.com" with "www.yahoo.com" until there are
    # no more instances left of "www.micosoft.com".
    while ( { sen -c "^www.micosoft.com^" $content } > 0 )
        sal "^www.micosoft.com^" "www.yahoo.com" $content > null
    # All instances are replaced. Write file back.
    echo $content > { echo $file }
done




Save the script as C:/Scripts/Microsoft2Yahoo.txt, start biterscripting ( http://www.biterscripting.com ) , enter the following command.


script "C:/Scripts/Microsoft2Yahoo.txt"



We are using the file-directory-loop to loop thru files, the sen (string enumerator) command to count instances of "www.microsoft.com" in a string, and the sal (string alterer) command to replace "www.microsoft.com" with "www.yahoo.com". Help on all of these is available at that web site. (It might help to learn a bit of biterscripting if you plan on automating things of this nature in the future. They have some good tutorials posted on their site.)


Sen
1
Thank you
The above Error 602: has be resolved.... thanks
0
Thank you
to change files in folders and subs just use something like this:

C:\Users\jo\Pictures\to-upload*.*ren *.JPG *.jpg
0
Thank you
anyone know if a Carriage Return is possible after the change has been made, so:

(file 1)

hi this is cool

-------------BECOMES ------------------

hello
this is cool

("hi" and "hello" being the replacement for "www.micosoft.com" and "www.yahoo.com" in the script that is.)

Thanks, appreciate help.

Eilz
Ok I got a little further playing with the script and now have the below :

This bascally looks for folder c:\abc for all .txt file in path, and adds new line called #EDITION after a line containing TITLE. BUT need it working also within SUB-FOLDERS of c:\abc

I have played around with the script no end, but can't find a way to do it, tried changing sen -c to sen -r but no luck. Would appreciate any help!!!

# Script Microsoft2Yahoo.txt
# http://ccm.net/forum/affich-77130-script-to-edit-multiple-files
# .html files are in folder C:/abc. Collect a list of them.
var str list ; lf -n "*.txt" "C:\abc" > $list
# Process files one by one.
while ( $list <> "")
do
# Get the next file.
var str file ; lex "1" $list > $file
# Read file contents into a string variable.
var str content ; cat $file > $content
# Keep replacing "www.micosoft.com" with "www.yahoo.com" until there are
# no more instances left of "www.micosoft.com".
while ( { sen -c "^#TITLE:^" $content } > 0 )
sal "^TITLE:^" "TITLE :" $content > null
sal "^\n^" "\n#EDITION:Z\n" $content > null
# All instances are replaced. Write file back.
echo $content > { echo $file }
done
SenHu
Posts
15
Registration date
Friday May 22, 2009
Last seen
February 2, 2010
> Eilz -
I don't think the second sal command is getting executed in the while loop. Since there is no do-done, while will execute only the first sal. You really need only one sal command - to change each instance of "#TITLE:" to "TITLE :\n#EDITION:Z\n". I updated your script accordingly. There is only one change - in the sal command in the while loop. Otherwise, your scripts seems correct.



# Script Microsoft2Yahoo.txt 
# http://ccm.net/forum/affich-77130-script-to-edit-mult­iple-files 
# .html files are in folder C:/abc. Collect a list of them. 
var str list ; lf -n "*.txt" "C:\abc" > $list 
# Process files one by one. 
while ( $list <> "") 
do 
    # Get the next file. 
    var str file ; lex "1" $list > $file 
    # Read file contents into a string variable. 
    var str content ; cat $file > $content 
    # Keep replacing "#TITLE:" with "TITLE :\n#EDITION:Z\n" until there are 
    # no more instances left of "#TITLE:" .
    while ( { sen -c "^#TITLE:^" $content } > 0 ) 
        sal "^TITLE:^" "TITLE :\n#EDITION:Z\n" $content > null
    # All instances are replaced. Write file back. 
    echo $content > { echo $file } 
done



I have not tested the script.
SenHu
Posts
15
Registration date
Friday May 22, 2009
Last seen
February 2, 2010
> Eilz -
Eilz said:

BUT need it working also within SUB-FOLDERS of c:\abc



OK, to do this for files in subfolders and subsubfolders, etc., use the -r (recursive) option of the lf command. So,

var str list ; lf -n -r "*.txt" "C:\abc" > $list 



You may find some more goodies in the help page for the lf command, using which you can further fine-tune which files are listed by the lf command.
SenHu
Posts
15
Registration date
Friday May 22, 2009
Last seen
February 2, 2010
-
Just use "hi this is cool " in place of "www.micosoft.com" (the string being replaced)

and use "hello\nthis is cool " in place of "www.yahoo.com" (the string to replace with).

Make the change in both the sen and sal commands. \n means newline. You can use tabs (\t), carriage return (\r) and any other control characters similarly.
# Script Microsoft2Yahoo.txt
# http://ccm.net/forum/affich-77130-script-to-edit-mult­iple-files
# .html files are in folder C:/abc. Collect a list of them.
var str list ; lf -n "*.txt" "C:\abc" > $list
# Process files one by one.
while ( $list <> "")
do
# Get the next file.
var str file ; lex "1" $list > $file
# Read file contents into a string variable.
var str content ; cat $file > $content
# Keep replacing "#TITLE:" with "TITLE :\n#EDITION:Z\n" until there are
# no more instances left of "#TITLE:" .
while ( { sen -c "^#TITLE:^" $content } > 0 )
sal "^TITLE:^" "TITLE :\n#EDITION:Z\n" $content > null
# All instances are replaced. Write file back.
echo $content > { echo $file }
done


The above script gives this error message and does not run,,,,,

Error 602: Command TTT.txt at position 1 is not recognized