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
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.
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.
Re: Working with batch files
Quote:
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.
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.
Re: Working with batch files
Quote:
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 ?
Re: Working with batch files
Quote:
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.
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
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.
Re: Working with batch files
Quote:
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?
Re: Working with batch files
Quote:
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 .
Re: Working with batch files
Quote:
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?
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.
Re: Working with batch files
Quote:
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?
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.
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:
Quote:
Originally Posted by
cristyro
It just opens and closes fast, like when ran directly from a folder ...
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...
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
Re: Working with batch files
Quote:
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. ;)
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