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>
相关推荐
YCOSA20252 小时前
迎双节 雨晨 Windows 11 IoT 企业版 LTSC 26H1 轻装 28000.2956
windows·物联网
csdn_aspnet3 小时前
Windows 如何隐藏 IIS Web 服务响应头中的 IIS Server 版本信息
windows·iis·url rewrite
AxureMost4 小时前
VideoProc Converter AI 视频转换/下载/超分辨率
windows
李小白杂货铺5 小时前
从 macOS 免密 SSH 到 Windows 10 完整配置指南
windows·win10·openssh·踩坑·ssh服务器·免密连接·安装和配置
线上放牧人14 小时前
Windows删除图标缓存
windows·python·pyqt
Koi慢热15 小时前
DayDayMap学术社区体验:卫星网络测绘专题用下来怎么样
网络·人工智能·windows·web安全·网络安全
web守墓人16 小时前
【goed/ui】自定义组件设计思想篇
linux·windows·ui·golang
2601_9623649719 小时前
红外活体模组,Windows 设备安全身份入口解析
windows·安全·电脑
易点云徐源20 小时前
(已解决)Photoshop 2020 点击“选择主体”闪退:不用重装,1 个配置切回旧算法解决
windows·photoshop