There are many reasons to want to 'convert' a .bat to .exe - to hide/obfuscate implementation, passwords, path to resources , to create a service from batch file ... and mainly to make your work to look more complicated and important than it really is.
There are also many reasons to not want to use third party tools.
So what if you want to 'convert' a batch file to .exe without external software?
(convert is in quotes because I don't think there's really way to compile a batch file to executable. There are too many abusive twisty techniques and bugs used extensively and all the tools that I know in fact create a temporary .bat file and then call it )
One very obvious approach is to use IEXPRESS - the ancient built-in tool that creates self-extracting packages and is capable to execute post extraction commands.
So here's IEXPRESS sed-directive/.bat file that creates a self-extracting .exe with packed .bat.
It accepts two arguments - the .bat file you want to convert and the target executable:
;#echo off
; rem https://github.com/npocmaka/batch.scripts/edit/master/hybrids/iexpress/bat2exeIEXP.bat
;if "%~2" equ "" (
; echo usage: %~nx0 batFile.bat target.Exe
;)
;set "target.exe=%__cd__%%~2"
;set "batch_file=%~f1"
;set "bat_name=%~nx1"
;set "bat_dir=%~dp1"
;copy /y "%~f0" "%temp%\2exe.sed" >nul
;(echo()>>"%temp%\2exe.sed"
;(echo(AppLaunched=cmd.exe /c "%bat_name%")>>"%temp%\2exe.sed"
;(echo(TargetName=%target.exe%)>>"%temp%\2exe.sed"
;(echo(FILE0="%bat_name%")>>"%temp%\2exe.sed"
;(echo([SourceFiles])>>"%temp%\2exe.sed"
;(echo(SourceFiles0=%bat_dir%)>>"%temp%\2exe.sed"
;(echo([SourceFiles0])>>"%temp%\2exe.sed"
;(echo(%%FILE0%%=)>>"%temp%\2exe.sed"
;iexpress /n /q /m %temp%\2exe.sed
;del /q /f "%temp%\2exe.sed"
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=1
UseLongFileName=1
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=
DisplayLicense=
FinishMessage=
FriendlyName=-
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
example:
bat2exeIEXP.bat myBatFile.bat MyExecutable.exe
This should work practically on every Windows machine out there but has one major limitation - you cannot pass arguments to the created .exe file
So one other possible approach is to look at the .NET compilers (again should be available on almost every win machine).I've choose Jscript.net .
This is a hybrid jscript.net/.bat script that will read the .batch file content.Will create another jscript.net with the .bat file content and after the compilation will create a new bat file int the temp folder and will call it.And will accept command line arguments.(explained might look complex but in fact it's simple):
#if (#X)==(#Y) #end /* JScript comment
#echo off
setlocal
del %~n0.exe /q /s >nul 2>nul
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
%~n0.exe "%jsc%" %*
del /q /f %~n0.exe 1>nul 2>nul
endlocal & exit /b %errorlevel%
*/
//https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/bat2exe.bat
import System;
import System;
import System.IO;
import System.Diagnostics;
var arguments:String[] = Environment.GetCommandLineArgs();
if (arguments.length<3){
Console.WriteLine("Path to cmd\bat file not given");
Environment.Exit(1);
}
var binName=Path.GetFileName(arguments[2])+".exe";
if(arguments.length>3){
binName=Path.GetFileName(arguments[3]);
}
var batchContent:byte[]= File.ReadAllBytes(arguments[2]);
var compilerLoc=arguments[1];
var content="["
for (var i=0;i<batchContent.length-1;i++){
content=content+batchContent[i]+","
}
content=content+batchContent[batchContent.length-1]+"]";
var temp=Path.GetTempPath();
var dt=(new Date()).getTime();
var tempJS=temp+"\\2exe"+dt+".js";
var toCompile="\r\n\
import System;\r\n\
import System.IO;\r\n\
import System.Diagnostics;\r\n\
var batCommandLine:String='';\r\n\
//Remove the executable name from the command line\r\n\
try{\r\n\
var arguments:String[] = Environment.GetCommandLineArgs();\r\n\
batCommandLine=Environment.CommandLine.substring(arguments[0].length,Environment.CommandLine.length);\r\n\
}catch(e){}\r\n\
var content2:byte[]="+content+";\r\n\
var dt=(new Date()).getTime();\r\n\
var temp=Path.GetTempPath();\r\n\
var nm=Process.GetCurrentProcess().ProcessName.substring(0,Process.GetCurrentProcess().ProcessName.length-3);\r\n\
var tempBatPath=Path.Combine(temp,nm+dt+'.bat');\r\n\
File.WriteAllBytes(tempBatPath,content2);\r\n\
var pr=System.Diagnostics.Process.Start('cmd.exe','/c '+' '+tempBatPath+' '+batCommandLine);\r\n\
pr.WaitForExit();\r\n\
File.Delete(tempBatPath);\r\n\
";
File.WriteAllText(tempJS,toCompile);
var pr=System.Diagnostics.Process.Start(compilerLoc,'/nologo /out:"'+binName+'" "'+tempJS+'"');
pr.WaitForExit();
File.Delete(tempJS);
It's rather a POC , but .NET System.Diagnostics and System.IO libraries are powerful enough to add features like hidden start , enctiption and etc.You can check also jsc.exe compiling options to see what else is capable of (like adding resources).
I promise an upvote to every improvement over the .NET method :-)
UPDATE: the second script has been changed and now the exe from the converted bat file can be started with double click.It uses the same interface as previous script:
bat2exejs.bat example.bat example.exe
I do know how to convert bat/cmd to exe manually, make sure the bat/cmd filename contains just letters, and numbers. Open 'IExpress Wizard' as admin.
Select 'Create new Self Extraction Directive file'
Select 'Extract files and run an installation command'
Name the package anything
'No prompt' for 'Confirmation prompt'
'Do not display a license' for 'License agreement'
Click 'Add' for the 'Packaged files', from there select the bat/cmd file
Then in 'Install Program' text box for 'Install Program to Launch', type cmd /c, followed by the full name of the bat/cmd file, (example: emptyrecyclebin.bat => cmd /c emptyrecyclebin.bat)
Leave the 'Post Install Command' as is
'Hidden' for 'Show window'
'No message' for 'Finished message'
Click 'Browse', and select where to download the exe to
Enable 'Hide File Extracting Progress Animation from User'
Disable 'Store files using Long File Name inside Package'
Definitely 'No restart' for 'Configure restart'
Then save SED if you want to re-compile it quicker later
Then create the package! A command window should quickly appear and disappear
Navigate to the place where you downloaded the exe to, and enjoy!
All of the above methods do not protect your source code in any way. I recently saw a press release in the news about a new compiler that does not depend on CMD.exe. I don't know exactly how it works, but it doesn't create temporary files at runtime.
https://www.prlog.org/12882479-the-worlds-first-true-compiler-for-batch-files.html
Using IEXPRESS makes a kind of SFX archive, which does not achieve the main goal of hiding the source code with passwords. All other "compilers" work on a similar principle - extract the script to a temporary folder and run it via cmd.exe. The press release claimed a real compilation, so I downloaded the trial version and wrote a primitive script to measure the speed:
#echo off
set startTime=%time%
set counter=0
:main_loop
echo %counter%
set /a counter=counter+1
if %counter% LEQ 10000 goto main_loop
echo Start Time: %startTime%
echo Finish Time: %time%
After compiling the code runs twice as fast and I haven't noticed any calls to cmd.exe in the Task Manager. In addition, I ran Process Monitor and saw no creation of temporary files while it was running. This leads me to believe that this compiler provides better protection for the source code.
You can also develop a simple exe, which just calls your bat-script.
For example you could write one in C# (I'm no C#-Pro, this is actually my first program and I copied lots of it from this other Stackoverflow post.):
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
class BatCaller {
static void Main() {
var batFile = System.Reflection.Assembly.GetEntryAssembly().Location.Replace(".exe", ".bat");
if (!File.Exists(batFile)) {
MessageBox.Show("The launch script could not be found.", "Critical error", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(42);
}
var processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + batFile + "\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}
}
If you store this code above to MySuperApp.cs just next to MySuperApp.bat and then compile it with csc.exe /target:winexe MySuperApp.cs (and maybe even add /win32icon:MySuperApp.ico to add a fancy icon) it will generate a MySuperApp.exe.
Launching MySuperApp.exe will call MySuperApp.bat (the bat-file with the same name).
csc.exe (should?) be present on every Windows machine.
Different versions of Windows has different effects for same batch file commands, and some commands are limited to some Windows systems eg. findstr and shutdown.
BTW, Win 10 CMD doesn't allow changes to SETLOCAL on command line. OK for batch files.
See this link for different commands for restarting different versions of windows:
https://www.computerhope.com/issues/ch000321.htm
So if you were to compile a script on Win 98, and run on Win 8.1, you'd get unexpected results or scripts may not even work.
See list of commands here:
https://www.ionos.com/digitalguide/server/know-how/windows-cmd-commands/
For this reason, one would need a different compiler on each version of Windows, preferably which would spit out binary code (generic) that can be run on as many CPU chips as possible, with same instruction sets. A workaround offered by most programs is to wrap the script in an exe file that would unwrap and execute the script when opened/run eg. Bat_To_Exe_Converter, Bat2Exe, BatchCompiler, iexpress or Winzip:
https://support.winzip.com/hc/en-us/articles/115011794948-What-is-a-Self-Extracting-Zip-File-
To solve this issue of portability, virtual machines have become more popular and hence the rise of Java & related scripts.
This however, would still be intepreted code, and not as fast as compiled code. Even byte code (intermediate code) from virtual machines still need to be compiled, even if it's (JIT):
https://aboullaite.me/understanding-jit-compiler-just-in-time-compiler/
In short, you can get an exe file which would contain a script that would be intepreted by the command processor, but it won't be a native executable file, meaning it won't run without a host by the Windows operating system.
Related
Following the same steps as this guide. I am trying to convert from bat to exe without installing any new software such as Bat to Exe Converter. The reason I am using this method is because all machines in my workplace already have 7zip installed and can use it, however I am not allowed to make the script work using external software not present on the main server to be compatible on any machine in the company.
I have the following TEST.bat:
ECHO This is a Test bat to exe
pause
and the config.txt:
;!#Install#!UTF-8!
RunProgram="TEST.bat"
;!#InstallEnd#!
then I call the following command line (in another .bat):
COPY /B "%PathTo7Zip%\7zCon.sfx" + %Config% + %Source7ZFile% %OutputFile%
%PathTo7Zip% is the directory to 7zCon.sfx
%Config% is the config.txt file above
%Source7ZFile% is my .7z archive
and %OutputFile% is my output TEST.exe file which should supposedly run the bat file when I call it according to the author of the guide.
However calling TEST.exe triggers unzipping the .7z archive (which is expected) and then exits without running TEST.bat.
However the author explains:
Conclusion:
It is important to note that while the resulting file runs exactly the same as the source BAT file, this is not a true batch to executable conversion. The resulting file is an EXE, however it is intended to be used for self-extracting installers. When you execute the resulting EXE file, the process goes something like this:
The contents of the EXE file are extracted to the temp directory.
The config file generated by the script is read.
The batch file contained in the EXE file is executed in a new
command window.
Once finished, the temp files are removed.
Refer to this How can a .bat file be 'converted' to .exe without third party tools?
The original script accepts two arguments - the .bat file you want to convert and the target executable.
I made a little modification to accept one argument : Just the .bat file you want to convert.
So in this case, you can drag and drop your batch file over this script bat2exeIEXP.bat and it will be converted to exe file with the same name as the batch file.
;#echo off
;Title Converting batch scripts to file.exe with iexpress
;Mode 75,3 & color 0A
;Rem Original Script https://github.com/npocmaka/batch.scripts/edit/master/hybrids/iexpress/bat2exeIEXP.bat
;echo(
;if "%~1" equ "" (
;echo Usage : Drag and Drop your batch file over this script:"%~nx0"
;Timeout /T 5 /nobreak>nul & Exit
;)
;set "target.exe=%__cd__%%~n1.exe"
;set "batch_file=%~f1"
;set "bat_name=%~nx1"
;set "bat_dir=%~dp1"
;Set "sed=%temp%\2exe.sed"
;echo Please wait a while ... Creating "%~n1.exe" ...
;copy /y "%~f0" "%sed%" >nul
;(
;(echo()
;(echo(AppLaunched=cmd /c "%bat_name%")
;(echo(TargetName=%target.exe%)
;(echo(FILE0="%bat_name%")
;(echo([SourceFiles])
;(echo(SourceFiles0=%bat_dir%)
;(echo([SourceFiles0])
;(echo(%%FILE0%%=)
;)>>"%sed%"
;iexpress /n /q /m %sed%
;del /q /f "%sed%"
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=1
UseLongFileName=1
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=
DisplayLicense=
FinishMessage=
FriendlyName=-
PostInstallCmd=<None>
AdminQuietInstCmd=
All windows machines with .NET installed come with files called vbc.exe, csc.exe & jsc.exe, at %windir%\Microsoft.NET\Framework (for 32bits) or Framework64 (for 64 bits), in a folder name starting with "v".
Guide on C# compiler:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe
Guide on Visual Basic compiler:
https://learn.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/index
Sample commands:
https://learn.microsoft.com/en-us/dotnet/visual-basic/reference/command-line-compiler/sample-compilation-command-lines
A Jscripting guide:
http://www.phpied.com/make-your-javascript-a-windows-exe/
A simple walkthrough from #Itchy to bundle a batch file to exe file with csc.exe:
How can a .bat file be 'converted' to .exe without third party tools?
In the same folder, a file called ngen.exe may also be found, which "generates native code so the application does not need to go through the just-in-time compilation process at runtime".
See:
https://books.google.com.au/books?id=iZM1jyqiyakC&pg=PA453&lpg=PA453&dq=guide++vbc.exe&source=bl&ots=UB041mSfni&sig=ACfU3U0xtmS8X9p0eDKD-u6bt-WvOVCbmQ&hl=en&sa=X&ved=2ahUKEwjshc6589_lAhXXXSsKHcTaBlUQ6AEwD3oECAYQAQ#v=onepage&q=guide%20%20vbc.exe&f=false
I am trying to start the default application for a file, wait for it to complete, and then continue with my batch file. The problem is that start, when used below simply creates another command prompt window with the example.doc in the title bar. I can use call instead of start, but then call does not wait for the program to finish before going to the next line. It appears that start needs to have an executable name and will not work with the default application system in windows.
Any ideas how I can make this happen without having to hardcode the windows application in the batch file as well?
set filename=example.doc
start /wait %filename%
copy %filename% %filename%.bak
How do I start the default application for a file, wait for completion, then continue?
It appears that start needs to have an executable name and will not work with the default application system in windows.
start, when used below simply creates another command prompt window with the example.doc in the title bar
start /wait %filename%
The above command won't work because %filename% is used as the window title instead of a command to run.
Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""
According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.
Source start
Try the following command instead:
start "" /wait %filename%
Alternative solution using the default open command
Any ideas how I can make this happen without having to hardcode the
windows application in the batch file as well?
One way is to use assoc and ftype to get the default open command used for the file then execute that command.
The following batch file does that for you (so no hard coding of windows applications is needed).
Open.cmd:
#echo off
setlocal enabledelayedexpansion
set _file=example.doc
rem get the extension
for %%a in (%_file%) do (
set _ext=%%~xa
)
rem get the filetype associated with the extension
for /f "usebackq tokens=2 delims==" %%b in (`assoc %_ext%`) do (
set _assoc=%%b
)
rem get the open command used for files of type filetype
for /f "usebackq tokens=2 delims==" %%c in (`ftype %_assoc%`) do (
set _command=%%c
rem replace %1 in the open command with the filename
set _command=!_command:%%1=%_file%!
)
rem run the command and wait for it to finish.
start "" /wait %_command%
copy %_file% %_file%.bak 1>nul
endlocal
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
assoc - Display or change the association between a file extension and a fileType
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
for - Conditionally perform a command several times.
for /f - Loop command against the results of another command.
ftype - Display or change the link between a FileType and an executable program.
start - Start a program, command or batch script (opens in a new window).
variable edit/replace - Edit and replace the characters assigned to a string variable.
Simply use the filename directly as command, unless that filename is a batch file, in which case use call.
In a batch file invocation of a GUI subsystem executable is blocking, unlike for an interactive command.
Use the start command when you don't want blocking execution.
There is a subtle point about “default application”, namely that a file type can have a registered default application for the graphical shell, e.g. its “Open with…”, without having an assoc/ftype association, or different from that association.
I'm not entirely sure of which registry entries are used for this. I've always had to look it up and research it each time. As I recall it's not well-documented.
But hopefully you're OK with just the assoc/ftype scheme.
A further subtle point about “default application”: on the laptop I'm writing this on the ftype association for text files is to open them in Notepad:
[H:\forums\so]
> assoc .txt
.txt=txtfile
[H:\forums\so]
> ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
[H:\forums\so]
> _
And this is what the graphical shell (Windows Explorer) will do.
But cmd.exe looks inside files, and if it finds an executable signature then it tries to run the text file as an executable, even in Windows 10:
[H:\forums\so]
> echo MZ bah! >oops.txt
[H:\forums\so]
> oops.txt
This version of H:\forums\so\oops.txt is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.
[H:\forums\so]
> _
My personal concern for this problem is because of a dynamic desktop program i am creating which the aim is for users to click a folder on a desktop and the contents of that folder becomes the new desktop. (I will post the code as an answer below as so to not convolute my actual question). However part of the code needs to kill and restart the explorer.exe process in order to reinitialize the desktop to display the new location.
Documentation of this problem is extremely difficult to find as its more technical than most people are willing to go for this particular field. This man is trying to do the exact same thing as me except using autoit, and here users looked more into doing it vbscript side but both came to the same result of killing and restarting explorer.exe in order to update the desktop.
My issue with killing the explorer.exe process in a forceful manner can result in unstable systems and the actual killing of the process takes a longer time to reboot the desktop than whatever action occurs when you simply move the desktop location. I want to know how i can update my desktop, by calling the dlls that update it, but from within a batch and vbscript hybrid.
EDIT:
Investigations to a command such as rundll32.exe user32.dll,LockWorkStation, and later to the user32.dll dependencies has uncovered multiple uses of desktop functions, in which i assume is used to update the desktop in some form. If you would like to view this, download dependency walker and open it to this folder from within the program. (C:\Windows\WinSxS\amd64_microsoft-windows-user32_31bf3856ad364e35_6.3.9600.18123_none_be367a2e4123fd9d\user32.dll)
Here is the manual changing of the desktop via batch and vbs hybrid. Its not perfect, but it provides a nice interface between moving in and out of directories to select the one you want to update to. This uses the taskkill which i want to depreciate with something else.
This is the initial batch script...
#echo off
setlocal enableextensions
::Below computes the desktop location that the program will reference
for /f %%a in ('cscript //nologo C:\HighestPrivelege\DesktopTools\findDesktop.vbs') do set "fold=%%a"
echo %fold%
::this loop will allow users to input new locations for the desktop by
moving through a terminal. Wildcards and autotab completion is usable are
and lack of input results in continuation of script
:loop
echo ################## %fold%
dir %fold% /A:D /B
echo _________________________
set loc=
set /p loc=[New Location?]
cd %fold%\%loc%
set fold=%cd%
IF [%loc%]==[] (goto cont) ELSE (goto loop)
:cont
::Below is the program that runs the regedit rewrite of the desktop variable
for the current user. It passes the decided folder value from earlier as the
new desktop.
cscript //nologo C:\HighestPrivelege\DesktopTools\setdesktop.vbs %fold%
::This restarts explorer.exe in order to reboot the newly created desktop
location. I wish i didnt have to kill explorer.exe forcefully in the process
but so far its the only way to make this program work.
taskkill /F /IM explorer.exe
explorer.exe
endlocal
pause
exit
and the VBS script that follows...
Option Explicit
'variable invocation
Dim objShell, strDesktop, strModify, strDelete, fso, f, File, content, parthe, args, arg1
Set args = WScript.Arguments
set objShell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
'this will take in the new desktop location.
set file= fso.OpenTextFile("C:\HighestPrivelege\DesktopTemporary.txt")
content = file.ReadAll
arg1 = args.Item(0)
parthe = ""&arg1&""
'The actual rewrite of the registry file containing the script lies below.
strDesktop = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strModify = objShell.RegWrite(strDesktop,parthe,"REG_EXPAND_SZ")
Activating the batch script will prompt user for where they would like to relocate their desktop to, and pass that variable into the VBscript to be rewrited to the registry. Upon VBScript completion, windows explorer will restart itself in order to reinitialize the new desktop location.
Until i can get a working model of the one where all i have to do is click/interact with the folder to initialize the program, this manual one will have to do.
I have written a batch script in an interactive mode, for making some tasks.
Sometimes, These tasks takes a long time to be finished, and then the batch asks if the user wants to go on to the next task, or back to the Batch's Main Menu or... etc
Now, what I want to do, is to add an "Interactive Alarm" command, that sounds a small short beep (Ex: Like the one when we turn on our PCs), to alert the batch user for new questions .
I don't know if this is possible or not, but the most important thing for me, NOT to use a GUI application like WMP or so..
I just want to do this from the Background, even If that beep has to be made from the free speaker, or by using a Third-Party CLI Application (Btw, I've Cygwin installed on my Win7-x64) .
Please note that, I will add that alarm command exactly before the interactive questions, waiting for user's answer to get to the next stage, so I can't just finish the batch, by making a real error beep !
So, would somebody please tell me how to do this ?
Appreciate your help :)
WARNING: rundll32.exe Kernel32.dll,Beep 750,300 no longer works well from the command line on modern windows systems as rundll32 no longer accepts integer values (again, through the command line) and this will play the beep with the default values which is too long (and frequency is irritating):
REM Again, with warnings about running this from the command line...
rundll32.exe Kernel32.dll,Beep 750,300
or
rundll32.exe cmdext.dll,MessageBeepStub
or
rundll32 user32.dll,MessageBeep
With rundll functions you won't need special symbols like ^G. With the first method you can also set the frequency and the time you want to beep, though see the warning that those parameters no longer work on modern systems from the command line and will instead play the annoying defaults.
UPDATE
other options are:
powershell "[console]::beep(500,300)"
or using systemSounds.bat
call systemsounds.bat beep
The capability of beeping depends on the mainboard and if the mainboard has a system speaker - which has increasingly become a rarity as systems tend to depend solely on "normal" speakers instead. An alternative is to play sound through those speakers. Here are some options:
Using the speaking capabilities of the SAPI.SpVoice:
mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('beep')))"
Here this is wrapped in a batch file and the words can be passed as an argument.
SAPI.SpVoice can be used for playing wav files and you have some packaged with the default Windows installation. You can use this script:
spplayer.bat "C:\Windows\Media\Windows Navigation Start.wav"
Another option: Using the windows media player active-x objects to play a sound. On Windows XP it was not installed by default but I think for the newer Windows versions it is. It also can play mp3 files:
call mediarunner.bat "C:\Windows\Media\Ring03.wav"
And one that is a little bit obscure - using the <bgsound> tag from internet explorer (which also can play mp3 files). Here's the script:
call soundplayer.bat "C:\Windows\Media\tada.wav"
And here's a way to use the BEL character to produce sound with easy to copy-paste code (I've called it a beeper.bat):
#echo off
setlocal
::Define a Linefeed variable
(set LF=^
%=-=%
)
for /f eol^=^%LF%%LF%^ delims^= %%A in (
'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x07"'
) do echo(%%A
It's not possible to type the BEL directly in (for example) notepad.
To get it, type echo ^G>>yourbatch.bat on the command line (don't type ^ G, but <Control>-G, which will be shown as ^G on the screen). That puts a strange looking character to the end of your file. That's the BELcharacter 0x007 ("control-G"). Just copy/move it to any echo command, you like. Also
set /p "input=^Ggive value: "
is possible (where the ^G represents that strange char)
The following can be used to issue a beep
without pausing the script
without creating a new line.
without requiring the use of a non-printable character
Echo/| CHOICE /N 2> nul & rem BEL
It is a deliberate misuse of the choice command, that Echo's nothing via a pipe to Choice, causing a non-breaking error. STDERR is redirected to nul, and the default choice prompt is suppressed via the /N switch, meaning no new line is output.
If for some reason you wanted to reuse this annoying tone throughout a script, you could define it as a macro
Set "BEL=Echo/| CHOICE /N 2> nul"
%BEL%
#echo off
echo BEEP.BAT by CSS---
echo PRESS ANY KEY TO HEAR A BEEP...
PAUSE>NUL
ECHO
echo I BEEPED
PAUSE
there is an ASCII control code ^G after the echo. Just copy this code, and save it as ASCII/ANSI using a text editor.
use ECHO command to echo a CTRL G
I think the better solution is echoing a ^G to a file from the cmd prompt and then type that file from within the script, that way you don't need to include control characteres in the batch file itself:
C:\> echo ^G>beep.snd
Now there's an ASCII 007 char in the "beep.snd" file, then from your .bat file all you have to do is type it or copy to the screen:
type beep.snd
or
copy beep.snd con > nul
I tried all the options above in Win 10. I settled with this
powershell.exe [console]::beep(500,600)
So programmatically in node.js it would look like this (python or C would be similar)
require("child_process").exec("powershell.exe [console]::beep(500,600)");
A bit late to the party, but I find this variation on #npocmaka version works for me with Windows 10:
REM This captures the Bell as a variable.
for /f %%g in ('%__APPDIR__%forfiles.exe /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "bel=%%g"
REM This produces the Bell sound.
set /P "=%bel%"<NUL
This works for me..
there was a special character in line 4 which stackoverflow was omitting,
code's pasted here:
hashb.in/long
and line 5 and 6 can be used interchangeably of course.
I'm trying to learn how to run Javascript (instead of VBScript) using CScript.exe, but when I try, I get an error:
cscript temp.js
Input Error: There is no script engine for file extension ".js".
I thought this is possible -- any idea what's wrong?
Setting the registry with regsvr32 did not work for me. Also, I don't want it, since I want my .js to be linked with a plain text editor.
But there is a command line option //E for cscript which makes the job:
cscript //E:jscript hello.js
A very simple fix: use assoc.
c:\>assoc .js=JSFile
(Mine had become associated with a text editor at some point.)
It's worth to mention that rplantiko's solution works even if the
extension of the filename is not .js. This allows for putting .js code
into a .cmd file and running as a batch, forming a single-file
solution that is fully portable without preliminary steps (like
assoc).
For example, if you create a test.cmd file with the following content,
you'll be able to run it by simply clicking on it in Explorer,
or by drag&drop another file over its icon:
#if (#CodeSection == #Batch) #then
#cscript //Nologo //E:jscript "%~f0" "test arg" %* & pause & goto :eof
#end
WScript.Echo("hello world");
for (var i = 0, n = WScript.Arguments.Length, args = []; i < n; ++i)
args.push(WScript.Arguments(i));
WScript.Echo("arguments: " + args.join(","));
The lines between #then ... #end are batch commands interpreted by cmd.exe. The last command is goto :eof to skip the rest of the file. The lines after #end are interpreted by cscript.exe.
It turns out that the .js extension wasn't associated with JSFile in the registry. Doing so fixed the problem.
assoc .js=JSfile
ftype jsfile=C:\Windows\System32\Cscript.exe "%1" %*
Had this problem, too, and I solved ...
1.- locate wsh.inf, the installation file for windows scripting host in %windir%\inf
2.- right-click on wsh.inf and choose install.
You should be able to run this command to fix the error:
regsvr32 %systemroot%\system32\vbscript.dll