实用的bat脚本

众所周知只要把bat脚本路径加入环境变量就可以在任意shell以命令的形式使用脚本。 为此我创建了一个文件夹专门用来存放自定义的bat脚本,然后将该文件夹的路径加入环境变量。 于是便可以使用win+R的方式快捷使用命令。部分脚本如下

mdr.bat


@echo off  
setlocal enabledelayedexpansion
:: 检查参数数量
if "%~1"=="" (
echo 请提供一个完整的文件路径作为参数。
exit /b 1
)
:: 获取文件路径和文件名  
set "full\_path=%~1"  
for %%i in ("%full\_path%") do set "folder\_path=%%~dpi" & set "file\_name=%%~nxi"

:: 创建文件夹  
if not exist "!folder\_path!" mkdir "!folder\_path!"

:: 在文件夹内创建文件  
set "file\_path=!folder\_path!!file\_name!"  
if not exist "!file\_path!" type nul > "!file\_path!"

echo File created successfully!  
endlocal

该脚本作用为一步创建文件夹和文件,如

mdr ./dir/text.txt

在当前目录创建dir文件夹,然后在文件夹内创建text.txt文件

new.bat

@echo off  
setlocal enabledelayedexpansion

:: 设置 VS Code 的路径  
set "vscode\_path=E:\\APP\\Microsoft VS Code\\Code.exe"

:: 初始化变量  
set "use\_vscode=0"  
set "create\_only=0"  
set "create\_on\_desktop=1"  
set "file\_to\_create="

:: 获取完整的文件名和扩展名  
set "file\_extension=%~x1"  
set "file\_name=%~n1"

:: 解析命令行参数  
:parse\_args  
if /i "%~1"=="" goto :end\_parse\_args  
if /i "%~1"=="-vs" (  
set "use\_vscode=1"  
shift /1  
goto :parse\_args  
)  
if /i "%~1"=="-no" (  
set "create\_only=1"  
shift /1  
goto :parse\_args  
)  
if /i "%~1"=="-d" (  
set "create\_on\_desktop=0"  
shift /1  
goto :parse\_args  
)  
set "file\_to\_create=%~1"  
shift /1  
goto :parse\_args

:end\_parse\_args

:: 检查文件名参数是否提供  
if "%file\_to\_create%"=="" (  
echo Usage: %~nx0 ^<filename\_with\_extension^> \[-vs\] \[-no\] \[-d\]  
exit /b 1  
)

:: 获取完整的文件名和扩展名  
set "full\_file\_name=%file\_to\_create%"

:: 设置创建文件的路径  
if %create\_on\_desktop%==1 (  
set "create\_path=%USERPROFILE%\\Desktop"  
) else (  
set "create\_path=."  
)

:: 构造完整的文件路径  
set "full\_path\_file\_name=%create\_path%\\%full\_file\_name%"

:: 如果文件不存在,则创建它  
if not exist "!full\_path\_file\_name!" (  
for %%i in ("%full\_path\_file\_name%") do set "folder\_path=%%~dpi" & set "file\_name=%%~nxi"

:: 创建文件夹  
if not exist "!folder_path!" mkdir "!folder_path!"  

:: 在文件夹内创建文件  
set "file_path=!folder_path!\!file_name!"  
if not exist "!file_path!" type nul > "!file_path!"  
)

:: 检查是否只需要创建文件而不打开  
if %create\_only%==1 (  
echo File created but not opened as requested.  
exit /b 0  
)

:: 根据参数决定是否使用 VS Code 打开文件  
if %use\_vscode%==1 (  
start "" "!vscode\_path!" "!full\_path\_file\_name!"  
) else (  
:: 根据文件后缀选择打开软件,或者默认使用 VS Code(这部分可以根据需要添加更多软件路径)  
set "open\_with="  
if /i "%file\_extension%"==".pdf" (  
set "open\_with=<your\_specific\_software\_path\_for\_pdf>"  
) else if /i "%file\_extension%"==".docx" (  
set "open\_with=C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE"  
) else if /i "%file\_extension%"==".xlsx" (  
set "open\_with=C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE"  
) else if /i "%file\_extension%"==".pptx" (  
set "open\_with=C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE"  
) else if /i "%file\_extension%"==".doc" (  
set "open\_with=C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE"  
) else if /i "%file\_extension%"==".xls" (  
set "open\_with=C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE"  
) else if /i "%file\_extension%"==".txt" (  
set "open\_with=C:\\Windows\\notepad.exe"  
) else if /i "%file\_extension%"==".c" (  
set "open\_with=E:\\APP\\VC6.0\\Common\\MSDev98\\Bin\\MSDEV.EXE"  
) else if /i "%file\_extension%"==".cpp" (  
set "open\_with=E:\\APP\\VC6.0\\Common\\MSDev98\\Bin\\MSDEV.EXE"  
) else if /i "%file\_extension%"==".r" (  
set "open\_with=E:\\APP\\RStudio\\rstudio.exe"  
) else if /i "%file\_extension%"==".R" (  
set "open\_with=E:\\APP\\RStudio\\rstudio.exe"  
)

:: 如果没有指定软件,则使用 VS Code  
if "!open_with!"=="" (  
    set "open_with=!vscode_path!"  
)  

:: 打开文件  
start "" "!open_with!" "!full_path_file_name!"
exit /b 0  
)

endlocal

该脚本作用是在桌面创建文件和文件夹,并自动使用设定 好的软件打开。使用方法

new dir/text.txt \[-d\]\[-vs\]\[-no\]

-d:改为当前目录下创建,通常用于打开文件夹后在控制台使用
-vs:使用vscode打开,不论是什么后缀的文件
-no:只创建不打开,作用同mdr命令

bing.bat

explorer "[https://cn.bing.com/search?q=%1"](https://cn.bing.com/search?q=%1&quot);

作用为使用bing搜索特定内容,也可以把后面改成其他的网址实现快捷打开网页

re.bat 立刻重启

shutdown -r -t 0

sd.bat 指定时间后关机(秒)

shutdown -s -t %1
posted @   咔白耶  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
鄂ICP备2024058921号
点击右上角即可分享
微信分享提示