Windows execute bat file from bat file

Execute a batch file on a remote PC using a batch file on local PC

I want to execute a batch file

Which is on my server inidsoasrv01 .

How should I write my .bat file?

4 Answers 4

Use microsoft’s tool for remote commands executions: PsExec

If there isn’t your bat-file on remote host, copy it first. For example:

And then execute:

Note: filepath for psexec is path to file on remote server, not your local.

You can use WMIC or SCHTASKS (which means no third party software is needed):

2) WMIC (wmic will return the pid of the started process)

If you are in same WORKGROUP shutdown.exe /s /m \\ should be enough shutdown /? for more, otherwise you need software to connect and control the target server.

UPDATE:

Seems shutdown.bat here is for shutting down apache-tomcat.

As native solution could be wmic

wmic /node: process call create «cmd.exe c:\\somefolder\\batch.bat»

In your example should be:

wmic /? and wmic /node /? for more

While I would recommend against this.

But you can use shutdown as client if the target machine has remote shutdown enabled and is in the same workgroup.

replacing with the URI for the target machine,

Otherwise, if you want to trigger this through Apache, you’ll need to configure the batch script as a CGI script by putting AddHandler cgi-script .bat and Options +ExecCGI into either a local .htaccess file or in the main configuration for your Apache install.

Command to run a .bat file

I’m trying to make my Visual Studio build script execute a .bat file that does something important.

Here is what I’m want to do right now:

But it doesn’t work.

I have to do this to make it work:

But this is pretty difficult to add to the Visual Studio script.

How can I do this in one single line?

4 Answers 4

«F:\- Big Packets -\kitterengine\Common\Template.bat» maybe prefaced with call (see call /? ). Or Cd /d «F:\- Big Packets -\kitterengine\Common\» & Template.bat .

CMD Cheat Sheet

CMD.exe

First thing to remember its a way of operating a computer. It’s the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common. It owes it roots to CPM, VMS, and Unix. It was used to start programs and copy and delete files. Also you could change the time and date.

For help on starting CMD type cmd /? . You must start it with either the /k or /c switch unless you just want to type in it.

Getting Help

For general help. Type Help in the command prompt. For each command listed type help (eg help dir ) or /? (eg dir /? ).

Some commands have sub commands. For example schtasks /create /? .

The NET command’s help is unusual. Typing net use /? is brief help. Type net help use for full help. The same applies at the root — net /? is also brief help, use net help .

References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.

WMIC is a multipurpose command. Type wmic /? .

Читайте также:  Mac os удалить apple music

Punctuation

Naming Files

Starting a Program

See start /? and call /? for help on all three ways.

There are two types of Windows programs — console or non console (these are called GUI even if they don’t have one). Console programs attach to the current console or Windows creates a new console. GUI programs have to explicitly create their own windows.

If a full path isn’t given then Windows looks in

The directory from which the application loaded.

The current directory for the parent process.

Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.

Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.

The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.

The directories that are listed in the PATH environment variable.

Specify a program name

This is the standard way to start a program.

In a batch file the batch will wait for the program to exit. When typed the command prompt does not wait for graphical programs to exit.

If the program is a batch file control is transferred and the rest of the calling batch file is not executed.

Use Start command

Start starts programs in non standard ways.

Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.

Start uses the Windows graphical shell — same as typing in WinKey + R (Run dialog). Try

Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths can also be typed without specifying a full path.

Also note the first set of quotes, if any, MUST be the window title.

Use Call command

Call is used to start batch files and wait for them to exit and continue the current batch file.

Other Filenames

Typing a non program filename is the same as double clicking the file.

Keys

Ctrl + C exits a program without exiting the console window.

For other editing keys type Doskey /? .

↑ and ↓ recall commands

ESC clears command line

F7 displays command history

ALT + F7 clears command history

F8 searches command history

F9 selects a command by number

ALT + F10 clears macro definitions

Also not listed

Ctrl + ← or → Moves a word at a time

Ctrl + Backspace Deletes the previous word

Home Beginning of line

End End of line

Ctrl + End Deletes to end of line

There are many possibilities to solve this task.

1. RUN the batch file with full path

The easiest solution is running the batch file with full path.

Once end of batch file Template.bat is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.

The current directory for the batch file Template.bat is the current directory of the current process. In case of Template.bat requires that the directory of this batch file is the current directory, the batch file Template.bat should contain after @echo off as second line the following command line:

Run in a command prompt window cd /? for getting displayed the help of this command explaining parameter /D . change to specified directory also on a different drive.

Run in a command prompt window call /? for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %

dp0 . drive and path of argument 0 which is the name of the batch file.

2. CALL the batch file with full path

Another solution is calling the batch file with full path.

Читайте также:  Исчез микрофон windows 10

The difference to first solution is that after end of batch file Template.bat is reached the batch processing continues in batch script containing this command line.

For the current directory read above.

3. Change directory and RUN batch file with one command line

There are 3 operators for running multiple commands on one command line: & , && and || .
For details see answer on Single line with multiple commands using Windows batch file

I suggest for this task the && operator.

As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat is successful.

4. Change directory and CALL batch file with one command line

This command line changes the directory and on success calls the batch file.

The difference to third solution is the return to current batch script on exiting processing of Template.bat .

5. Change directory and CALL batch file with keeping current environment with one command line

The four solutions above change the current directory and it is unknown what Template.bat does regarding

  1. current directory
  2. environment variables
  3. command extensions state
  4. delayed expansion state

In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat changes on environment for itself, it is advisable to use setlocal and endlocal .

Run in a command prompt window setlocal /? and endlocal /? for getting displayed the help of these two commands. And read answer on change directory command cd ..not working in batch file after npm install explaining more detailed what these two commands do.

Now there is only & instead of && used as it is important here that after setlocal is executed the command endlocal is finally also executed.

How to execute a .bat file from a C# windows form app?

What I need to do is have a C# 2005 GUI app call a .bat and several VBScript files at user’s request. This is just a stop-gap solution until the end of the holidays and I can write it all in C#. I can get the VBScript files to execute with no problem but I am unable to execute the .bat file. When I «click» in the C# app to execute the .bat file a DOS window opens up and closes very fast and the test .bat file does not execute — «Windows does not recognize bat as an internal or external command» is the error returned in the DOS box. If I simply doubl-click the .bat file or manually run it from the command prompt it does execute. I also need the .bat file to execute silently unless a user interaction is required — this script copies 11k+ files to folders on a networked machine and occasionally Windows «forgets» if the destination is a file or directory and asks for the user to tell it what it is (this is a whole other issue not for discussion here. needless to say I am annoyed by it).

So far in my C# source I have this :

I am aware that this code does not work — but it is essentially what I want it to do. What I am looking at is something like this for .bat files. I assume I have to tell the system to use cmd to run the .bat. I am at a loss as to how to do this. I have checked out this site which is for C# 2003. Not much help for me as I am very green with C#.

EDIT: Using Kevin’s post I attempted it again. Same solution script from that post but modified for me since I do not need to redirect:

How do I run a bat file in the background from another bat file?

I have a «setup» script which I run in the morning which starts all the programs that I need. Now some of those need additional setup of the environment, so I need to wrap them in small BAT scripts.

Читайте также:  Windows 10 pro или windows 10 домашняя для одного языка

How do I run such a script on Windows XP in the background?

CALL env-script.bat runs it synchronously, i.e. the setup script can continue only after the command in the env-script has terminated.

START/B env-script.bat runs another instance of CMD.exe in the same command prompt, leaving it in a really messy state (I see the output of the nested CMD.exe, keyboard is dead for a while, script is not executed).

START/B CMD env-script.bat yields the same result. None of the flags in CMD seem to match my bill.

7 Answers 7

Actually, the following works fine for me and creates new windows:

Combine that with parameters to start , such as /min , as Moshe pointed out if you don’t want the new windows to spawn in front of you.

Two years old, but for completeness.

Standard, inline approach: (i.e. behaviour you’d get when using & in Linux)

Notes: 1. CALL is paired with the .bat file because that where it usually goes.. (i.e. This is just an extension to the CMD /C CALL «foo.bat» form to make it asynchronous. Usually, it’s required to correctly get exit codes, but that’s a non-issue here.); 2. Double quotes around the .bat file is only needed if the name contains spaces. (The name could be a path in which case there’s more likelihood of that.).

If you don’t want the output:

If you want the bat to be run on an independent console: (i.e. another window)

If you want the other window to hang around afterwards:

Note: This is actually poor form unless you have users that specifically want to use the opened window as a normal console. If you just want the window to stick around in order to see the output, it’s better off putting a PAUSE at the end of the bat file. Or even yet, add ^& PAUSE after the command line:

Calling a .BAT file from another .bat file

I have a .bat file to which I can pass parameters.

Briefly, it runs an SQL to load Table1 on the Dev environment. Now, I want it to load multiple tables overnight. So, I set up a master .BAT which goes something like

If I submit MASTER_LOAD.BAT from cmd, it executes the load for Table1 but does not proceed to Table2 load. These are the last two lines of the WRAPPER.BAT

4 Answers 4

Your exit %error_status% command in LOAD_TABLE_WRAPPER.BAT is terminating your batch session, so your MASTER_LOAD.BAT never gets a chance to resume with the next call.

You can fix the problem simply by adding the /B option to your EXIT command

I almost never use EXIT without /B in a batch file (though there are times when /B is not wanted).

But another alternative is to run the called scripts via CMD instead of CALL.

There are a number of differences between the methods

CALL with EXIT /B

  • Comparatively fast
  • Can preserve environment variable values upon return (SETLOCAL is available if you don’t want to preserve values)
  • Called script inherits delayed expansion and extension states (enabled or disabled)

CMD /C

  • Comparatively slow
  • Cannot preserve environment variable values upon return. (You could write out the definitions to a file and load them back in upon returning to the master, but that is not convenient or efficient)
  • Called script always starts with default delayed expansion and extension states (Normally delayed expansion is disabled and extensions are enabled)

I would never recommend using CMD /C over CALL unless the called batch files have EXIT without the /B option and you cannot modify the batch file to add the /B option.

Оцените статью