|
-
Oct 18th, 2013, 02:48 PM
#1
Thread Starter
New Member
Working with batch files
Hello. I want to make a program in VB6 so that if you click a button the program asks you in a textbox for user input then it saves the input as a line in a batch file and runs it. Can anyone please help me? It isn't working. Thank you in advance.
Code:
Private Declare Function ShellExecute Lib _
"shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal _
lpParameters As String, ByVal lpDirectory _
As String, ByVal nShowCmd As Long) As Long
Const SW_HIDE = 0
Const SW_SHOWNORMAL = 1
Const SW_NORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_MAXIMIZE = 3
Const SW_SHOWNOACTIVATE = 4
Const SW_SHOW = 5
Const SW_MINIMIZE = 6
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNA = 8
Const SW_RESTORE = 9
Const SW_SHOWDEFAULT = 10
Const SW_MAX = 10
Private Sub Command1_Click()
usercommand = InputBox("Enter the command")
Dim intFF As Long
intFF = FreeFile
Open "C:\start.bat" For Output As intFF
Print intFF, "somecodehere"
'Close intFF
Dim intFF2 As Long
intFF2 = FreeFile
Open "C:\command.bat" For Output As intFF2
Print intFF2, "somecodehere"
' something
'Close intFF2
Dim intFF3 As Long
intFF3 = FreeFile
'Open "C:\command.bat" For Append As intFF3
Print intFF3, "usercommand"
ShellExecute 0&, vbNullString, "t", vbNullString, _
"C:\=start.bat", SW_SHOWDEFAULT
't = Shell("C:\start.bat")
ShellExecute 0&, vbNullString, "z", vbNullString, _
"C:\=command.bat", SW_SHOWDEFAULT
'z = Shell ("C:\command.bat")
Dim intFF4 As Long
intFF4 = FreeFile
Open "C:\=drive.txt" For Input As intFF4
MsgBox (intFF4)
Close intFF
Close intFF2
Close intFF3
End Sub
-
Oct 18th, 2013, 03:45 PM
#2
Re: Working with batch files
Well first off you have to put a # in front of your file number
Second, I have no idea what you are writing to the files
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Oct 18th, 2013, 03:45 PM
#3
Re: Working with batch files
Do you really need to write the batch script to disk? How about executing it "in-memory" like this?
Code:
Shell "CMD.EXE /C @TITLE File-less BAT Script & @ECHO Hi %UserName%! & @ECHO. & @ECHO You typed """ & InputBox("Enter the command") & """ & @ECHO. & PAUSE", vbNormalFocus
The /C switch "Carries out the command specified by the string and then terminates". The @ character prevents echoing the issued command to the standard output. The & character concatenates multiple commands together. Environment variables are denoted by %EnvVarName% and can be declared via the SET command.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 09:38 AM
#4
Thread Starter
New Member
Re: Working with batch files
Yes I need to because the program adds the user command to the batch and then runs it. It needs other program to run, that's why I work with batch files. Can you please help me fix my software? Thank you.
-
Oct 19th, 2013, 10:08 AM
#5
Re: Working with batch files
 Originally Posted by cristyro
Can you please help me fix my software?
Which one? The code in post #1?
Can you please describe what you're attempting to do in more detail? Also, can you show the real code you have written so far? Your code in post #1 looks like just an example of what you're trying to achieve.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 10:21 AM
#6
Thread Starter
New Member
Re: Working with batch files
That's my only code. I am trying to make a program that does this: it shows a button, and if you click it then it asks you (in a VB6 box, not CMD line) for a command. That command gets saved in a file, and the other program (not mine) uses it (sends the SCSI command to the drive). There is already a batch file which runs that program, but that batch file needs to be updated by this software with the user specified command and then it must be started in order to make that other program (not mine) to send the SCSI command.
-
Oct 19th, 2013, 10:31 AM
#7
Hyperactive Member
Re: Working with batch files
 Originally Posted by cristyro
shows a button, and if you click it then it asks you (in a VB6 box, not CMD line) for a command. That command gets saved in a file
Code:
Private Sub Command1_Click()
Dim something
something = InputBox("What is the command ?", "Command to Enter")
MsgBox "you entered " & something '''' or 'save it where ever
End Sub
something like that ?
-
Oct 19th, 2013, 01:28 PM
#8
Re: Working with batch files
 Originally Posted by cristyro
It isn't working.
It's probably because you were implicitly invoking the Form's Print method instead of specifying the Print # statement.
Code:
Option Explicit
Private Sub Command1_Click()
Dim FN As Integer, strUserCmd As String, strBuffer As String
strUserCmd = InputBox("Enter the command")
FN = FreeFile
Open "C:\start.bat" For Output As FN
Print #FN, "somecodehere"
Close FN
FN = FreeFile
Open "C:\command.bat" For Output As FN
Print #FN, "somecodehere"
Close FN
FN = FreeFile
Open "C:\command.bat" For Append As FN
Print #FN, strUserCmd
Close FN
Shell "C:\start.bat", vbNormalFocus
Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
FN = FreeFile
Open "C:\drive.txt" For Input As FN
strBuffer = Input$(LOF(FN), FN)
MsgBox strBuffer, vbInformation
Close FN
End Sub
BTW, next time, don't just say "It isn't working". We generally have no idea what you mean by that. Instead, always be specific and state exactly what isn't working.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 01:42 PM
#9
Frenzied Member
Re: Working with batch files
another point what are you doing with it .somecodehere" i would suggest take one string type variable .and store data in it .together with this please use err handler . so that you will get identify issue .
Code:
Option Explicit
Dim FN As Integer, strUserCmd As String, strBuffer As String
Private Sub Command1_Click()
strUserCmd = InputBox("Enter the command")
FN = FreeFile
Open "C:\start.bat" For Output As FN
Print #FN, "somecodehere"
Close FN
FN = FreeFile
Open "C:\command.bat" For Output As FN
Print #FN, "somecodehere"
Close FN
FN = FreeFile
Open "C:\command.bat" For Append As FN
Print #FN, strUserCmd
Close FN
Shell "C:\start.bat", vbNormalFocus
Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
FN = FreeFile
Open "C:\drive.txt" For Input As FN
strBuffer = Input$(LOF(FN), FN)
MsgBox strBuffer, vbInformation
Close FN
End Sub
BTW
-
Oct 19th, 2013, 01:51 PM
#10
Thread Starter
New Member
Re: Working with batch files
There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.
-
Oct 19th, 2013, 01:58 PM
#11
Re: Working with batch files
 Originally Posted by firoz.raj
another point what are you doing with it .somecodehere" i would suggest take one string type variable .and store data in it
I think it's just a placeholder for the actual batch script.
firoz.raj
Why did you copy the code I've posted? This is the 2nd time you've done that.
I think you've been warned before not to plagiarize other member's posts. Why do you keep doing that?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 01:59 PM
#12
Frenzied Member
Re: Working with batch files
There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.
just put breakpoint at command1_click . and debug step by step .
-
Oct 19th, 2013, 02:02 PM
#13
Re: Working with batch files
 Originally Posted by cristyro
There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.
Can you show the code for that?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 02:04 PM
#14
Thread Starter
New Member
Re: Working with batch files
I don't have the code for that app, it isn't written by me. My program just has to save a customized batch which will run that program and that program will send the scsi command.
-
Oct 19th, 2013, 02:07 PM
#15
Re: Working with batch files
 Originally Posted by cristyro
I don't have the code for that app, it isn't written by me. My program just has to save a customized batch which will run that program and that program will send the scsi command.
I mean, where is your code that shells that other app? How are you calling it?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 02:10 PM
#16
Thread Starter
New Member
Re: Working with batch files
At the start:
Code:
strUserCmd = InputBox("Enter the command")
FN = FreeFile
Open "C:\start.bat" For Output As FN
Print #FN, "start nameofthatprogram"
Close FN
The "nameofthatprogram" is that program. It runs in cmd prompt.
-
Oct 19th, 2013, 02:21 PM
#17
Re: Working with batch files
So, you are starting the program but you are not passing any command line parameters to it? I think the behavior you mentioned is, indeed, to be expected:
 Originally Posted by cristyro
It just opens and closes fast, like when ran directly from a folder ...
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 19th, 2013, 02:26 PM
#18
Thread Starter
New Member
Re: Working with batch files
I do pass command to it. Maybe the two from the code need to be ran at the same time? The first one starts the program and the second one sends the parameters to it...
-
Oct 19th, 2013, 02:33 PM
#19
Frenzied Member
Re: Working with batch files
can you try the following way ? .why are you not using strusercmd after getting input .
Code:
strUserCmd = InputBox("Enter the command")
FN = FreeFile
Open "C:\start.bat" For Output As FN
write #FN, strUserCmd 'cmd name will be storedstrusercmd
Close #FN
together with this close statement also needs # sign in before . i would really suggest please debug step by step .
after keeping break point at command1_click.
Code:
Option Explicit
Dim FN As Integer, strUserCmd As String, strBuffer As String
Private Sub Command1_Click()
strUserCmd = InputBox("Enter the command")
FN = FreeFile
Open "C:\start.bat" For Output As FN
Print #FN, "somecodehere"
Close #FN
FN = FreeFile
Open "C:\command.bat" For Output As FN
Print #FN, "somecodehere"
Close #FN
FN = FreeFile
Open "C:\command.bat" For Append As FN
Print #FN, strUserCmd
Close #FN
Shell "C:\start.bat", vbNormalFocus
Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
FN = FreeFile
Open "C:\drive.txt" For Input As FN
strBuffer = Input$(LOF(FN), FN)
MsgBox strBuffer, vbInformation
Close #FN
End Sub
-
Oct 19th, 2013, 02:40 PM
#20
Re: Working with batch files
 Originally Posted by cristyro
Maybe the two Shell from the code need to be ran at the same time? The first one starts the program and the second one sends the parameters to it...
Well, did you expect two separate instances of the command prompt window to be able to communicate with each other? Of course, they can't. You'll have to launch the app and pass parameters to it using a single batch file ...
OR ...
tell us exactly what the contents of the batch files you have now and I'll see if we can get rid of those batch files by using the technique in post #3.
If you will expose more of what you're trying to do, better approaches/solutions can be suggested.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Oct 20th, 2013, 08:31 AM
#21
Re: Working with batch files
On another note you should not be opening the file more than once here
Code:
FN = FreeFile
Open "C:\command.bat" For Output As FN
Print #FN, "somecodehere"
Close FN
FN = FreeFile
Open "C:\command.bat" For Append As FN
Print #FN, strUserCmd
Close FN
There is no reason to close the file and then open it again here, all that does is makes the code longer and slows down the program
It should be more like this
Code:
FN = FreeFile
Open "C:\command.bat" For Output As FN
Print #FN, "somecodehere"
Print #FN, strUserCmd
Close #FN
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|