Board logo

标题: [文件操作] 批处理怎样删除指定文件名的文件? [打印本页]

作者: sdhaozao    时间: 2015-2-28 10:37     标题: 批处理怎样删除指定文件名的文件?

想删除指定文件名的文件,自己百度加在论坛里面看了一下,写了如下代码,喜剧效果明显,求大神指点

本来是想把repetition.txt 里面的文件删除,结果把repetition.txt文件删除了....
  1. @echo off
  2. cd /d "D:\"
  3. del /s ('findstr /g:repetition.txt')
  4. pause
复制代码

作者: pcl_test    时间: 2015-2-28 12:05

回复 1# sdhaozao

repetition.txt里的内容格式是怎样的?
作者: DAIC    时间: 2015-2-28 12:17

  1. @echo off
  2. for /f "usebackq delims=" %%i in ("D:\repetition.txt") do (
  3.     if exist "%%i" (
  4.         del /f /q "%%i"
  5.     )
  6. )
复制代码

作者: sdhaozao    时间: 2015-2-28 13:07

回复 2# pcl_test


    全部是XXX.pdf 文件
作者: sdhaozao    时间: 2015-2-28 13:10

回复 3# DAIC

非常感谢!!!

刚开始不行,我调整了一下代码,就可以了。
  1. @echo off
  2. cd /d "d:\"
  3. for /f "usebackq delims=" %%i in ("D:\repetition.txt") do (
  4.     if exist "%%i" (
  5.         del /f /q "%%i"
  6.     )
  7. )
复制代码

作者: CrLf    时间: 2015-2-28 13:18

顶楼代码应该也删不到 repetition.txt 吧
作者: pcl_test    时间: 2015-2-28 13:27

本帖最后由 pcl_test 于 2015-2-28 13:36 编辑

回复 4# sdhaozao
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "usebackq delims=" %%a in ("D:\repetition.txt") do (
  4. for /r "D:\" %%b in (.) do (
  5. set "dir=%%b"
  6. if exist "!dir:~0,-1!%%a" (echo 删除文件"!dir:~0,-1!%%a" && del "!dir:~0,-1!%%a" /f /q)
  7. )
  8. )
  9. pause
复制代码
  1. @echo off
  2. cd /d "D:\"
  3. for /f "usebackq delims=" %%a in ("repetition.txt") do del "%%a" /f /q /s
  4. pause
复制代码

作者: sdhaozao    时间: 2015-2-28 13:31

回复 6# CrLf


不是顶楼的代码删除的.....就是我用顶楼的代码在不断修改的删除了repetition.txt ,就是不能读取repetition.txt 里面的信息
作者: DAIC    时间: 2015-2-28 13:35

回复 5# sdhaozao
  1. @echo off
  2. cd /d D:\
  3. for /f "delims=" %%i in (repetition.txt) do (
  4.     del /f /q "%%i" 2>nul
  5. )
复制代码
或者这样也行




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