Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a Batch File in ASP - help

Status
Not open for further replies.

tmourad

MIS
Mar 19, 2003
3
US
I have this code which creates a batch file. What I'm trying to do is close it once it's finished running.

Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set MyFile_bat = fs.CreateTextFile(filename_bat, True)
strLine = "C:\MiniSoft\WS92\ws92_32.exe \\cmr\scripts\mis\minisoft\" & qmake("dt") & ".s92"
MyFile_bat.writeline strLine
<>
MyFile_bat.Close
Set MyFile_bat = Nothing

--------------------------------------------------
If I add a line <>
MyFile_bat.writeline &quot;exit&quot; to close the dos window, it doesn't work. However putting some other command e.g &quot;cls&quot;, does work.

Any ideas on doing this? Do I need to create a PIF file - if so how.....what... :)

thanks.
 
tmourad,

First reaction is that your syntax for creating:

Set MyFile_bat = fs.CreateTextFile(filename_bat, True)

is incomplete or incorrect.

My check of for the syntax for CreateTextFile is:

object.OpenTextFile (filename [, iomode[, create[, format]]])

You did not include an the iomode or a comma to go with the default. You sample code included the filename and the create.

Near as I can tell you are closing =NOTHING ok.

Try this.

If this fails, save the file as a .VBS

Assuming you have Microsofts Script Debugger installed run the script with:

cscript scriptname.vbs //X

Then run it and see where it blows up.

I did and it failed at the point I noted above. Had to mod your code a bit to work as a .VBS script, which looks like:

vvvvvvvv

dim filename_bat
dim MyFile_bat
dim strLine
filename_bat = &quot;c:\cmdfiles\tst.txt&quot;
Set fs = CreateObject(&quot;scripting.FileSystemObject&quot;)
Set MyFile_bat = fs.CreateTextFile(filename_bat, 8, True)
strLine = &quot;C:\MiniSoft\WS92\ws92_32.exe \\cmr\scripts\mis\minisoft\&quot; & &quot;dt&quot; & &quot;.s92&quot;
MyFile_bat.writeline strLine

MyFile_bat.Close
Set MyFile_bat = Nothing

^^^^^^^^^^^^

I did not have access to your QMAKE program so it failed until I removed it.

Point is, if you run into problems, the script debugger can help narrow it down. Not the &quot;best&quot; but it will atleast get you to the line. Also, you get some error display running this from the command line.

Hope this helps.

DougCranston
 
Thanks for responding doug.

Here's a sample code for CreateTextFile from DevGuru

Code:
<%
Dim filesys, testfile
Set filesys = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, True)
testfile.WriteLine &quot;Your text goes here.&quot;
testfile.Close
%>
------------------------------------------------------
so i'm not sure why mine is incomplete..it looks the same to me.
I'm not getting any errors either. It does work perfectly, I just can't seem to close the window.
A colleage suggested I might need to create a PIF file. -tina
 
tmourad,

Your problem is in the following line:

Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, True)

SHOULD READ

to accept default (note extra comma)
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, , True)

to go with WRITE put a 2 (Opens a file for writing. If the file already exists, the contents are overwritten.)
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, 2, True)

to go with WRITE put a 8 (Opens a file and starts writing at the end (appends). Contents are not overwritten. )
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, 8, True)

For details on this, see:

Hope this helps clear it up.

DougCranston
 
Correction..

The last option should read:

to go with APPEND put a 8 (Opens a file and starts writing at the end (appends). Contents are not overwritten. )
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, 8, True)

APPEND instead of WRITE

Sorry for any confusion.

DougCranston
 
Hmm... some confusion here. Also I hope that &quot;example&quot; wasn't really published like that someplace. But maybe they assume people know better than to literally do what the example says.

You need to dispose of object references when you are through with them. In general the sooner the better.

It ought to read like:
Code:
<%
Const OverWrite = True
Dim filesys, testfile 
Set filesys = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) 
Set testfile= filesys.CreateTextFile(&quot;c:\somefile.txt&quot;, OverWrite) 
testfile.WriteLine &quot;Your text goes here.&quot; 
testfile.Close
Set testfile = Nothing
Set filesys = Nothing
%>
If you create a lot of bulky objects and you get a lot of hits, your ASP pages can eat a lot of memory. So even though by default
Code:
Server.CreateObject( )
references have only page scope (ASP will clean up the leftovers when the page completes) you still should get into the habit of freeing them up as soon as you know you are done with them. This is especially important with pooled objects such as the various ADO objects (connections, recordsets, etc.) and anything from an MTS package. For database connections you also tie up resources on the database server by keeping them around longer than you need to, and it only gets worse when you have transactional connections.

We aren't talking about opinion here. Code written right simply runs much better.

dougcranston is probably thinking of another FSO method such as
Code:
OpenTextFile( )
which does have a second parameter
Code:
iomode
. But the second parameter to the
Code:
CreateTextFile( )
method is called
Code:
overwrite
and a value of
Code:
True
means to overwrite any existing file of the same name.

You'll find VBScript and the FSO fully documented at:


No single source is perfect, but I'd start with the people who built the technology first.

Sorry to come off in &quot;lecture mode&quot; here. I've been cleaning up nasty ASP pages at work all week.
 
dilletante, thanks. i realized there was confusion between CreateTextFile and OpenTextFile...

i am closing the objects once i'm done with them...
however, although my script is fine still can't close the MS-DOS window which I assumed would close if i had

testfile.WriteLine &quot;exit&quot; as the last line.
What happens is that the command &quot;exit&quot; is written, but the window is still open with title &quot;Finished ...&quot;. It's just not executing it..or if it is executing it, it's not working.
if i had testfile.Writeline &quot;cls&quot;, it does work. Frankly, it seems ANY command other than &quot;exit&quot; works! e.g.
testfile.WriteLine &quot;CD C:\&quot; etc...


grrr... -tina
 
Maybe I'm blind but...

How are you starting this batch file once it's been created?

Is this script really in an ASP page or have we jumped to a conclusion about that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top