Solved

Batch to write to .file

Posted on 2014-01-26
13
724 Views
Last Modified: 2014-02-11
Hi,

I am looking for help with writing a simple batch file which would write text to a .file file.

My test.file file looks like this:

abc
abcd
abcde
163tre
2193

The batch file should write the word "treble349" to line 4 in the test.file file.

Thanks
0
Comment
Question by:cgruber
[X]
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
  • 7
  • 5
13 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39811193
as this is a simplified example and before we write in batch what is really going to be in this file. If there is any chance of special to batch character in there such as "&()^><!% etc. then they need special handling or can mae it impossible or very difficult to keep everything exactly the same.

A VBscript which you can run from batch if wanted can read any text file without any handling issues.

if the file is a simple format though are you wanting always, line 1-3 of file a, then your extra line,  then line 4 onwards?

Steve
0
 

Author Comment

by:cgruber
ID: 39811643
The test.file will always contain letter and numbers only (a-z and 0-9).

treble349 should always be inserted in test.file at line 34 (sorry 4 was a typo)

test.file will always have at least 100 lines

It can be a VB script as well, as long as I can then convert it to .exe easily enough.

PS: What I am trying to do here is write a script that will write the word treble349 to test.file, then run an application called summit.exe, then after 300 seconds delete the word treble349 from test.file
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39811699
Another question I should have asked, sorry is whether blank lines matter.  In case they do have used a FIND command to number the lines, then process them as using a FOR loop by itself and counting lines it ignores blank ones.

[Edit: amended as left it replacing line 34 rather than inserting at line 34.  The line with ECHO in capitals sends the current line 34 aswell as the new one]

Steve

@echo off
setlocal enabledelayedexpansion

set file=c:\users\stephen\list.txt
set newline=MY new line 34
set insertline=34

REM Use Set Output=Con to send to screen without amending code, or set it to a filename to make a new file
set output=C:\users\stephen\output.txt

(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
  if "%%~a"=="%insertline%" (
    echo %newline%
    ECHO.%%b
  ) ELSE (
    echo.%%b
  )
)) > %output%

REM Shows output file, remove if wanted
START "" "%output%"

Open in new window

0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 55

Expert Comment

by:Bill Prew
ID: 39811895
This line is troubling me.  Neither BAT scripts, or VBS scripts can be readily converted to EXE files without the use of a third party utility for that purpose, and even those can be problematic sometimes.  Did you really mean that you want the solution here to end up as an EXE file?
It can be a VB script as well, as long as I can then convert it to .exe easily enough.
~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39811977
Hmm, didn't see that bit Bill.

It can be a VB script as well, as long as I can then convert it to .exe easily enough.

PS: What I am trying to do here is write a script that will write the word treble349 to test.file, then run an application called summit.exe, then after 300 seconds delete the word treble349 from test.file

To remove the word is relatively easy using another find command to exclude that line, or just revert to the other file?  Does the file change at all when the process runs?

del list.old
rename list.txt list.old
rem process as above using list.old into list.txt to add extra line
summit.exe
del list.txt
rename list.old list.txt

Steve
0
 

Author Comment

by:cgruber
ID: 39812463
Hi,

Yes was planning to use a 3rd party tool such as bat_to_exe_converter.zip

No Steve, file will not change.
0
 

Author Comment

by:cgruber
ID: 39812468
In your example you use .txt files, where as my file is a .file file.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39812492
Unless you really have to why convert to EXE?
0
 

Author Comment

by:cgruber
ID: 39813155
I guess converting to .exe is not a must.

But then what I would need to achieve is have the script file run automatically when cmd.exe is opened on the computer, and do the following steps:

- insert treble349 at line 34 in test.file
- run summit.exe
- after 300sec delete treble349 from test.file

Also the whole process should be hidden from the user.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39813280
okwell then i think batch is probably a bit silly, best we can do really is minmise, you can soon start a batch file from startup, login script or anything else mind.  Depending upon what this apps is, what it has to do, i.e. does it need to run as the user in their profile, or is as computer ok -to i.e. can run as startup script before user logs in from registry or active directory group policy etc.

also does 'summit.exe' show any user interface?  Is that a console command or a gui app?

Feel like you are (naturally) leaving all the detail out and as you drip feed it through to realise a different solution would be needed.

i.e. what sounded relatively easy gets more complicated, goes off at a tangent about making an exe and now want to run silently.

I would suggest bet then is a VBscript which you call as wscript.exe myapp.vbs and see no command window etc.

Steve
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 39813361
cgruber.  before i or anyone else writes any more code I suggest you decide and write down complete requirement list as otherwise we just waste our time writing in one language only to find you want it do something which isnt possible.

if you want an exe anyway frankly best to write it in something that can be compiled, e.g. autoit is good for that.

I havent got any more input into this q. at the moment, not got time to write another script and IMO answered your original question and first few twists..
0
 

Author Comment

by:cgruber
ID: 39813712
In your example you use .txt files, where as my file is a .file file. Does that matter?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39851346
cgruber - didn't see your last comment.  If you had tried the script you would have found it doesn't care what the filename is.  If it is a text file then the name doesn't matter.  If the file isn't a plain text file you can view in Notepad cleanly then that is a different approach altogether.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

726 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