A little while I asked a question (and got a brilliant response) to create the program below, what I'd like to is silently launch it, so the command prompt does not display in the background.
The program below tests for "Agent.exe" and if it finds it, displays a message box to the user telling them the program will close in 2 minutes unless they press ok.
now
I came across this VBS script
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "Program goes here" & Chr(34), 0
Set WshShell = Nothing
but have not the tiniest clue to combine it with my script
#if (#CodeSection == #Batch) #then
setlocal
for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
setlocal
set "task=agent.exe"
set "timeout=120"
rem // Is %task% running?
tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (
rem // Re-launch script with JScript interpreter
wscript /e:JScript /nologo "%~f0" %timeout% || (
rem // If timeout or user hits No, kill %task%
taskkill /im "%task%" /f
)
)
rem // End main runtime
goto :EOF
rem // Begin JScript portion
#end
var osh = WSH.CreateObject('WScript.Shell'),
nag = 'Greetings! Your administrator has requested you to log out of Program '
+ 'after work. It appears you are still using it.'
+ ' If you are still here, Press Yes to continue working.\n\n'
+ 'Otherwise, press no to close Program.'
+ 'Program will close automatically in less than ' + WSH.Arguments(0) + ' seconds.';
popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
WSH.Quit(popup - 6);
Anyone have any ideas?
(shoutout to Rojo, who answered my initial Question with the above script)
To answer your question, that VBScript snippet will work. Just save it with a .vbs extension and replace "Program goes here" with the path + filename of your batch script. When you double-click the vbs file or execute it with wscript /nologo "path\to\vbsfile", it will run the batch script hidden (no console window) but the popup will still appear.
If you'd prefer to keep all your code within a single file, you could invoke powershell -windowstyle hidden to hide the window. You'll still see the black window for a second, but it'll disappear before the popup appears.
#if (#CodeSection == #Batch) #then
setlocal
rem // hide current window
powershell -windowstyle hidden -command ""
for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
set "task=agent.exe"
set "timeout=120"
rem // Is %task% running?
tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (
rem // Re-launch script with JScript interpreter
wscript /e:JScript /nologo "%~f0" %timeout% || (
rem // If timeout or user hits No, kill %task%
taskkill /im "%task%" /f
)
)
rem // if not in a self-destructing window, re-show
set "caller=%cmdcmdline:"=%"
if /I not "%caller:~0,6%"=="cmd /c" powershell -windowstyle normal -command ""
rem // End main runtime
goto :EOF
rem // Begin JScript portion
#end
var osh = WSH.CreateObject('WScript.Shell'),
nag = 'Greetings! Your administrator has requested you to log out of the Cisco '
+ 'Agent Desktop after work. It appears you are still using it. If you '
+ 'are still here, Press Yes to continue working.\n\n'
+ 'Otherwise, press No to close the agent. Agent will close automatically '
+ 'in less than ' + WSH.Arguments(0) + ' seconds.',
popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
WSH.Quit(popup - 6);
Shout received and appreciated. I still think you ought to play a cheesy midi file instead of beeping. :)
Related
i want to run a .wav-file. This is with
start "" SoundFile.wav
totally practical. But how can i run the window of the standard-windows-soundplayer minimized? So that nothing of my view changes?
start "" SoundFile.wav /MIN
start "" SoundFile.wav /min
start "" SoundFile.wav -MIN
start "" SoundFile.wav -min
didn't work sadly.
Can you help me?
Greets.
Sorry but i am not familiar with stackoverflow, so i can't make it that understandable, but i hope that this is enough.
In a simple way, you could just use a VBS like this:
s = createobject("WScript.Shell")
s.run "music.mp3", 2
Or create an automatic and CLI program to do this:
runmusic.bat
#echo off
set music=%1
set music=%music:"=%
if ":" neq "%music:~1,1%" set music=%cd%\%music%
if "%music:~-4%" neq ".mp3" set music=%music%.mp3
set katorn=%random%
echo >>"%temp%\%katorn%.vbs" set s = createobject("WScript.Shell")
echo >>"%temp%\%katorn%.vbs" s.run "%music%", 2
cscript "%temp%\%katorn%.vbs"
:: Uncomment the next line to autodelete the vbs file.
:: del /q /f "%temp%\%katorn%.vbs"
Usage:
runmusic (name or full path with quotation marks)
In a "professional way" you could do a shortcut file that starts the file minimized.
It could be useful if you needs the musics to have the "artist logo" or something like that, also if you don't want to use an CMD all the time...
Delete all comments to execute.
Set WshShell = CreateObject ("Wscript.Shell")
strDesktop = "X:\Shortcutlocation\yoo"
Set Shortcut = WshShell.CreateShortcut(strDesktop + "\MusicNameMaybe.lnk")
Shortcut.WindowStyle = "7"
Shortcut.IconLocation = "X:\IconPath\youricon.ico" // You can delete this and the file will be the same ever and forever.
Shortcut.TargetPath = "X:\yourpath\music.mp3"
Shortcut.Save
And you don't have to keep the shortcuts on the desktop if you don't want to, you don't even have to keep them fixed. Example:
runmusic.bat
#echo off
set music=%1
set music=%music:"=%
if ":" neq "%music:~1,1%" set music=%cd%\%music%
set music=%music%.mp3
set katorn=%random%
echo >>"%temp%\%katorn%.vbs" Set WshShell = CreateObject ("Wscript.Shell")
echo >>"%temp%\%katorn%.vbs" strDesktop = "%temp%"
echo >>"%temp%\%katorn%.vbs" Set Shortcut = WshShell.CreateShortcut(strDesktop + "\%katorn%.lnk")
echo >>"%temp%\%katorn%.vbs" Shortcut.WindowStyle = "7"
echo >>"%temp%\%katorn%.vbs" Shortcut.TargetPath = "%music%"
echo >>"%temp%\%katorn%.vbs" Shortcut.Save
cscript "%temp%\%katorn%.vbs"
:: Uncomment the next two lines to delete the temporary files.
:: del /q /f "%temp%\%katorn%.vbs"
:: del /q /f "%temp%\%katorn%.lnk"
echo Enjoy!
Usage:
runmusic (name or full path with quotation marks)
Now that you understand, you can even leave a folder with the songs and the other with just the customized shortcuts, which can come in handy.
Hope this helps,
K.
i wanted to know if there is some way of doing music with batch, not just opening a mp3 file from cmd, but commands to do notes or something like that. If there really isn't how to do it, i understand.
batch can play music by creating and starting a vbs script to Leverage the internal windows media player.
Note: In all the below scripts, the variable %sounds% refers to the folder your sound scripts are located, and must be defined.
The core of playing music is the following Batch Script:
#Echo off & REM MusicPlayer.bat
Set "MusicPath=%~1" & REM Full Path for the music file
Set "vol=%~2" & REM Volume as number between 0 and 100
Set "LoopTF=%~3" & REM 'paramater 3 as 'true' or 'false' determines if the track is to be looped.
(
PUSHD %sounds%
%= Change to the directory your sound files are located =%)
::: Ensure no Conflict with the Previous Script.
IF exist PlayMusic.vbs (
DEL PlayMusic.vbs
)
::: Creates a vbs Script to launch the music (Occurs without any visual indication or prompting)
(
echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%MusicPath%"
echo Sound.settings.volume = %vol%
echo Sound.settings.setMode "loop", %LoopTF%
echo Sound.Controls.play
echo While Sound.playState ^<^> 1
echo WScript.Sleep 100
echo Wend
echo Sound objTS = Nothing 'Destroy the object.
)>PlayMusic.vbs
start /min PlayMusic.vbs
(
POPD
%= Return to your Previous Directory =%)
::: Exit the Launcher and return to Previous batch program.
GOTO :EOF
The above script is called with 3 parameters as Remarked.
Another Vbs can be used to Monitor the batch's status and call a batch script to stop the music when the batch is closed:
#Echo off & REM Monitor.bat
(
ECHO Set objWMIService = GetObject ("winmgmts:"^)
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO DO while proc.count ^> 0
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO if proc.count ^< 1 then exit do
ECHO wscript.sleep 1500
ECHO loop
ECHO Set WshShell=createobject("wscript.shell"^)
ECHO WshShell.run "%sounds%\KillMusic.bat", 0, true
)>%sounds%\MusicMonitor.vbs
start %sounds%\MusicMonitor.vbs
Goto :EOF
The above script creates a hidden vbs that fetches the number of instances of cmd.exe via the objWMIService.ExecQuery. This occurs during a loop, with the break condition being 0 open cmd.exe windows. A sleep is built into the loop to reduce the frequency of calls to the WMI service, as these are very resource intensive. When the loop break occurs, it starts the killmusic.bat program in a hidden state.
The below script "KillMusic.bat" is called either directly in your quit label or by the vbs monitor when it determines Cmd.exe is no longer running. DoMonitor is a variable that is changed in your main script prior to killmusic being called. 1 indicates the monitor should be restarted, and is used when killmusic.bat stops a currently playing song to start a new song. Monitor is a Variable containing the path to Monitor.bat
#ECHO OFF & REM KillMusic.bat
taskkill /pid WScript.exe /f /t >nul
IF exist "%sounds%\PlayMusic.vbs" (
DEL /Q "%sounds%\PlayMusic.vbs"
)
Timeout 1 > nul
IF "%DoMonitor%"=="1" GOTO reset
GOTO :EOF
:reset
CALL "%Monitor%"
GOTO :EOF
These three programs can be seen in effect here.
Okay.I want to do a little app thing, and it sounds like this:
#echo off
CLS
start https://www.youtube.com/
timeout 4
#if (#CodeSection == #Batch) #then
#echo off
CScript //nologo //E:JScript "%~F0"
rem Open the browser here
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys("{K}");
>nul
It basically starts a video, when you open the file, runs in the background and presses K (my attempt to solve the problem), and then I want the video to automaticaly start.
P.S.I want to use .bat, not any other extension or program.
So:
How can I make the tab to open, and the video to automaticaly play?
You can also do like this in powershell:
#Echo Off
start /d "C:\Program Files (x86)\Google\Chrome\Application\" chrome.exe new-tab "www.youtube.com/watch?v=BISQwNjP2NE"
powershell -command "& {$wsh = New-Object -ComObject Wscript.Shell; $wsh.AppActivate("Chrome"); Start-Sleep -Seconds 5; $wsh.Sendkeys(" "); $wsh.Sendkeys("+{F10}"); $wsh.Sendkeys("{DOWN}"); $wsh.Sendkeys("{ENTER}")}"
Exit /b
Thanks #It wasn't me for classic idea.
Well, this is some kind of duplicate question here, anyway, I suggest using vbs to get this result.
Obs.: Take a time to see the link video about SendKeys/VBS
<!-- :
#echo off && mode 50,03 && title <nul && title .\%~nx0
start /d "c:\Program Files (x86)\Google\Chrome\Application\" chrome.exe -new-tab "www.youtube.com/watch?v=BISQwNjP2NE"
%__APPDIR__%wScript.exe "%~dpnx0?.wsf" && goto :EOF
--> <job> <script language = "vbscript">
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.AppActivate "chrome"
WScript.Sleep 4000
objShell.SendKeys " " ' use space (charater) to play/pause
'objShell.SendKeys ("+{F10}") left click | in your question, the
'objShell.SendKeys "{DOWN}" left click | left click is not works
'objShell.SendKeys "{ENTER}" left click | for play the video
</script></job>
I need to create a batch script that continually monitors a specific file for changes, in this case, LogTest.txt.
When the file is updated it will trigger a VBScript message box, which displays the last line from within LogTest.txt. The idea is that this will continue monitoring after the message box is cleared.
I have tried using the forfiles option, but this really only lets me deal with the date and not the time. I know that PowerShell and other options are available, but for reasons that are just too long to explain I am limited to being only able to use a batch and VBScript.
Batch File:
#echo off
:RENEW
forfiles /m LogTest.txt /d 0
if %errorlevel% == 0 (
echo The file was modified today
forfiles /M LogTest.txt /C "cmd /c echo #file #fdate #ftime"
cscript MessageBox.vbs "Error found."
REM do whatever else you need to do
) else (
echo The file has not been modified today
dir /T:W LogTest.txt
REM do whatever else you need to do
)
goto :RENEW
MessageBox.vbs:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox "This is an error", vbOkCancel + vbExclamation, "Error Found"
There is an archive attribute on every file. Windows sets this attribute on every write access to the file.
You can set it with the attrib command and check it for example with:
#echo off
:loop
timeout -t 1 >nul
for %%i in (LogTest.txt) do echo %%~ai|find "a">nul || goto :loop
echo file was changed
rem do workload
attrib -a LogTest.txt
goto :loop
timeout /t 1 >nul: small wait interval to reduce CPU-Load (never build a loop without some idle time)
for %%i in (logTest.txt) do... process the file
echo %%~ai print the attributes (see for /? for details)
|find "a" >nul try to find the "a"rchive-attribute in the output of the previous echo and redirect any output to nirvana (we don't need it, just the errorlevel)
|| goto :loop works as "if previous command (find) failed, then start again at the label :loop"
If find was successful (there is the archive attribute), then the next lines will be processed (echo file was changed...)
attrib -a LogTest.txt unsets the archive attribute of the file.
I am trying to add the following yes/no box into a batch file but keep failing.
Set objShell = CreateObject("Wscript.Shell")
intMessage = Msgbox("Would you like to go to URL?", _
vbYesNo, "Click yes to go to URL")
If intMessage = vbYes Then
objShell.Run("http://www.url.com")
Else
Wscript.Quit
End If
Can anyone point me in the right direction please?
#echo off
call :MsgBox "Would you like to go to URL?" "VBYesNo+VBQuestion" "Click yes to go to URL"
if errorlevel 7 (
echo NO - don't go to the url
) else if errorlevel 6 (
echo YES - go to the url
start "" "http://www.google.com"
)
exit /b
:MsgBox prompt type title
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%
Although there are several methods to include vbs code into a Batch file, all of them are clumsy, but you may google for they if you want.
May I suggest a different solution?
The inclusion of JScript code into a Batch file is simpler than VBScript, and the translation of a small code segment from VBS to JScript is not problematic. For example:
#if (#CodeSection == #Batch) #then
#echo off
rem Execute the JScript code:
CScript //nologo //E:JScript "%~F0"
goto :EOF
#end
// JScript code start here
objShell = WScript.CreateObject("WScript.Shell")
// http://msdn.microsoft.com/en-us/library/x83z1d9f.aspx
intMessage = objShell.Popup("Would you like to go to URL?",0,"Click yes to go to URL",4)
if (intMessage == 6)
objShell.Run("http://www.url.com")
else
WScript.Quit()
//endif
I found this code on the Net, perhaps it can help you !
::'.#FILE: .#This: NTFunID031.cmd #Date: 2014-04-17 .#Note: NTMaxToolsPlay ID 031
::'.#SUMMARY: .#Note: cmd+vbs, all in one .#INFO: .#File: NTFunID031_readme.txt
::'.#PROJECT_NAME: .#Parent: NTMaxTools .#AUTHOR: .#Name: NT
::'.
::'.
::response= MsgBox("Are you okay ?", vbYesNo+vbQuestion, "Hello ! ;)")
rem assign response to exit code
::wscript.quit(response)
call break '& #echo off& cls& echo.& echo.
call break '& title : -- Real all in one [cmd+vbs], NTMaxToolsPlay, just for the fun ;)
call break '& echo Hi!
call break '& wscript //e:vbs //nologo "%~f0"
call break '& set /a rep=%errorlevel%
call break '& if %rep% equ 6 echo Yesssssssssss, me too :p
call break '& if %rep% equ 7 echo Oh Nonnn :'(
call break '& echo.& pause