We help IT Professionals succeed at work.

clear autoexec.bat listings

stnic
stnic asked
on
Hello :) I have the following code that writes to the autoexec.bat file when you click a button. I need the code modified so that it will also remove the listings from the autoexec.bat file when you click another button:

Dim fnum As Integer
Dim txt As String
    fnum = FreeFile
    Open "C:\autoexec.bat" For Append As fnum
    txt = text1.text
    Print #fnum, txt
    txt = text2.text
    Print #fnum, txt
    txt = text3.text
    Print #fnum, txt
Close fnum
   
Any help would be appreciated !

stnic
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012

Commented:
Perhaps:
If Len(Dir("c:\autoexec.bat") Then
   Kill "c:\autoexec.bat"
End If

Or

fnum = FreeFile
Open "C:\autoexec.bat" For Output As fnum
Close fnum

Anthony

Author

Commented:
Hi  acperkins , Little bit confused here ? Wouldn't
If Len(Dir("c:\autoexec.bat") Then
  Kill "c:\autoexec.bat"
End If

erase the autoexec.bat ? Don't want to do that just erase what I placed into it using the text1.text boxes.

Your second example may be on the right track:
fnum = FreeFile
Open "C:\autoexec.bat" For Output As fnum
Close fnum

The problem is I don't understand exactly what the above will do? Do I just change the text1.text boxes to show "nothing" ?

You will have to excuse me as I am a novice in both batch and visual basic :(

Thank You

stnic

Richie_SimonettiIT Operations
CERTIFIED EXPERT

Commented:
You could use start/ending tags inside autoexec file and open it like any text file, remove those lines inside tags (searching with  instr function) and save to disk again.
Example:


'autoexec contents
rem Path=%path%
rem ****** start list
1
2
3
4
rem ***** end list
win



Private Sub Form_Load()
Dim lStart As Integer, lEnd As Integer
Dim strContents As String

Open "c:\autoexec.bat" For Input As #1
     strContents = Input(LOF(1), 1)
Close #1

Debug.Print strContents

lStart = InStr(1, strContents, "rem ****** start list", vbTextCompare)
lEnd = InStr(1, strContents, "rem ***** end list", vbTextCompare)

Dim strTemp As String
strTemp = Mid$(strContents, 1, lStart - 1)
MsgBox strTemp
strContents = Mid$(strContents, lEnd + 18)
MsgBox strContents
strContents = strTemp & strContents
Debug.Print strContents

Open "c:\autoexec.bat" For Output As #1
     Write #1, strContents
Close #1
End Sub
Richie_SimonettiIT Operations
CERTIFIED EXPERT

Commented:
Oops, maybe slowly typing...
CERTIFIED EXPERT
Top Expert 2012

Commented:
You are correct. The first example will erase the file, but then what do you care: empty or non-existant will have the same effect.  You confusion may be that you assume that to do:

Open "C:\autoexec.bat" For Append As fnum

the file must exist.  This is not correct. If the file does not exist the Append mode will create it.

The second example opens it for Output (take a look at the syntax again), so in fact it empties the file out, leaving a 0 byte file.

Pick which ever you think more appropriate.  As a third alternative you can do what Richie is suggesting.

Anthony
Senior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013
Commented:
I think what you want is the ability to restore the autoexec file to its state before you added to it.

Why not do something like

copy autoexec.bat to vb_autoexec.old
insert your stuff
when you are done
copy vb_autoexec.old to autoexec.bat
kill vb_autoexec.old

I know that isn't VB code but I think you can translate it.

good luck
mlmcc

Author

Commented:
Hi mlmcc , You know, its the simple things that we forget. This was a simple answer. Just make a copy of the .bat file and rename it. Later on put it in place of the other one!

Thank you and all who commented !

stnic

Explore More ContentExplore courses, solutions, and other research materials related to this topic.