I running Windows 10 and I can't execute a script shell in .bat
So, my code is:
#echo off
echo .
echo MsgBox "TA MERE"
echo .
echo MsgBox "Salut ca va?"
> msgbox.vbs
cscript msgbox.vbs
loop
So, my script is running but the script do not create msgbox.vbs file on the desktop and the program close the Windows
This is a guess based upon what I think your script is supposed to do:
#echo off
( echo.
echo MsgBox "TA MERE"
echo.
echo MsgBox "Salut ca va?"
)> msgbox.vbs
cscript msgbox.vbs
I've missed out loop because I don't know where you want to loop to.
You can try something like that :
#echo off
REM We create our vbs file in temporary folder
REM If you want to create your vbs file on your desktop
REM Just Replace this line set "VBSFile=%Tmp%\%~n0.vbs" to
REM set "VBSFile=%userprofile%\desktop\%~n0.vbs"
set "VBSFile=%Tmp%\%~n0.vbs"
(
echo MsgBox "TA MERE TOI MEME LOL !",vbExclamation,"TA MERE TOI MEME LOL !"
echo MsgBox "Salut Comment ca va ?",VbQuestion,"TA MERE TOI MEME LOL !"
)> "%VBSFile%"
REM We execute our vbs file
cscript /nologo "%VBSFile%"
REM We Clean our vbs file
Del "%VBSFile%"
Related
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.
I have a batch script which when given the input "edit", should then echo "hello" as a sort of debug and open the batch scripts file in notepad. However for some inexplicable reason the script will not respond to the if statement no matter what. How do I get it to respond to the "edit" input?
REM #ECHO OFF
cd/
cd projects\py_test
ECHO Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
ECHO.
SET /P name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
REM #ECHO OFF
cmd /k IF %name%==edit GOTO EDIT
REM IF EXIST %name% py %name%
REM IF NOT EXIST %name% echo [101mERROR: The requested file could not be found. Make sure the file exists in "C:\Projects\py_test\" and that the filename includes the ".py" extension.[0m
#ECHO OFF
:EDIT
ECHO HELLO
notepad projects-py_test-dot_start.bat`
Firstly, why all the REM #ECHO OFFs? It looks ugly, especially when they are all caps.
Then, you want to run cmd /k for an if statement for no real reason? With the variable name you need to preferbly enclose the if statement variables in double quotes to eliminate possible whitespace:
#echo off
cd /d "C:\projects\py_test"
echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
echo/
set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
if defined name set "name=%name:"=%"
if /i "%name%"=="edit" goto edit
goto :EOF
:edit
echo hello
notepad echo "%~f0"
but by guessing that you simply want to launch a python script if it exists, else edit itself, then I would instead do this version without the labels. It simply checks if the name typed exists (hoping the user typed the full script with extension) else, we add the extension test in case the user typed only the name and not extension.:
#echo off
cd /d "C:\projects\py_test"
echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
echo/
set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
if defined name set "name=%name:"=%"
if /i "%name%"=="edit" notepad "%~f0"
if exist "%name%" (
python "%name%"
) else (
if exist "%name%.py" (
python "%name%.py"
) else (
echo "%name%" does not exist
)
)
In command prompt...
First execute Batch file
then execute numbers of commands.
so how can i store all that command and its output in a single file,
and file path define in batch file. ?
like this maybe
#echo OFF
(
Echo Ipconfig
Echo.
Ipconfig
Echo.
Echo.
Echo Net User Guest
Echo.
Net user Guest
)>file.txt
Exit
Or you could set the commands as variables and then type it instead of the whole command.
And easy Way to create a batch prompt that saves its output you could do it like This:
#echo off
:top
Cls
Set /p c=
Echo %c% >> output.txt & %c% >> output.txt
%c%
Echo. >> output.txt
Goto top
Of course then run the command and then create a space for the next Line
Batch content:
echo Set wshShell =wscript.CreateObject("WScript.Shell")>"C:\Users\Riccardo\Desktop\prova.vbs"
<nul set /p =wshshell.sendkeys ^"^<nul set ^/p ^=>>"C:\Users\Riccardo\Desktop\prova.vbs"
Type "C:\Users\Riccardo\Desktop\prova.txt">>"C:\Users\Riccardo\Desktop\prova.vbs"
echo {bs}^>^"^"C:\Users\Riccardo\Desktop\prova.txt^"^"{enter}^">>"C:\Users\Riccardo\Desktop\prova.vbs"
wscript "C:\Users\Riccardo\Desktop\prova.vbs"
The vbs generated is:
Set wshShell =wscript.CreateObject("WScript.Shell")
wshshell.sendkeys "<nul set /p =La#{bs}>""C:\Users\Riccardo\Desktop\prova.txt""{enter}"
The prova.txt contains:
La#
After execute this line from new command prompt session:
wscript "C:\Users\Riccardo\Desktop\prova.vbs"
The prova.txt becomes
La
But if execute the same line as above from batch:
wscript "C:\Users\Riccardo\Desktop\prova.vbs"
nothing happens!
How can I fix the problem?
Try to call the script like so:
C:\Users\Riccardo\Desktop\prova.vbs
If that doesn't work, change directory to your desktop, and call the vbs:
cd C:\Users\Riccardo\Desktop\
prova.vbs
That should work in a batch file.
I am solved the my problem. The "Prova.bat" code is:
echo Set wshShell =wscript.CreateObject("WScript.Shell")>prova.vbs
echo wshShell.Run "cmd">>prova.vbs
echo WScript.Sleep 250>>prova.vbs
<nul set /p =wshShell.sendkeys ^"^<nul set ^/p ^=>>prova.vbs
Type prova.txt>>prova.vbs
<nul set /p ={bs}^>prova.txt{enter}exit{enter}^">>prova.vbs
wscript prova.vbs
DEL /q prova.vbs 2> NUL
The "Prova.vbs" generated and deleted is:
Set wshShell =wscript.CreateObject("WScript.Shell")
wshShell.Run "cmd"
WScript.Sleep 250
wshShell.sendkeys "<nul set /p =La#8{bs}>prova.txt{enter}exit{enter}"
The "Prova.txt" before:
La#8
The "Prova.txt" now:
La#
I am found the way of insert backspace character in a txt file using bat+vbs file! ;-) I hope that such a solution can be of help to someone else.
I created a batch file to lookup my external ip.
and it works well .
This is the code.
#echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause
What i want is to Print the results to a text file named "IPlog.txt"
and every time i run the bat file it has to do the same thing and print the new results to the next line in the text file. So please can anyone help me with this.
... or change your
echo %ip:~1%
to
echo %ip:~1% >>IPlog.txt
to run your batch without the additional " >>IPlog.txt "
Please remove the pause command from your code and run the batch-file like this
mybatch.bat >> IPlog.txt
This will append the resulting ip address on to the log file IPLog.txt every time you run this batch file.