windows批处理脚本(bat)问题解决方案大全——持续更新

1.判断指定文件或文件夹是否存在

bash 复制代码
if exist <file/fileName>(
	echo "文件不存在"
)

2.取非

在bat脚本中,使用not取非

bash 复制代码
if exist <file>(
	echo "文件存在"
)

if not exist <file>(
	echo "文件不存在"
)

3.判断是文件还是文件夹

3.1.判断是否为文件

dir /a-d

/a 是按属性筛选

-d 表示排除目录(d 是 directory 属性,前面的 - 是"非")

使用errorlevel的值判断结果

如果"路径"是一个文件,它能被列出来 → 成功 → errorlevel = 0

如果"路径"是一个目录,因为排除了目录,它什么都列不出来 → 报错"File Not Found" → errorlevel = 1

3.2判断是否为文件夹

dir /ad

/a 是按属性筛选

d 表示排除目录(d 是 directory 属性)

使用errorlevel的值判断结果

如果"路径"是一个文件,它能被列出来 → 成功 → errorlevel = 0

如果"路径"是一个目录,因为排除了目录,它什么都列不出来 → 报错"File Not Found" → errorlevel = 1

bash 复制代码
//判断是否为文件
dir /a-d <file> >nul 2>nul
if %errorlevel%==0(
	echo "是文件"
)

dir /ad <file> >nul 2>nul
if %errorlevel%==0(
	echo "是文件夹"
)

4.删除文件

bash 复制代码
del <file>

//强制删除
del /f <file>

//静默删除
del /p <file>

//递归子目录
del /s <file>

5.删除文件

bash 复制代码
rmdir <file>

//静默删除
rmdir /p <file>

//递归子目录
rmdir /s <file>
相关推荐
?re?ta?rd?ed?15 小时前
C++11新特性,{}与左值
c++·windows
必须得开心呀18 小时前
windows10系统任务栏图标消失修复
windows
zl_dfq18 小时前
Windows 下 UDP 套接字编程指南
网络·windows·udp
学习论之费曼学习法19 小时前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
少年的范儿20 小时前
Windows 安装 MySQL 8.4 教程(ZIP 解压版):配置 my.ini、初始化数据库与启动服务
数据库·windows·mysql
胖纸不争20 小时前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
2501_915909061 天前
Windows 上用 VS Code 调试 iPhone 真机 App,KXBuild 断点、崩溃定位实操
ide·windows·vscode·ios·iphone·个人开发·敏捷流程
情 九1 天前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
懂点Tech1 天前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云梦谭1 天前
让 Linux 主动连接 Windows:配置 Windows 11 SSH 服务端
linux·windows·ssh