Board logo

标题: [文件操作] [已解决]批处理删除隐藏文件时,目录名含有英文!怎么办 [打印本页]

作者: microsoft_yaw    时间: 2024-4-27 13:47     标题: [已解决]批处理删除隐藏文件时,目录名含有英文!怎么办

各位大佬好!
请问使用Bat批处理进行批量删除目录下指定文件(普通文件和只读隐藏文件)的时候,如果目录名称 或 路径含有英标的!特殊符号(此时开启了延迟变量扩展)
那么此时删除含有!特殊符号的目录内的普通/只读隐藏文件时,会返回 The system cannot find the file specified(系统找不到指定的文件)的错误
因为Bat程序会将路径中的!识别为命令,从而系统找不到指定文件。
我的代码如下:
  1. @echo off & chcp 65001>nul & setlocal enabledelayedexpansion
  2. set "folder_path=%~dp0Working\%1"
  3. set "del_files_suffix_list=jpg txt url html"
  4. REM 删除普通文件中的123.jpg、*.txt、*.url、*.html
  5. for /f "delims=" %%i in ('dir /b /s /on "!folder_path!"') do (
  6. if "%%~nxi" equ "123.jpg" (echo. & echo Delete:%%i & del /s /q "%%i")
  7. for %%j in (!del_files_suffix_list!) do (
  8. if "%%~xi" neq ".jpg" (
  9. if "%%~xi" equ ".%%j" (echo. & echo Delete:%%i & del /s /q "%%i")
  10. )
  11. )
  12. REM 删除只读、隐藏文件中的*.jpg、*.txt、*.url、*.html
  13. for /f "delims=" %%i in ('dir /ar /ah /b /s /on "!folder_path!"') do (
  14. for %%j in (!del_files_suffix_list!) do (
  15. if "%%~xi" equ ".%%j" (echo. & echo Delete:%%i & del /ar /ah /s /q "%%i")
  16. )
  17. )
复制代码
结果返回:
  1. Delete:D:\Quick_Access\Desktop\Picture-Tools\Working\100\123123\123.jpg
  2. The system cannot find the file specified.
复制代码
原目录名称为:123!123
---------------------------------
其它补充:
[1].部分二级目录名称含有外语(日语/韩语/英语),为了防止遍历时出现乱码,所以使用了chcp 65001
[2].第9行代码的写法,是因为需要删除只读隐藏文件的名称为全中文,接上点,由于使用了chcp 65001去适配外语目录名称,现在无法正常识别变量中的中文字符.为了防止删除普通文件和出现乱码的情况,所以添加了此代码(如有更好的方法,感谢大佬的贡献!)
---------------------------------
我的需求是:
[1].在目录名称含有!特殊符号下,删除指定的普通/只读隐藏文件(名称+后缀)
[2].在目录名称含有!特殊符号下,删除指定的普通/只读隐藏文件(后缀)
[3].尽可能的使用for循环完成操作(学习目的) - *重点*
---------------------------------
最后最后,感谢各位提供方案的大佬们!!!
作者: 77七    时间: 2024-4-27 14:09

楼主的代码有一些错误,还有一些不好理解的地方,不如直接描述下需求,重新写好了。
作者: microsoft_yaw    时间: 2024-4-27 14:18

回复 2# 77七


    代码已简化,大致意思是,接收主Bat程序传递的参数%1(一级目录名称),使用dir遍历该路径所有文件,判断该遍历文件是否等于123.jpg,如果等于则删除,现在的问题是目录名称中含有英文!特殊符号,使用del 删除文件时,结果返回找不到指定文件
作者: 77七    时间: 2024-4-27 14:26

回复 3# microsoft_yaw


  
  1. @echo off & chcp 65001 >nul
  2. set "folder_path=%~dp0Working\%1"
  3. set "del_files_suffix_list=jpg txt url html"
  4. del /s /q "%folder_path%\123.jpg"
  5. pause
复制代码

作者: microsoft_yaw    时间: 2024-4-27 14:50

回复 4# 77七


    可是我需要批量处理呀,延迟变量扩展和for循环是不可缺的
作者: czjt1234    时间: 2024-4-27 14:57

ENDLOCAL
del 111!222\123.jpg
SETLOCAL ENABLEDELAYEDEXPANSION
作者: 77七    时间: 2024-4-27 15:19

回复 5# microsoft_yaw


   4楼代码就是批量处理,先dir ,再判断是否等于,再删除,属于多此一举。
作者: microsoft_yaw    时间: 2024-4-27 16:26

回复 7# 77七


    第一次发帖,有些遗漏,已补充,不好意思
作者: 77七    时间: 2024-4-27 17:44

回复 8# microsoft_yaw


   
现在的代码为什么还会用到 setlocal enabledelayedexpansion 呢
作者: terse    时间: 2024-4-27 17:49

有没有试过在FOR里开启延迟
作者: microsoft_yaw    时间: 2024-4-27 17:54

回复 9# 77七


    使用for循环不需要开启延迟变量扩展吗?
作者: 77七    时间: 2024-4-27 18:02

回复 11# microsoft_yaw


   
不是必须。
作者: microsoft_yaw    时间: 2024-4-27 18:05

回复 12# 77七


    我知道可以直接使用del /s /q 和 del /ar /ah /s /q 删除指定文件,但我想了解学习一下for如何解决这种情况(目录名称含有!特殊符号)
作者: 77七    时间: 2024-4-27 18:23

回复 13# microsoft_yaw


  
  1. @echo off & chcp 65001 >nul
  2. set "folder_path=%~dp0Working\%1"
  3. for /f "delims=" %%i in ('dir /b /s /a-d "%folder_path%\*.jpg"') do (
  4.         set str=%%i
  5.         setlocal enabledelayedexpansion
  6.         echo !str!
  7.         endlocal
  8. )
  9. pause
复制代码
开就是这样,也可以不开
作者: microsoft_yaw    时间: 2024-4-27 18:35

本帖最后由 microsoft_yaw 于 2024-4-27 19:17 编辑

回复 14# 77七

大佬,这样子如何?
  1. @echo off & chcp 65001>nul & setlocal enabledelayedexpansion
  2. set "del_files_list=123.jpg *.txt *.url *.html"
  3. for %%i in (!del_files_list!) do (del /s /q "%folder_path%\%%i")
  4. del /ar /ah /s /q "%folder_path%\*"
复制代码
这个代码还没有尝试,应该可以去除setlocal enabledelayedexpansion
作者: 77七    时间: 2024-4-27 19:33

回复 15# microsoft_yaw


  
  1. @echo off & chcp 65001>nul
  2. set "folder_path=%~dp0Working\%1"
  3. REM 删除普通文件中的123.jpg、*.txt、*.url、*.html
  4. del /s /q "%folder_path%\123.jpg"
  5. for %%a in (txt url html) do (
  6. del /s /q "%folder_path%\*.%%a"
  7. )
  8. REM 删除只读、隐藏文件中的*.jpg、*.txt、*.url、*.html
  9. for %%a in (jpg txt url html) do (
  10. del /s /q /ar "%folder_path%\*.%%a"
  11. del /s /q /ah "%folder_path%\*.%%a"
  12. )
  13. pause
复制代码

作者: aloha20200628    时间: 2024-4-30 23:17

本帖最后由 aloha20200628 于 2024-4-30 23:31 编辑


开启变量延迟后,字符串变量无论是否被双引号包裹,其中的!字符都会被系统转义求值,除非!字符已用转义方法处理。
删除只读文件须用 /f 参数
再给一个保留for句式的简化版...
  1. @echo off
  2. pushd "%~dp0Working\%~1"
  3. for %%m in (" ", "/ah") do (del /s /f /q %%~m 123.jpg *.txt *.url *.html)>nul 2>nul
  4. popd&pause&exit/b
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2