Search : in
By :

Script to edit multiple files

Last answer on Sep 28, 2009 1:56:44 pm BST Freondude, on Mar 12, 2009 8:23:29 pm GMT 
 Report this message to moderators

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

Best answers for « Script to edit multiple files » in :
Renaming multiple files in batch Show Renaming multiple files in batch Native features Using a third-party program Renaming a large number of files can quickly become tedious. Fortunately, there are tools to automate this task in many cases: Native features On Windows...
Assembling multiple PDF files ShowAssembling multiple PDF files Intro Steps: Intro PDF creator is a software allowing you to print several files into one with its printer function. Download link http://en.kioskea.net/telecharger/telecharger-40-pdf...
[BootLoader] System dual boot by default Show[BootLoader] System dual boot by default For Lilo For Grub From your "bootloader" to change the default system load up, proceed as follow: (note that all these procedures are run under "root") For Lilo Edit configuration file...
[Linux]Cutting a file into several parts Show[Linux]Cutting a file into several parts Linux is full of utility tools allowing you to manipulate files. There is a very handy tool to split a file into several parts (for example to carry data on low capacity media). The split...
Download EF Multi File Renamer ShowEF Multi File Renamer is a versatile tool that lets you rename multiple files or folders (including subdirectories) and all this in one step. You can define your own rules of renaming or use the rules already predefined. Advantage Complex tasks...

1

alvin, on Jun 17, 2009 2:46:59 pm BST
  • +1

To change files in folders and subs just use something like this:

C:\Users\jo\Pictures\to-upload*.*ren *.JPG *.jpg

Reply to alvin

2

syeddilawer, on Aug 1, 2009 5:11:37 am BST
  • +1

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 :)

Reply to syeddilawer

3

SenHu, on Aug 12, 2009 6:12:45 pm BST
  • +1

Definitely use biterscripting ( http://www.biterscripting.com ) . Here is a sample script - it will replace all "ABC" in files *.html to "XYZ".

# 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" $coontent > null
# Write file back.
echo $content > { echo $file }
done

Reply to SenHu

4

newboy, on Sep 24, 2009 3:09:57 pm BST

Dear Senhu,

Im new to this.

I have a folder called 'folder abc' with 100 .html files within (along with other file types).

I trying to run a script that will replace certain contents of these .html files with somthing else. i.e.

swap every instance of 'www.micosoft.com' to be replaced with 'www.yahoo.com'.

exaclty who would this be possible in your opinion.

sorry for being such a novice.

rgs

Newboy

Reply to newboy

5

 SenHu, on Sep 28, 2009 1:56:44 pm BST

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

Reply to SenHu