Jump to content

[Help required] Add string within file


Recommended Posts

Hey ho,

I also need a solution for a similar problem:

I have a file with 610 lines of code. In exactly line 347 I have to add some characters between "><" :

<DATA Type="xsd::string">%MYOWNCHARACTERS%</DATA>

Can you give me the code I need for a batch-file to do this job?

I'm not so familiar with the commands in batch to do this job, and know I have to study it B)

Thank you very much

Edited by Yzöwl
Strikethrough added , Topic split from another!
Link to comment
Share on other sites


[uPDATE]

I resolved the problem and found a solution for that.

I'm now using "sed.exe" from the "UnxUtils" to find the string and replace it by another.

But it would be very nice to know how to do this without any 3rd party tool... :yes:

Edited by HØLLØW
Link to comment
Share on other sites

The problem with batch is that the file contains redirection symbols <> and % signs, and also quotes ":

this may prove to be a very hard problem to get around.

Mind you, it may be possible to do it in pure batch, but definetly NOT worth the effort.

To get an idea of the complexity involved, check this:

http://www.robvanderwoude.com/xtreme.php

http://www.robvanderwoude.com/files/cvturlnt_nt.txt

The use of an external program is far better/easier/foolproof, besides sed, I am personally fond of gsar:

http://home.online.no/~tjaberg/

jaclaz

Link to comment
Share on other sites


@echo off
setlocal ENABLEDELAYEDEXPANSION
if "%1"=="" goto syntax
if "%2"=="" goto syntax
if "%3"=="" goto syntax
if "%4"=="" goto syntax
set filename=%1
set destfile=%2
set line=%3
set value=%4

set /a nbl=0
if exist %destfile% del /q /f %destfile%
for /f "delims=" %%i in (%filename%) do (
set /a nbl+=1

if !nbl! == %line% (echo ^<DATA Type="xsd::string"^>%value%^</DATA^> >>%destfile%) ELSE (echo %%i >>%destfile%)

)
goto end
:syntax
echo %0 sourcefile destinationfile linenumber value
echo will create destinationfile inserting the wanted value between already preset line
goto end

:end
endlocal

With batch limits i didn't found how to make a proper code.

Link to comment
Share on other sites

This should do as you requested:

@ECHO OFF&SETLOCAL
(SET IF_=PATHTO\SOME NAME.EXT)
(SET IS_=%%MYOWNCHARACTERS%%)
(SET LN_=347)
FOR %%# IN (IF LN) DO IF NOT DEFINED %%#_ GOTO :EOF
IF NOT EXIST "%IF_%" GOTO :EOF
FOR %%# IN ("%IF_%") DO (SET OF_=%%~dpn#_NEW%%~x#)
TYPE NUL>"%OF_%"
(SET/A PL_=LN_-1)
MORE +%PL_% "%IF_%"|FINDSTR/VG:"/" "%IF_%">>"%OF_%"
ECHO=^<DATA Type="xsd::string"^>%IS_%^</DATA^>>>"%OF_%"
MORE +%LN_% "%IF_%">>"%OF_%"

Just change:

Line 2 to the input file name

Line 3 to the characters you wish to insert.
(remembering to escape any 'poison' characters)

Line 4 to the line number you're wishing to add your text to.

Line 11 any further changes to the existing lines text.
(once again remembering to escape any 'poison' characters)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...