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>
相关推荐
Vex2une1 天前
JDK 8 下载安装与环境变量配置完整指南(Windows新手图文教程)
java·windows·环境配置·环境变量·jdk8·jdk下载安装·java入门教程
星空孤辰1 天前
【camera 008】android camera id 注册、状态监控等状态变化 全流程梳理
android·windows
全方位办公软件1 天前
Windows音频服务架构解析:AudioSrv与AudioEndpointBuilder排障
windows·命令行·powershell·音频服务·驱动修复·mmdevices·audiosrv
Vex2une2 天前
电脑装双系统怎么弄?Win+Win 与 Win+Linux 安装及启动菜单管理全攻略
linux·windows·电脑·uefi·双系统安装·bcdedit启动项
无情的西瓜皮2 天前
eMudhra 根证书自 11-16 起失去微软产品默认信任:4 步 PowerShell 自查你的 Windows 是否已收到变更(Win11 实测)
windows·powershell
Funlang3 天前
在 Windows NT 3.5 上跑 Fun
windows
xx z4 天前
directx修复工具下载(DirectX Repair 增强版 使用说明)
windows·系统·工具·软件
xx z4 天前
pcl2启动器下载(Minecraft PCL2 启动器 最新版 安装与使用说明)
windows·游戏·工具·软件
凯瑟琳.奥古斯特4 天前
我开源了一个 Windows 截图取字工具:全局热键 + 离线 OCR + 标注打码,不上传任何数据
windows·ocr
Funlang5 天前
在 Windows 95 上跑 Fun
windows