开发者

BAT script to run different commands on different machines

开发者 https://www.devze.com 2023-02-01 06:39 出处:网络
I need to write a .bat script that executes different sets of commands based on which machine it is running on.

I need to write a .bat script that executes different sets of commands based on which machine it is running on.

If it's running on a machine called "analysis" it needs to run:

mv *.pdf D:\Export\Worksheet
mv *.csv D:\Export\Statistics

开发者_StackOverflow社区Otherwise it needs to run:

mv *.pdf C:\Export\Worksheet
mv *.csv C:\Export\Statistics

Any pointers in the right direction would be appreciated.


The COMPUTERNAME environment variable has what you're looking for.

Something like the following should help you:

if /i "%COMPUTERNAME%" == "analysis" (
    mv *.pdf D:\Export\Worksheet
    mv *.csv D:\Export\Statistics
) else (
    mv *.pdf C:\Export\Worksheet
    mv *.csv C:\Export\Statistics
)


You want to look into the environment variable COMPUTERNAME.
Start with

echo %COMPUTERNAME%

Basically, I think you want:

if %COMPUTERNAME% == analysis ....


You can use %COMPUTERNAME% as others have pointed out. You may want to make your script more readable by defining a local variable containing the path that varies between the machines and then using that subsequently to avoid repetition.


I normally use something like:

::Common settings
SET X=....
....

GOTO %COMPUTERNAME%

:COMPUTER1
::Commands for #1
....
GOTO :EOF

:COMPUTER2
::Commands for #2
....
GOTO :EOF
0

精彩评论

暂无评论...
验证码 换一张
取 消