Ionic start command from batch file - batch-file

I want to create an application in ionic from a batch file, executing commands from a file named appgenerator.bat.
I have created the batch file but commands aren't recognized.
It works correctly when I enter the command at the Command Prompt, but not inside the file.
appgenerator.bat:
#echo %off
set nombreAPP=nuevaAPP2
set path=C:\Users\Antonio
ionic start %path%\%nombreAPP% blank
And this is the screen in cmd.exe (first I put "ionic" to probe that the command works and after "appgenerator" to start with my batch file)
I dont think that the problem is in system environment variables, because in the cmd window, work correctly.

Related

Why does my VBScript function differently if it is opened by a batch script rather than a person?

Simply put, I have a VBScript titled "tyrian_soundtest.vbs" that plays an .mp3 that is titled "tyrian_soundtest.mp3"
The VBScript code is below
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "tyrian_soundtest.mp3"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 1
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000
When opened, it plays the .mp3. Simple enough.
The trouble comes in when I run a batch script titled "tyrian_soundtest.bat". Relative to it, the .vbs and .mp3 are in a folder called sfx. Here is what one iteration of that file contained.
#echo off
start %cd%\sfx\tyrian_soundtest.vbs
exit /b
The result is an error stating that Windows couldn't find the file path, likely due to it containing a space. Other attempts of the .bat were replacing line 2 with
start .\sfx\tyrian_soundtest.vbs
or
start "%cd%\sfx\tyrian_soundtest.vbs"
Any attempt I've made gives one of three results. Option 1: There is no error, but the audio simply never plays. Option 2: An error is thrown about the file directory not being found. Option 3: That file path opens up in a new cmd window, but the .vbs is never run.
Is there any way format the .bat to get the .vbs to run through the without an error being caused?
The main issue is that there is used in batch file and in the VBScript file the current directory path. The current directory on starting %SystemRoot%\System32\cmd.exe to process the batch file can be any directory.
The Windows Explorer sets the directory of the batch file as current directory on double clicking the batch file resulting in starting cmd.exe by explorer.exe to process the double clicked batch file of which full qualified file name is passed to cmd.exe after the option /c as additional argument. But if the batch file is stored on a network resource accessed using UNC path, the Windows command processor changes the current directory from the network resource to %SystemRoot% (the Windows directory) and the batch file fails to start Windows Script Host to process the VBS file. See also: CMD does not support UNC paths as current directories.
A batch file can be also started by right clicking on it and using Run as administrator. This can result in making the directory %SystemRoot%\System32 (the Windows system directory) the current directory. See also: Why does 'Run as administrator' change (sometimes) batch file's current directory?
That are just two of many examples where the current directory is different to the directory containing the batch file. So if a batch file references other files stored in same directory as the batch file itself or a subdirectory of the batch files directory, it is advisable to reference these files with the full path of the batch file instead of using a path relative to current directory.
Example:
The used files are stored in directory C:\Temp\Development & Test as follows:
sfx
tyrian_soundtest.vbs
tyrian_soundtest.mp3
tyrian_soundtest.bat
A user opens a command prompt window which usually results in making the directory referenced with %USERPROFILE% or with %HOMEDRIVE%%HOMEPATH% the current directory. A user executes next in the command prompt window:
"C:\Temp\Development & Test\tyrian_soundtest.bat"
So the current directory is definitely not the directory containing the batch file.
The batch file can be coded as follows to start nevertheless the Windows Script Host for processing the VBS file tyrian_soundtest.vbs and successfully play the MP3 file tyrian_soundtest.mp3.
#start "" /D "%~dp0sfx" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
%~dp0 references the drive and path of argument 0 which is the full path of the currently processed batch file. The batch file path referenced with %~dp0 always ends with a backslash. For that reason the concatenation of %~dp0 with a file/folder name or wildcard pattern should be always done without using an additional backslash as that would result in two \ in series in complete argument string and the Windows file management would need to fix that small error by replacing \\ by just \ before passing the argument string to the file system. See also the Microsoft documentation about Naming Files, Paths, and Namespaces which explains which automatic corrections are usually applied on file/folder strings before passing them to the file system.
The internal command START of cmd.exe interprets the first double quoted argument string as title string for the console window as it can be seen on running in a command prompt window start /? and reading the output help. For that reason it is not enough to use just:
#start "%~dp0sfx\tyrian_soundtest.vbs"
That would result in starting one more command process with its own console window with the full qualified file name of the VBS file as title of the console window.
The Windows Script Host processing a VBS file by default on Windows exists in two versions:
%SystemRoot%\System32\cscript.exe is the console version.
%SystemRoot%\System32\wscript.exe is the Windows GUI version.
The usage of the console version cscript.exe results usually in opening a console window by the parent process if the parent process is not itself a console application running already with an opened console window like on execution of a batch file processed by %SystemRoot%\System32\cmd.exe being also a console application.
The usage of the Windows GUI version wscript.exe results in no opening of a window by default at all. The processed script file must contain commands to open a window if that is wanted at all.
The difference can be also seen on running from within a command prompt window cscript /? and next wscript /?. The first command results in printing the help for the command line options of Windows Script Host in already opened command prompt window while the second command results in showing a graphic window by wscript.exe displaying the same usage help.
The usage help of Windows Script Host explains also how each user can define which version of Windows Script Host is the default for executing scripts. So it is not advisable to specify just the VBS file name with full path on the command line with start and let cmd.exe look up in Windows registry which version of Windows Script Host to run to process the VBS file. It is better to explicitly run the version of Windows Script Host most suitable for playing the MP3 file which is in this case the Windows GUI version wscript.exe opening by default no window at all to play the MP3 file in background.
So it would be possible to use:
#start "" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
There is an empty title string defined with "" as the started executable wscript.exe is a Windows GUI application for which no console window is opened at all by cmd.exe. So the title string does not really matter and can be an empty string.
But there is left one problem with that command line. The VB script references the MP3 file without path which means with a path relative to current directory. The current directory is %USERPROFILE% and not C:\Temp\Development & Test\sfx which contains the MP3 file tyrian_soundtest.mp3. So the VB script would fail to find the MP3 file to play.
There are two solutions to fix this:
The usage of the following command line in the batch file:
#start "" /D "%~dp0sfx" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
The option /D of command START is used to explicitly set the subdirectory sfx of the batch file directory as start in respectively current directory for the process wscript.exe which cmd.exe starts using the Windows kernel library function CreateProcess with an appropriate created structure STARTUPINFO.
The VBS file references the MP3 file tyrian_soundtest.mp3 with its full path which the VB script file determines itself from its own full qualified file name. That can be achieved in the VB script file tyrian_soundtest.vbs by using in the second line:
Sound.URL = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) + "\tyrian_soundtest.mp3"
Best would be using both possible solutions as in this case the VB script file would work also on being executed by a different process than cmd.exe processing the batch file tyrian_soundtest.bat and deletion of directory %~dp0sfx is not possible as long as started wscript.exe is running because of this directory is the current directory of running Windows Script Host.
So the batch file as well as the VB script file work now both independent on which directory is the current directory as both reference the files with full path determined by the scripts themselves from their full qualified file names.

How to use a Batch file to set PATH and then input commands normally?

I'm setting up an external hard drive with portable apps, and there are some CLI tools that I'd like to easily access.
I've done it before but I forgot how; basically, I have a file which sets the PATH variable for all the required CLI programs, and then allows me to use the command prompt normally.
How do I do this without having to run the file from another command prompt, but by double clicking the Batch file?
If your command line tools are in a folder named CLiTools next to the batch-file, then you could have 2 lines as the batch-file content.
#set "path=%path%;%~dp0CLiTools"
#cmd
You can double click the batch-file, it will open cmd with the modified setting of %path% and you can enter commands as you would normally do. If you have i.e. xyz.exe in the CLiTools folder, then you can type xyz at the current command prompt and will be recognized as a command.
The changed environment applies to the current child cmd session that inherited the environment.
%~dp0 is the drive and path of argument 0 which is the drive and path to the batch-file in this case.

How to start Python virtual environment in .bat batch file?

I need to set up a long list of temporary environment variables every time when I start a Python Flask app on Windows 10. Now I would like to create a batch file to run with all settings in one double click. The following lines run well if I copy them and paste to cmd prompt, but I couldn't run them in the batch file.
The execution of batch file always gets tripped and exited at the 2nd line venv\scripts\activate in the batch file, which has no issue at all if I copy and paste line by line at cmd.
cd C:\py\project
venv\scripts\activate
set env1=val1
set env2=val2
set FLASK_APP=some.py
flask run
One of the many (far too many) quirks of .bat files is that if you launch another .bat file, it doesn't know where to return to.
You need to explicitly call it:
call venv\scripts\activate
you can just use
start venv\Scripts\activate
This will open up a new terminal just like that... you can pass other commands using the && or & sign.

Why can't I use the command prompt after running a batch file on it?

So I have a batch file that simply runs an exe file. I want to be able to open the command prompt, run the batch file, then... I want to type another command in the command prompt.
here is the code that is in the batch file called "sublime.bat":
"C:\Sublime Text 3\sublime_text.exe"
I open cmd in the directory with my bat file and I type:
"sublime.bat"
It works by opening sublime text but the cmd cursor starts flashing and I can no longer type anything until I close sublime text.
I want to be able to open sublime text and type commands out while still having sublime text open. Please help, thanks.
Command prompt doesn't execute a further line, while a command is on execution. It executes commands serially, not parallelly. So if you want a command to be executed cmd should return from executing previous one.
Here, in "sublime.bat" you have called a batch file which contains a command of executing another program. So, cmd waits for the result of executing the bat file and thus stuck there.
You can use start "/k" "C:\Sublime Text 3\sublime_text.exe" in your "sublime.bat". This holds only the start command and cmd gets free after starting the file.

How to run multiple commands in cmd using a script

I have three different commands that I want to execute my running one script,
I created a file called as myscript.bat
I want the following two commands to run in sequence when I run the script:
cd C:\Users\johnDoe\Idea\View\Proxy
node proxy.js
PS:And after that I want the cmd to be left open, not close automatically
Leave the bat file as it is, but run it with cmd /k myscript.bat. Also, if there's a chance of the command window opening by default on another drive, you might want to add the line C: to the beginning of the script to change volumes. cd changes folders within a given volume, but it doesn't actually change volumes.
Alternatively, if you just want the window to stay open so you can read the output, but you don't actually want to run any additional commands in it after your commands finish, you can just add the line pause at the end of the script.
#reirab's answer contains the crucial pointer: use cmd /k to create a console window that stays open.
If you don't want to use cmd /k explicitly - say, because you want to open the batch file from Explorer, you have 2 options:
[Suboptimal] Add a cmd /k statement as the last statement to your batch file.
[Better] Write a wrapper batch file that invokes the target batch file with cmd /k.
For instance, if your batch file is startProxy.bat, create another batch file in the same folder, name it startProxyWrapper.bat, and assign the following content:
#cmd /k "%~dp0startProxy.bat"
When you then open startProxyWrapper.bat from Explorer, a persistent console window (one that stays open) will open and execute startProxy.bat.

Resources