Solved

Batch to write to .file

Posted on 2014-01-26
13
668 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
  • 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
 
LVL 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now