Why Vbs show this message [duplicate] - batch-file

This question already has answers here:
Why oShell.Run not work
(2 answers)
Closed 6 years ago.
This code work fine
set oShell = CreateObject ("WScript.shell")
eAppData = oshell.ExpandEnvironmentStrings("%appdata%")
wscript.echo Appdata
oshell.run(Appdata & "\Test.bat"),0,False
This .vbs in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup
But when windows start show this message why ?
The Test.bat is in %appdata%\Test.bat

And when you get rid of this line : wscript.echo Appdata
This code will come like that if you don't want to display this message again
set oShell = CreateObject ("WScript.shell")
AppData = oshell.ExpandEnvironmentStrings("%appdata%")
oshell.run(Appdata & "\Test.bat"),0,False

You're using wscript.echo Appdata, which will cause this box to pop open containing the shown folder path.

Related

run a vbscript from usb "AS ADMIN" and get drive letter to ru batch script stored in usb drive [duplicate]

This question already has answers here:
Getting current directory in VBScript
(9 answers)
VBscript relative path
(3 answers)
Closed 8 months ago.
I have following vbscript:
set objShell = Createobject("wscript.shell")
objShell.Run """Scripts\Rec.bat""", 0
objShell.Run """Scripts\Prod.bat""", 0
Set objShell = Nothing
The above vbscript is stored in USB drive and i am trying to launch it from usb.
Now problem is when i normally run this vbscript it executes. But when i do run as administrator from context menu following error occurs "System cannot find the file specified"
I tried to debug the problem using another below script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
Wscript.Echo strDrive
When i run it normally it echo F: that is my usb drive letter.
But when i run as admin it echo C:
So the batch file location is being searced to C:\Scripts\Rec.bat instead of F:\Scripts\Rec.bat thats why this error is comming.
Can anyone help??
I want to run vbscript as admin and still retain the usb drive letter path because the code in my Rec.bat requires admin privilege
You don't want it to be relative to the current directory, you want it to be relative to the directory of the running vbscript:
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = "" & strFolder & "\Scripts\Rec.bat" & ""
objShell.Run strPath, 0
If you want to try it with a test script, you'd be advised to replace 0 on the last line with 1.

Check if a process is closed/not running, then restart the application

Below is the script I am using to detect if my application is being closed. If it is closed the application will restart again. The application should run in the background as I am running this on startup.
run.bat
#echo off
:Restart
start "AppNAME" /wait "C:\Program Files (x86)\App\App.exe"
goto Restart
How can I hide the command windows after running the .bat file?
As per your requirement here is a vbs file that will run the batch file in background.just past the vbs file in startup and the batch file somewhere else.
REM 0 = hide window, 1 = show window (useful for debugging)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """" & "file.bat" & """" & sargs, 0, False
Set WshShell = Nothing

How can i run a batch file hiddenly using shell script? [duplicate]

This question already has an answer here:
Prevent VBscript app from showing Console Window
(1 answer)
Closed 2 years ago.
I am working on shell script/ .vbs extension file, and I want to run a batch file hiddenly or in background from shell script. I am mentioning my code but its not executing a batch file. I have saved my file with a name test.vbs.
Set WshShell = CreateObject()
WshShell.Run chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Set WshShell = Nothing
Thanks in advance.
when you write CreateObject(), you have to mention in those circle brackets. Your code looks fine just add WScript.shell in circle brackets like this.
Set WshShell = CreateObject("WScript.shell")
You need CreateObject("WScript.Shell"), not CreateObject():
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Otherwise VBScript wouldn't know what you want to create! So it can't know you want the shell object that has the Run method...

How to hide a command prompt while running a batch file in java

Okay so I am making a game and have fixed most of my problems there is just one thing, the cmd prompt is annoying me when it pops up. If you have any recommendations on how to have the prompts functions run without it opening the prompt itself.
This is an answer from #badbod99 for the question How can I run a windows batch file but hide the command window?:
Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("mybatch.bat", null, objConfig, intProcessID)
In the last line, change myfile.bat to the file location of your batch file. Save this as a .vbs file and run it instead of the batch file.
The batch file part:
#echo off
cls
start "" "C:\directory\yourfile.java"
exit
Save that as a .bat file. This is the batch file that will be run by the .vbs file.

Passing variable from vbs to batch file [duplicate]

This question already has answers here:
Passing-variable-from-vbscript-to-batch-file with arguments
(2 answers)
Closed 9 years ago.
I am trying to pass a string that is contained in a variable named sDateFile into test.bat using the following line:
WshShell.Run "test.bat sDateFile"
And to check whether test.bat has received the variable. I get test.bat to do the following:
echo %1
But my output is sDateFile, not the string in the variable.
Am I approaching my problem incorrectly? Would there be a better way to approach it?
You have to concat your bat file and the value of sDateFile. There is a SPACE after test.bat
WshShell.Run "test.bat " + chr(34) + sDateFile + chr(34)
Run command

Resources