Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do you insert given text into a line in a text file using a batch script?

Posted on 2010-11-10
8
498 Views
Last Modified: 2012-05-10
Greetings,

I have a batch file that grabs a file off of an ftp server, then deletes a given line based off a search string using type and find.

type in.txt | fine /V "<searchstring>">out.txt

I want to take that out.txt and insert text into one of the lines now, for example:

lineinformation1|moreinfo1|info1|somerandomnumber1
lineinfomation2|||somerandomnumber2
lineinformation3|moreinfo3|info3|somerandomnumber3

and afterwards i want the line to look like:

lineinformation1|moreinfo1|info1|somerandomnumber1
lineinfomation2|insertedinfo|moreinsertedinfo|somerandomnumber2
lineinformation3|moreinfo3|info3|somerandomnumber3

is there any not too complex way to write this?
0
Comment
Question by:jedininja
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 34106591
Some questions that might make a difference in the code:

1. Is the "lineinformation2" always the same?
2. Do the "|" actually exist as shown?

0
 

Author Comment

by:jedininja
ID: 34106684
@pony10us

yes, the informationline2 is always the same, so is the "insesrtedinfo"

also the | are in as shown. i've been trying this:

for /F "token=*" %%l in (myfilename.txt) do (
   set line=%%l
   set line=!line:"|||"="|insertedinfo|moreinsertedinfo|"!
   echo !line!>> newmyfilename.txt
)

but its not changing the line, also tried it with the full line name in it.
0
 

Author Comment

by:jedininja
ID: 34112979
ended up going with a vb script and running it in my batch.


'replace.vbs
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\TEMP\oldfilename.txt")

OldData = "|||"
NewData = "|newinfo|moreinfo|"

arrData = Replace(objFile.ReadAll, OldData, NewData)
objFile.Close
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\TEMP\newfilename.txt")
objOutput.Write arrData
objOutput.Close

Set objOutput=Nothing
Set objFile=Nothing
Set objFSO=Not

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 16

Accepted Solution

by:
t0t0 earned 250 total points
ID: 34119146
Try this:

Save the code as PROC.BAT and call it like this:

   PROC in.txt > out.txt

or

   PROC "in.txt" > "out.txt"




@echo off
setlocal enabledelayedexpansion

for /f "tokens=* usebackq" %%a in ("%~1") do (
   set line=%%a
   set line=!line:^|^|^|=^|insertedinfo^|moreinsertedinfo^|!
   echo !line!
)

exit /b
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34119874
@t0t0, looks good.  

@pony10us, are you watching :-).

Only thing I would mention is that this assumes that there aren't any "odd" characters in the existing file data, or the new data to be inserted, occasionally certain special characters can be mishandled by BAT, whereas they typically are handled okay in VBS.

~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 34124089
@jedininja - I was thinking about VBS

@t0t0 - your code is much cleaner than what I was working on but I was trying to do it without the user having to input anything. Of course I was not able to finish it.

@billprew - of course I am watching.  :)   I almost had something but was trying to use an IF inside a FOR /f and it wouldn't work.  Any ideas?  

Your comments make good sense as usual    :)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 34126838
--> bill

There was no mention of 'odd' characters in the question, other than the pipe symbol of course.

The 'file data' is taken literally from the example given in the question by the asker.

The solution i provide above works with the data provided.

I would be quite happy to amend the code, if possible, to handle other requirements where special characters are concerned however, this may not be an issue we need to concerned ourselves with.

VBS does provide advantages over DOS and maybe, keeping in flavour with DOS, the VBS script could be generated from within the same batch file thereby not requiring a 'seperate' external file.


--> pony10us

Your comments are noted.
0
 

Author Closing Comment

by:jedininja
ID: 34136117
t0t0 strikes again :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copy XML file and strip off certain elements 4 53
regex to extract ip:john 17 74
Date variable in batch file 2 44
how to count files? 4 30
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question