Search : in
By :

Script to edit multiple files

Last answer on Nov 18, 2009 1:42:33 pm GMT 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...
Batch Script - Move files to \%date%\%time%\ ShowBatch Script - Move files to \%date%\%time%\ Issue Solution Note Issue I have been trying to make a simple batch script to create database backups and move then into a folder named by date, and a sub folder by time. This is what I...
Cannot find script file [C:WINDOWSsowar.vbs] ShowCannot find script file "C:\WINDOWS\sowar.vbs" Issue Solution Issue Whenever I start my laptop I get: Cannot find script file "C:\WINDOWS\sowar.vbs". I think this is a virus. What should I do to resolve this one? Another thing,...
Download FTP Voyager ShowFTP Voyager can be used to: - maintain and publish web sites - save local and remote files - upload and download music, images, documents, video files and much more - automate routine tasks with scheduler. Moreover, it is possible to...
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...
UNIX - Files ShowIntroduction to UNIX files In UNIX systems any element is represented in the form of a file. All files are architectured around a single tree structure where the base, called the root, is written "/". File types UNIX systems define different...
User management in Windows NT ShowThe notion of a user Windows NT is an operating system which manages sessions, meaning that when the system is started, it is necessary to log in with a user name and password. When Windows NT is installed, the administrator account is created by...

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
  • +2

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

6

 SenHu, on Nov 18, 2009 1:42:33 pm GMT

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

Reply to SenHu