JN_0029:批处理文件语法 创建快捷方式 管理员身份运行 批处理添加自启动程序




按顺序 延时启动程序:

@echo off ping localhost
-n 5 >null start C:\MyProgram\Sublime3\sublime_text.exe ping localhost -n 10 >null start "" "C:\Program Files\Mozilla Firefox\firefox.exe" exit

 

批处理添加自启动程序

reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /ve /t REG_SZ /d "c:\1.exe"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

创建快捷方式

:: by 琥珀君创建快捷方式
@echo off
echo set WshShell = WScript.CreateObject("WScript.Shell")>tmp.vbs
:: 设置快捷方式的放置位置,前提是目录已经存在
echo set oShellLink = WshShell.CreateShortcut("%~dp0" ^& "\me\Chrome.lnk")>>tmp.vbs
:: 设置快捷方式的目标位置,前提是目标目录已经存在
echo oShellLink.TargetPath ="%~dp0App\wg\chrome.exe">>tmp.vbs
echo oShellLink.WindowStyle ="1">>tmp.vbs
:: 设置快捷方式的目标位置,前提是目标目录已经存在
echo oShellLink.IconLocation = "%~dp0App\wg\chrome.exe">>tmp.vbs
echo oShellLink.Description = "">>tmp.vbs
echo oShellLink.WorkingDirectory = "%~dp0">>tmp.vbs
echo oShellLink.Save>>tmp.vbs
call tmp.vbs
del /f /q tmp.vbs

 

 

 

 

 

 

 

 

 

 

关闭cmd窗口

ctrl + c

 

启动本地EXE程序

start F:/MyProgram/Sublime3/sublime_text.exe

 

 

 

 

启动带空格路径的程序写法,杀死程序

bat脚本要打开有空格的路径,start后面必须加引号,exe路径也要加引号

echo
 
start "" "C:\Program Files\erwin\Data Modeler r9\erwin.exe"
 
:等待10秒
:ping 127.0.0.1 -n 10
 
:杀死进程
taskkill /f /im erwin.exe

echo

 

 

1,通过批处理添加环境变量

@echo off
::设置要永久加入到path环境变量中的路径度
set My_PATH=D:\AppFolder
set PATH=%PATH%;%My_PATH%
 
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" /t REG_EXPAND_SZ /d "%PATH%" /f
exit

 

SETX 永久设置用户环境变量
SETX /M 永久设置系统环境变量
SET 临时设置用户环境变量
SET /M  临时设置系统环境变量

setx/m Path "%Path%;D:\EsunWebServer\node_global"
exit

 

2,自动以管理员身份运行批处理文件

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
这里填写批处理代码
setx/m Path "%Path%;D:\EsunWebServer\node_global" exit

 

使用批处理加入防火墙白名单

add rule:
netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\MyApp.exe"

delete rule:
netsh advfirewall firewall delete rule name="MyApp" program="C:\MyApp.exe"

使用批处理将当前目录下的”*.exe”都加入到白名单中:
addfirewall.bat

for %%a in (*.exe) do (
netsh advfirewall firewall add rule name="%%a" dir=in action=allow program="%cd%\%%a"
)

 

posted @ 2020-05-14 11:47  琥珀君  阅读(228)  评论(0编辑  收藏  举报