HØLLØW Posted May 19, 2010 Share Posted May 19, 2010 (edited) 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 May 20, 2010 by Yzöwl Strikethrough added , Topic split from another! Link to comment Share on other sites More sharing options...
HØLLØW Posted May 20, 2010 Author Share Posted May 20, 2010 (edited) [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... Edited May 20, 2010 by HØLLØW Link to comment Share on other sites More sharing options...
jaclaz Posted May 20, 2010 Share Posted May 20, 2010 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.phphttp://www.robvanderwoude.com/files/cvturlnt_nt.txtThe 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 More sharing options...
allen2 Posted May 20, 2010 Share Posted May 20, 2010 @echo off setlocal ENABLEDELAYEDEXPANSIONif "%1"=="" goto syntaxif "%2"=="" goto syntaxif "%3"=="" goto syntaxif "%4"=="" goto syntaxset filename=%1set destfile=%2set line=%3set value=%4set /a nbl=0if 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:syntaxecho %0 sourcefile destinationfile linenumber valueecho will create destinationfile inserting the wanted value between already preset linegoto end:endendlocalWith batch limits i didn't found how to make a proper code. Link to comment Share on other sites More sharing options...
HØLLØW Posted May 21, 2010 Author Share Posted May 21, 2010 Ok, thank you guys. Link to comment Share on other sites More sharing options...
Yzöwl Posted May 21, 2010 Share Posted May 21, 2010 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 :EOFIF NOT EXIST "%IF_%" GOTO :EOFFOR %%# 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 nameLine 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now