• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 850
  • Last Modified:

Batch to write to .file

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
cgruber
Asked:
cgruber
  • 7
  • 5
1 Solution
 
Steve KnightIT ConsultancyCommented:
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
 
cgruberAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bill PrewCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
cgruberAuthor Commented:
Hi,

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

No Steve, file will not change.
0
 
cgruberAuthor Commented:
In your example you use .txt files, where as my file is a .file file.
0
 
Steve KnightIT ConsultancyCommented:
Unless you really have to why convert to EXE?
0
 
cgruberAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
cgruberAuthor Commented:
In your example you use .txt files, where as my file is a .file file. Does that matter?
0
 
Steve KnightIT ConsultancyCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now