How to run a cmd command from bat file? - batch-file

I need to run this command in the command prompt automatically:
msiexec /i "My application"/qn
How can I write a batch file to do this?

Batch files are, for all intents and purposes, just lists of cmd commands. Just take that command, paste it into notepad, and save it with a .bat extension.
It's generally considered good practice to start the batch script with the line #echo off so that the lines of the script don't display as they're being executed.

#ECHO OFF
msiexec/i "My application"/qn
To run the batch file when the VM user logs in:
Drag batch file itself to Start - All Programs - Startup. Now when you login as that user, it will launch the batch file.
you may find the Batch HowTos helpful.
Examples: see Link
Install:
msiexec /i "C:\Install\ss64app.msi"
UnInstall:
msiexec /uninstall "{5AFF6499-63BA-4A36-83B2-8D256404AC3D}" /log "C:\install\ss64app.txt"
Autorun
1. create text file
2. edit with Notepad
[autorun]
icon=drive.ico
open=launch.bat
open=launch.bat
[autorun]
open=\folder1\runinstallers.bat
open=\folder2\PLSetup_2.exe
3. save the file as Autorun.inf
4. copy the Autorun.inf to the root folder of your CD-ROM

Related

Execute a copy command using a batch file by opening CMD

I'm creating a batch file on my desktop which has a simple code:
%SystemRoot%\system32\cmd.exe
This will open up Command Prompt.
Then I need to copy paste following code into the command prompt.
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %i in (NCR*.pdf) do copy "%~fi" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
The above command simply copies and pastes PDFs from one directory to another directory.
Is there a way to write the entire thing into a batch file?
Desired output is:
A Desktop Icon of a BAT File.
Double clicking on it will do two things: Open up Command Prompt and Executes the Copy command.
Closes the Command Prompt once done
Once that's done, I can simply use Windows Task Scheduler to run this Bat file everyday at 5:00 AM.
All the helps are appreciated. Thank You.
Create a batch file on the desktop and use the following code...
#echo off
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %%i in (NCR*.pdf) do (
copy "%%i" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
)
Double Click on the batch file anytime you want to run it.
That's it! =)

How to run batches NOT using cmd.exe (like Cmder.exe?)

I've recently installed cmder (or simply 'Commander') portable console emulator for windows, and really like the functionalities it comes with. But how can you (if at all) run batches through it like you can with the Windows command line?
The details:
If you write...
cd C:\test\
REM executeSomething.exe
Pause
... to a textfile and save it as a .bat file you can do can do pretty much anything just by double-clicking that file. I've got a work-flow where I launch a web-application through a batch file. Sometimes this applicaton launches automatically in Chrome, and sometimes it does not. In that case, I'll have to copy and paste a URL to Chrome manually. And that's a real pain. With cmder.exe that's much easier, but I'll have to manually navigate to a folder and start the application through Commander it manually without the luxury of a .bat file.
The functionality I'm trying to run automatically with cmder.exe is simply changing a folder and starting an executable file, specifically Jupyter Lab:
cd C:\jupyterlab\
jupyter lab
Pause
I've tried various approaches with:
#echo off
set CMDER_ROOT=C:\Cmder
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
as described here, but with no success. The example above does launch commander, but also raises a syntax error:
Current directory: C:\batches
Command to be executed: "C:\Windows\system32\cmd.exe" /k
"C:\Cmder\vendor\init.bat cd C:\batches && "
I'm obviously on to somehting here since I'm able to start Commander, but I'm not sure how to edit the remaining code to run the necessary steps. I do realize that the /title "Homestead VM" part just edits the title of the Commander window:
but I have no idea about the rest.
If this just isn't possible, I'm going to have to try to reassociate .bat files in Control Panel > Default Programs > Associate a filetype, but I'm hoping to avoid that.
Thank you for any suggestions!
System info:
Windows 7, 64 bit
Commander v1.3.12
Edit 1: My (failed) attempt trying to follow a suggestion from Gerhard Barnard
I've saved a file named please.cmd to C:\Windows\System32 that contains this:
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
I've also saved a file named SObatch.bat in C:\batches that contains this:
if not defined myComs set myComs=0 && please %~0
#echo off
ping localhost
echo %userprofile%
pause
Upon double-clicking SObatch.bat a Windows prompt is opened, and the pings are run:
I just narrowed the window to leave out real-world user-names. But the ping functions are being run and the usual responses are returned.
So I guess it still seems that I've broken something.
Create a file called please.cmd and save it to C:\Windows\System32 as administrator. It should then exist as c:\Windows\System32\please.cmd
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %*"
Now when you create a batch files you need to make some changes in the top of each batch file you want. You would need to add the below line to the very top of the script.
"%systemroot%\system32\please.cmd" "%~f0" && goto :eof
You can also from cmd please command to launch it from the cmder shell. Example:
please ping localhost

Why is xcopy command not working for copying a bitmap file into a subdirectory of Windows system32 directory?

I have a problem with my batch file. I want to copy some files using xcopy, but it isn't working.
#echo off
md %systemroot%\system32\hello
md %systemroot%\system32\wlppr
xcopy /y "%cd%\qwerty.bmp" "%systemroot%\system32\hello"
xcopy /y "%cd%\qwerty2.bmp" "%systemroot%\system32\wlppr"
The folders create succesfully, but when Windows tries to copy files this message appears in CMD:
File not found - qwerty.bmp
0 File(s) copied
File not found - qwerty2.bmp
0 File(s) copied
And yes, I tried to run the file with administrator privileges.
What am I doing wrong?
By double clicking on a batch file the Windows command interpreter cmd.exe is executed with using implicitly option /C to close the console window automatically and terminate the command process when batch processing is finished after successful execution or an exit because of a syntax error. This behavior is not good for debugging a batch file not working as expected. Better for debugging a batch file is opening a command prompt window resulting in running cmd.exe with using implicitly option /K to keep open the console window and let the command process continue running after executing a command like a batch file (not containing exit without /B) to view all error messages output during execution of the batch file.
Double clicking on a batch file results usually in executing the batch file in the directory of the batch file. For that reason the current directory is the directory of the batch file.
But if the batch file is stored on a network share opened in Windows Explorer with a UNC path, Windows command interpreter outputs usually a message that the current directory is set to %SystemRoot% before executing the first command line from batch file. This behavior can be turned off via a special Windows registry value.
And if the batch file is executed with right clicking on it and using context menu option Run as administrator, the Windows command process is started with %SystemRoot%\System32 as current directory resulting in executing the batch file with the Windows system directory as current directory.
So it is advisable to write this batch file to work independent on current directory. A good idea would be to put the two bitmap files to copy into same directory as the batch file and use %~dp0 to reference the directory of the batch file independent on what is the current directory on execution of the batch file. %~dp0 references drive and path of argument 0 which is the name of the batch file. Run call /? in a command prompt window for details on referencing batch file arguments.
The command XCOPY creates automatically the entire folder tree on copying 1 or more files if the target string ends with a backslash making it clear for xcopy that the target is a folder name and not a file name.
So the commands to use are:
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty.bmp" "%SystemRoot%\System32\hello\" /Q /Y >nul
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty2.bmp" "%SystemRoot%\System32\wlppr\" /Q /Y >nul
The folder path of batch file referenced with %~dp0 always ends with a backslash. Therefore no additional backslash is specified in both source file name strings.
Run in a command prompt window xcopy /? for help on this command.

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

How to unregister a dll and then register a dll on the same batch file?

I unregistered and registered a dll manually by using the following commands in the command prompt.
Unregister:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe "C:\Program Files\ABC\foo.dll" /codebase /u
Register:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe "C:\Program Files\ABC\foo.dll" /codebase
So help me using batch commands so that clients can run this batch without knowing how to do it manually
A batch file, simply put, is just a long list of command line commands that are executed sequentially. So to make a .bat file with your commands. Create a text file and name it File.bat and then copy an paste your commands into the file. Save and you are done. Note that there are a few differences when working with symbols on the command and then in bat files, but for what you need it is not an issue.
Example:
:: Hide commands statements
#echo off
:: Unregister
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" "C:\Program Files\ABC\foo.dll" /codebase /u
:: Register
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" "C:\Program Files\ABC\foo.dll" /codebase
Great Batch References:
Technet, Rob, SS64, DosTips

Resources