Batch file won't open a hidden .vbs file - batch-file

I have a batch file which makes a .vbs file and is then supposed to open it. They are both in the same folder. I've tried using start, but it simply doesn't work for some reason. I also tried cscript, but since the script is supposed to be hidden, this doesn't work either. If I try to run the vbs file by hand, it works.
code:
#echo off
tasklist | find "wscript.exe" >nul: && goto runningalr
:reset
type nul > sound.vbs
(echo Set Sound = CreateObject("WMPlayer.OCX.7"^))>>sound.vbs
:begin
set /p loop=Do you want the music to repeat? (y/n) ...
if /i %loop%==y goto repeat
if /i %loop%==n goto begin1
goto begin
:repeat
(echo do until false)>>sound.vbs
goto begin1
:begin1
set /p damd=The music's directory? (type "exit" if no more) ...
if /i %damd%==exit goto start
( echo Sound.URL = "%damd%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000)>>sound.vbs
goto begin1
:runningalr
set /p kill=Music already playing. Do you want to stop it? (y/n) ...
if %kill%==y taskkill /f /im wscript.exe && exit
if %kill%==n goto gonnaexit
:gonnaexit
<nul set /p "=Press any key to exit ... "
pause >nul
exit
:start
if /i %loop%==y (echo loop)>>sound.vbs
start C:\Users\User\Desktop\server\sound.vbs
Why didn't start work for me and what works?

instead of having just "find wscript" try using "find %WINDIR%/System32/wscript.exe /I"
You might also need to run the script as an administrator, however this is unlikely.

you give a dedicated path with start, but you write the vbs script to the current working directory. The following line starts from that working directory:
start "" "%~dp0sound.vbs"

Related

How come running a batch script in task scheduler does not work the same way as running a batch script in file explorer?

I have a functioning script that works as intended when ran in file explorer, but I am trying to implement a way to run the script on logon. My solution was to use task scheduler, but when I run the script the status says "running," yet, the file doesn't execute.
Here is the following code:
#echo on
if exist reboot.txt goto iterate
if not exist reboot.txt goto begin
:begin
echo HOW MANY REBOOTS DO YOU WANT?
set /p input= Enter number:
type nul > reboot.txt
echo %input% >> reboot.txt
type nul > number.txt
echo 0 >> number.txt
goto iterate
:iterate
for /f " delims==" %%i in (number.txt) do set /A temp_counter= %%i+1
echo %temp_counter% > number.txt
for /f "delims=" %%i in (reboot.txt) do set Boot=%%i
echo %Boot%
if %temp_counter% GTR %Boot% (
echo Numbers match goto end
) else (
echo Numbers don't match
start shutdown.exe /r /t 60
exit
)
:end
del reboot.txt
del number.txt
exit

My batch script doesn't use if command correctly

I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

Batch IF doesn't work

I made a simple BATCH file for notes, it just deletes a folder, makes a new one, and puts in a file called note.txt and then edits it... for some reason, it doesn't do ANYTHING at all! not even removing the folder, here's the code:
goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
del /q "D:\Users\Eldar\Desktop\Note Folder"
mkdir "Note Folder"
#echo>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
#echo %note%> "D:\Users\Eldar\Desktop\Note Folder\note.txt"
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
ECHO goto replace
) ELSE (
GOTO abort
)
There are few errors in your script:
del can delete only empty folders. Use rmdir /s /q folder_name to remove folder with files
Remove ECHO in ECHO goto replace
You are using full paths everywhere except mkdir. Is it correct?
My working version:
#echo off
goto start
:abort
echo Aborted!
pause
goto start
:replace
rmdir /s /q ".\TestF"
mkdir "TestF"
echo>".\TestF\note.txt"
echo %note%> ".\TestF\note.txt"
pause
:start
color e
cls
echo Put the note in here:
set /p LastNote=<".\TestF\note.txt"
set /p note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
set /p agree=
if "%agree%"=="y" (
goto replace
) else (
goto abort
)
There appears to be no reason to remove the Note Folder directory or even to delete the note.txt file.
How does it perform if you rewrite it like this?
#Echo Off
Color 0E
Set "LastNote="
If Exist "%UserProfile%\Desktop\Note Folder\note.txt" (
Set/P LastNote=<"%UserProfile%\Desktop\Note Folder\note.txt"
)
If Not Defined LastNote GoTo replace
:ask
ClS
Echo=The last note you saved was:
Echo=%LastNote%
Echo=
Choice /M "Would you like to replace this note with a new one"
If "%ErrorLevel%"=="1" GoTo replace
Echo=
Echo=Aborted!
Timeout -1
GoTo ask
:replace
ClS
Set/P "note=Put the note in here: "
If "%note%"=="" GoTo ask
Echo=Note has been saved!
If Not Exist "%UserProfile%\Desktop\Note Folder\" (
MD "%UserProfile%\Desktop\Note Folder"
)
(Echo=%note%)>"%UserProfile%\Desktop\Note Folder\note.txt"
Timeout -1
GoTo ask
For the benefit of the OP, here is your version of your script with the major flaws removed and with unnecessary lines kept in.
#goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
rmdir "D:\Users\Eldar\Desktop\Note Folder"
mkdir "D:\Users\Eldar\Desktop\Note Folder"
echo.>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
>"D:\Users\Eldar\Desktop\Note Folder\note.txt" echo %note%
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
goto replace
) ELSE (
GOTO abort
)

BAT commands to recognize if process is running and display prompt

I need to create a bat that recognizes if a process is running, and if the process is running I need to add a prompt asking if the user wants to end the process to continue with some other tasks.
I have the command to recognize if a process is running(this is the one that worked for me):
#echo off
tasklist /nh /fi "imagename eq vmware-view.exe" | find /i "vmware-view.exe" >nul && (
echo VMWare Horizon is running
) || (
echo VMWare Horizon is not running
)
pause>nul
and I have the command to prompt for an imput:
#echo off
setlocal
:PROMPT
SET /P AREYOUSURE=Are you sure (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
But I don't know how to put those 2 together to say that, if the process is running, display prompt asking user if bat can end process to continue with an update, and if not running just go on with update.
could you please assist?
thanks in advance
I remember from the old DOS days that you could use GOTOs.
for instance a bat file containing the following:
#ECHO OFF
ECHO something
GOTO LABEL0
ECHO something else
GOTO LABEL1
:LABEL0
ECHO some label
:LABEL1
ECHO some other label
will print:
something
some label
some othe label
you will not see
something else
because the first GOTO skipped to LABEL0
You can use a label like :QUIT or :END at the end of your bat and skip at the end if you have nothing to do.
Use ERRORLEVEL to see if TASKLIST and FIND got the process. Afterwards use CHOICE.
#echo off
set task=vmware-view.exe
tasklist | find /i "%task%" > nul
if %errorlevel%==0 (
echo task is running.
choice /c yn /t 10 /d n /m "should we kill it?"
if errorlevel 2 goto end
taskkill /f /im "%task%"
) else (
echo task is not running.
)
:update
echo updating...
:end

Windows batch script goto error

i have been trying to write a batch script in windows 7 sp1. After i start my batch script i try to type an option into the input and then i get "goto unexpected at this time" then the command prompt closes. Please help i need this for a personal project.
#echo off
:start
echo Is this the first time you ran this program on this computer
set input=
set /p intput= "y/n"
if%input%==yes goto Yes
if%input%==no goto No
:No
pause
echo Okay skipping the installation process
cd Minecraft_Server
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:serverrestart
echo Would you like to restart the server?
set input=
set /p intput= y or n
if%input%==y
pause
goto restart
if%input%==n
pause
goto norestart
:restart
echo To restart the server press any key.
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:norestart
exit
:Yes
pause
echo I will now install the Minecraft Serevr files, please make sure you
echo have me in the "Parent Directory" of the Minecraft_Server folder
echo --If you don't know what a parent directory is GOOGLE it!--
pause
mkdir Minecraft_Server
move files.exe Minecraft_Server
cd Minecraft_Server
start files.exe
timeout /t 3
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto :serverrestart
You misspelled input in the set /p line and you should use quotes. I used /i to make the compare case insensitive and shortened to one key required. Do as you wish with that. Try this:
#echo off
:start
echo Is this the first time you ran this program on this computer
set "input="
set /p input= "y/n"
if /i "%input%"=="y" goto :Yes
if /i "%input%"=="n" goto :No
echo.Enter a valid choice
pause
goto :start

Resources