Launch VBS MsgBox but continue the script - batch-file

I have tried multiple different way to make the below possible but cannot find a way to do it. I have a ROBOCOPY script that outputs a VBS messagebox depending on the exit code. However, I want the batch script to continue running and exit even after the messagebox appears. As of right now, I have to click Ok for the VBS message box to close and then the command line closes.
Here is my code:
#ECHO OFF
::Delete DB File
CD "C:\Program Files (x86)\SAP\Agentry Client"
DEL "Agentry.db"
CLS
::Copy new DB file from Fil003
2>nul PUSHD "\\sampleserver\123\123" >nul
ROBOCOPY /ZB /IS /IT /R:3 "\\sampleserver\123\123" "C:\Program Files (x86)\SP\AClient" "AClient.db" /log:"C:\Program Files (x86)\SP\AClient\Acleint.txt" /tee
if %ERRORLEVEL% EQU 16 GOTO ER16
if %ERRORLEVEL% EQU 1 GOTO ER1
if %ERRORLEVEL% EQU 0 GOTO ER0
:ER0
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "NoCopy.vbs"
TIMEOUT /T 3
POPD
exit
:ER1
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start "" cmd /c cscript "Success.vbs"
TIMEOUT /T 3
POPD
exit
:ER16
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "FatalError.vbs"
TIMEOUT /T 3
POPD
exit

You can use mshta in vbs rather than msgbox
Function mshta(ByVal MessageText, ByVal Title, ByVal PauseTimeSeconds)
Set WScriptShell = CreateObject("WScript.Shell")
ConfigString = "mshta.exe vbscript:close(CreateObject(""WScript.Shell"")." & "Popup(""" & MessageText & """," & PauseTimeSeconds & ",""" & Title & """))"
WScriptShell.Run ConfigString
End Function
'PauseTimeSeconds' will close the message, or you can set it to 0 for it to remain open until user clicks ok.

Related

Batch delete files in variable

I have a batch file which searches through a directory tree deleting generated file backups.
I want to edit the script to run the del command against the files found in the search, but I can't get it to work.
I've searched other threads and set it up similarly but without the expected results.
#echo off
pushd FILEPATH
echo Searching directories...
for /f "delims=" %%G in ('dir /b /s *.0**.rfa') do echo "%%G"
echo.
IF /I "%%G" == "" GOTO NOTFOUND
set /P delete="Delete files? [Y/N]: "
IF /I "%delete%" NEQ "Y" GOTO ENDOF
echo Deleting files...
echo.
del "%%G"
echo.
echo Done!
timeout 5
exit
:ENDOF
echo Aborted.
timeout 5
exit
:NOTFOUND
echo Found nothing.
timeout 5
exit
Result:
Deleting files...
Could Not Find FILEPATH\ %G
Done!
Do you really need the for loop? The following should work exactly as you want:
#echo off
dir /b /s "*.0*.rfa" || echo Found nothing. & goto :finish
choice /m "delete all? "
if errorlevel 2 echo Aborted. & goto :finish
echo Deleting files...
del /q /s "*.0*.rfa"
echo Done.
:finish
timeout 5
exit /b

Bat File to Trigger a program hotkey

I have to record a session with a program and want the windows task (1hour) to run a .Bat file to hit for example "F1" to stop the recording then run another .bat file to shutdown the pc.
I dont have problem with turning off pc but the one for "F1".
This is the code i thought it gonna work.
#echo off
cd "C:\Program Files\ShareX"
Start "" /b sharex.exe
timeout /T 5 /nobreak >nul
%SendKeys% "{F1}"
timeout /T 5 /nobreak >nul
taskkill /IM sharex.exe /F
Thanks
So, if I understood this question...
This will create the sendkey.vbs to you and run it in your code...
{ sorry my limited English }
#echo off & setlocal enabledelayedexpansion
set "_temp_vbs=%temp%\_temp_file_4vbs_.vbs"
>"!_temp_vbs!"^
(
echo/ Set WshShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Set objShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Wscript.Sleep 1500
echo/ objShell.AppActivate "sharex.exe"
echo/ Wscript.Sleep 1500
echo/ WshShell.SendKeys "({F1})"
)
cd /d "C:\Program Files\ShareX" & Start "" /b sharex.exe
timeout /t 3600 /nobreak && start "" /w "%Windir%\System32\wScript.exe" //nologo "!_temp_vbs!"
>nul 2>nul ((
tasklist | findstr /lic:"ShareX.exe"
) && (
timeout /t 5 /nobreak >nul & taskkill /f /im "ShareX.exe" & del /q /f "!_temp_vbs!"
) || (
call shotdown_pc.bat & exit /b
)) >nul 2>nul

How to sort formats in batch

This is my first question in stack overflow, I hope I'm not breaking any rules, If I do, Just tell me.
I want to do a batch to open just TXT files, And send an error while opening other file format
Example: RTF
#echo off
rem I do some jokes while coding, As put name the name "randy" to a random variable :3
set randy=%RANDOM%
if exist %1 goto 1
echo msgbox ("Corrupt or unexisting file"),16,("Project1")>"%TMP%\FC%randy%SPEECH.vbs"
CScript "%TMP%\FC%randy%SPEECH.vbs" //nologo
erase /f /q "%TMP%\FC%randy%SPEECH.vbs"
exit
:1
rem I failed at this part, This part was supposed to filter the File format.
set filetype=%~1
echo %filetype%
pause>nul
if %filetype%=="*.png" goto 2 else goto notvalid
goto fail
:2
type %1
echo.
echo Press any key to close the file
pause>nul
exit
:fail
rem I also did a section if the code fails
echo msgbox("Oops, The code did not work"),16,("Project1")>"%TMP%\FC%randy%SPEECH.vbs"
CScript "%TMP%\FC%randy%SPEECH.vbs" //nologo
erase /f /q "%TMP%\FC%randy%SPEECH.vbs"
exit
:notvalid
rem If the format is wrong
echo msgbox("This format is not compatible with the app"),16,("Project1")>"%TMP%\FC%randy%SPEECH.vbs"
CScript "%TMP%\FC%randy%SPEECH.vbs" //nologo
erase /f /q "%TMP%\FC%randy%SPEECH.vbs"
exit
I hope what you can help me.
Using this at the start will branch to your routine on other filetypes:
#echo off
if /i not "%~x1"==".txt" goto :notvalid
if /I "%filetype%"=="*.png" (goto 2) else goto notvalid
You need to use " double quotes and () parentheses properly, see IF command (note that goto fail statement will not perform at all then); /I switch for case insensitive comparing. Compare:
==>if "*.TXT"=="*.txt" dir /B xxxx2.csv else dir /B xxx.csv
==>if /I "*.TXT"=="*.txt" dir /B xxxx2.csv else dir /B xxx.csv
xxxx2.csv
xxx.csv
==>if /I "*.TXT"=="*.txt" (dir /B xxxx2.csv) else dir /B xxx.csv
xxxx2.csv
==>
And I would recommend next improvement (check whether at least one parameter is supplied):
set "randy=%RANDOM%"
if "%~1"=="" goto :notvalid
set "filetype=%~1"
if exist %1 goto 1

How to make shortcut to folder with Batch

How do i make a folder shortcut wiht Batch Commando Prompt?
i have tried this:
copy "C:\Windows" "C:\Users\%username%\Desktop\Windows.ink"
mklink "C:\Windows" "C:\Users\%username%\Desktop"
#echo off
call :createDesktopShortcut "%~1" "%~2"
exit /b
:createDesktopShortcut targetOfShortcut nameOfShortcut
if not exist "%~f1" goto :eof
setlocal & set "tempFile=%temp%\%~nx0.vbs.tmp" & set "name=%~2" & if not defined name set "name=%~n1"
echo(Set S=WScript.CreateObject("Wscript.Shell"):With S.Createshortcut(S.SpecialFolders("Desktop")+"\%~2.lnk"):.TargetPath="%~f1":.Save:End With>"%tempFile%"
cscript //nologo //e:vbscript "%tempFile%">nul & del /f /q "%tempFile%" >nul 2>nul
endlocal & goto :eof
Save as createDesktopShortcut.cmd and call it as
createDesktopShortcut.cmd "%windir%" "Win directory"

BAT file - open popup

I've written a little .bat file containing the following commands:
#echo off
Slmgr /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr /ato edkk4b-3sf6-j3h4-i5i5-d04gd453dgr3
Then i've packaged it in a .exe with a tool.
It works great but at the end of the execution two popup are displayed...
Can i remove the first popup and let display only the second?
As I see it, there are 2 options:
Run slmgr with cscript so all of the output goes to the console.
#echo off
setlocal
cscript //nologo c:\windows\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
cscript //nologo c:\windows\system32\slmgr.vbs /ato edkk4b-3sf6-j3h4-i5i5-d04gd453dgr3
Call :MsgBox "Done!" "VBOkOnly" "Process Complete"
exit /b
:MsgBox prompt type title
::Function by MC ND
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%
Edit 1 copy of slmgr.vbs and put it on a network share and run it from there for all PC's.

Resources