calling bat file using asp classic - batch-file

Can anyone help me with this problem, i need to call/execute bat file. below are my codes for bat file and asp classic.
test.bat
echo off
start C:\inetpub\wwwroot\Texting\Notification.lnk
That test.bat will run a shortcut and call the application, that will update received file.
sample.asp this code is for classic asp
<%
dim fs,tfile,loc,locadd,loctime1,nameFile,wshell
locadd = Month(Date)&Day(Date)&Year(Date)
loctime1 = stripNonNumeric(FormatDateTime(Now,3))
nameFile = "\\85new\Accts85new\Texting\Reply\Karing\"
loc = "\\85new\Accts85new\Texting\Reply\Karing\"&locadd&"_"&loctime1&".txt"
set fs= Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(loc)
For Each Item in Request.QueryString
tfile.Write Item &";"& Request.QueryString(Item)&"$&$"
next
tfile.close
set tfile=nothing
set fs=nothing
Function stripNonNumeric(inputString)
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = "\D"
stripNonNumeric = regEx.Replace(inputString,"")
End Function
set wshell = CreateObject("WScript.Shell")
wshell.Run "cmd.exe /c C:\inetpub\wwwroot\Texting\test1.bat"
set wshell = nothing
%>
that sample.asp will get the value of parameters from querystring.
The code is working, no error received when i hit ENTER on the browser.
Thank you in advance!

Typically this should be done in another way, but assuming you understand the security implications of calling bat file from a web application, you need to try to redirect output to a file to ensure it is executed and what is the output, i.e.
wshell.Run "test1.bat>debug.txt"
CreateObject("WScript.Shell") is executed in a separate process and you will not get any error in the browser, that's why your bat file needs to output its result somewhere.

Related

VbScript to launch URL in default browser from batch file argument

I have a batch script MyBatch.bat, it successfully calls MyVbScript.vbs by the statement:
cscript //nologo %~dp0\MyVbScript.vbs %NewPort%
Where NewPort is a environment variable value.
MyVbScript.vbs is getting called successfully (tested) and value of NewPort is passed to VBS successfully (tested) from batch script.
But I am NOT able to generate the URL for launching in default browser. I want to generate the URL like http://localhost:7006/MyWebApplication where 7006 is value of NewPort variable.
MyVbScript.vbs contains:
Option Explicit
Dim wsh
Dim port
Dim myNum
Set wsh=WScript.CreateObject("WScript.Shell")
port = WScript.Arguments(0)
'MsgBox port//For testing
wsh.Run " "http://localhost:"&port&"/MyWebApplication" "
'End of VB script
How to generate the URL and launch it in default browser. I don't want to specify Chrome or Mozilla and IE because user may not have a particular browser.
The following worked:
wsh.Run "http://localhost:"&port&"/MyWebApplication"
Any seasoned programmer can suggest other code too if I have written more LOC unnecessarily.

How do i prevent a VBScript from running standalone?

I'm doing a mash between VbScript and CMD, i can call the VBScript easily with
cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt"
But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than it being called through a batch file.
My first attempt was a mess of setting a variable into a text file before i ran cscript.exe and use error handling in VBScript to tell if that variable could be collected, but it added too much time to the script.
Does VBScript have a way to tell whether it was started by CMD, or simply by double clicking, and able to act accordingly?
Here is a simple function, detecting the parent process caption. You can check if the process was started by CMD shell (the caption is cmd.exe) or by double-click (explorer.exe):
If LCase(GetParentProcessCaption()) <> "cmd.exe" Then WScript.Quit
' the rest part of your code here
Function GetParentProcessCaption()
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & CreateObject("WScript.Shell").Exec("rundll32 kernel32,Sleep").ProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
GetParentProcessCaption = .Caption
End With
End With
.Terminate
End With
End Function
In the context of your question another method allowing to pass parameters from CMD shell process to WSH script child process may be useful. It uses environment variable and WScript.Shell object. Consider the below example.
There is code for task.cmd file:
set myvar=myvalue
wscript "%~dp0task.vbs"
And for task.vbs file:
WScript.Echo CreateObject("WScript.Shell").Environment("process").Item("myvar")
I have got the output as follows:
Note, process environment variables are accessible for child processes only.
One way is for your VBS file to check for the presence of parameters and if they do not exist then stop the execution.
In your VBS script:
If WScript.Arguments.Count = 0 then
' No parameters provided. Can stop here.
End If
When you call your VBS file, just passing any parameter will satisfy the condition:
REM This will work.
cscript.exe //NoLogo "%~dp0TASK.vbs" "hello world"
REM So will this.
cscript.exe //NoLogo "%~dp0TASK.vbs" 1 2 3 4
REM This will not.
cscript.exe //NoLogo "%~dp0TASK.vbs"
This will not stop people from running it manually (with a parameter) or creating a shortcut which has a parameter. It would only really stop running the VBS directly (as a parameter will not be passed).
When you double click on a .vbs file, the action is determined by the following registry key:
Computer\HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command
If you were to change the key, you will be changing the double click action, but you will not be affecting your ability to launch the command explicitly via invoking cscript.exe directly.
If the bat file will keep the cmd.exe open while the vbs file runs, you can try to detect the cmd process inside the vbs file to continue execution.
Put this at the start of your vbs file:
Set shell = CreateObject("WScript.Shell")
list_str = shell.Exec("tasklist").stdOut.ReadAll 'get a list of processes by calling the windows program 'tasklist.exe'
If InStr(list_str, "cmd.exe") = 0 Then WScript.Quit 'quit if process is not found

Launch two Explorer windows side-by-side

Is there a way to launch two Explorer windows side-by-side (vertically tiled) with a Batch script?
If not, how might I do this with VBS?
I have modified the VBS script above by Hackoo to do exactly what the OP wants...
The comments in the script explain exactly what it will do.
If the two windows don't set into correct position, increase the 'Sleep' time and try again.
If you want a horizontal split, use 'objShell.TileHorizontally'.
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Launches two Explorer windows side-by-side filling the screen dimensions.
''' Minimizes all current open windows before launch; if this is not done,
''' the current open windows will also be resized along with our two windows.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Calc,AppData,objShell
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
Call Explore(Calc)
WScript.Sleep 800
Call Explore(AppData)
WScript.Sleep 800
objShell.TileVertically
Set objShell = nothing
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
This might be in the same category as your question. :)
How can a batch file run a program and set the position and size of the window?
Unfortunately it seems that its not possible without any external third part software in batch. Probably easier in VBS - if so the answer should be in the link.
Try this code :
Option Explicit
Dim Calc,AppData
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Call Explore(Calc)
Call Explore(AppData)
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************

Writing a string in console through VBScript

I am using the following code snippet
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(".\lib\example.bat")
During this an empty console is getting opened till example.bat is completing it's process, is it possible to display some text in that console??
I have tried echo in example.bat file but no use.
please help me with it
Though displaying text in the console is not possible using exec, its possible to hide the console window using cscript and it worked.

Running a Batch File in the Same Directory from the VBS

I have a VBScript that is supposed to run a .bat or .vbs file, but it doesn't work!
It comes up with an error saying that the file could not be found, whether i put in a file path or not (it shouldn't matter anyway I think because it's in the same directory).
So my question is, how do I start a .bat file (or even better, a .vbs file) from within a VBScript?
The relevant code is bellow:
'*******This is the start of my open command that doesn't work*******
Do
If Hour(Now) >= 9 And Hour(Now) <= 18 And Minute(Now) = 34 And Second(Now) = 59 Then
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "F:\\EAS\Volume Up.vbs"
Set shell = Nothing
MsgBox "My De-bug Message Box which doesn' even get to open"
WScript.Sleep 2000
Set WshShell = CreateObject("WScript.Shell")
music = "C:\...\MYFILE.wav"
WshShell.Run "wmplayer """ & music & """", 0, True
WScript.Quit 1
Else
'*******This is the end*******
So what am I doing wrong? Is it the wrong way to open it? What should I put instead?
It would be good to see the contents of the .BAT File.
Your code seems fine as I am able to run the below script on my machine:
dim shell
set shell=createobject("wscript.shell")
shell.run "tester.bat"
You may not see what the .BAT File is doing as it happens so quickly, as a tester add the following command to the end of your .BAT Script:
pause
Then you will see the command prompt open. As per my VB code above, the .BAT file contents are below:
#echo OFF
#echo %time%
pause
This will show you the current time and then pause, leaving the command prompt open. Give this a go as a tester as it works fine for me.
putting triple quotes (as suggested by ToThePoint) around the path solved my vbs file error, where it was failed to find the file on specified path as file path was having spaces, like
D:\Main\My text Files\abc.txt.
thanks :)
The only thing i can think of is that it must be a typo in the file name.
Can you ensure the file name is spelled correct?
Or else please post the exact error you get.

Resources