ファイル削除および空きフォルダ削除
实现方案1:findstr関数を使用
rem ファイル削除および空きフォルダ削除(実現方式①).bat
@echo off
chcp 65001
setlocal enabledelayedexpansion
rem 特にファイル名にて日本語がある場合、
rem ファイルリストのENCODEはANSIである必要
rem でなければ、findstr関数が効かない。
set fileList="D:\temp\fileList.txt"
rem echo %0
rem echo %1
rem type %fileList%
echo ファイル削除処理開始
for /r "%1" %%f in (*.*) do (
  set fileFlg=""
  echo 処理ファイル:%%~nxf
  for  /f %%A in ('type %fileList% ^| findstr /i /c:"%%~nxf"') do (
    rem echo %%A
    set fileFlg=1
  )
  rem echo !fileFlg!
  if !fileFlg!=="" (
    echo Deleting: %%f
    del %%f
  )
)
echo ファイル削除処理終了
echo.
echo 空フォルダ削除処理開始
set cmd="dir %1 /ad /s /b | sort /r"
for /f "delims=" %%d in ('%cmd%') do (
  rd /q %%d>nul
)
echo 空フォルダ削除処理終了
pause
 
 
实现方案2:findstr代わりにfor繰り返しを使用
rem ファイル削除および空きフォルダ削除(実現方式②).bat
@echo off
chcp 65001
rem 遅延設置
setlocal enabledelayedexpansion
rem findstrを使わないため、ファイル一覧がUTF-8形式でも大丈夫
set fileList="D:\temp\fileList2.txt"
rem echo %0
rem echo %1
 type %fileList%
set filename=""
for /r "%1" %%f in (*.*) do (
    rem echo %%~nxf
    set filename=%%~nxf
    set fileExist=
    rem findstr代わりにfor繰り返しを使用
    for  /f %%A in ('type %fileList%') do (
        echo %%A !filename!
        if "!filename!"=="%%A" (
          rem echo ファイル一覧に存在する
          set fileExist=1
        )
    )
    if "!fileExist!"=="" (
      echo %%f
      del %%f
    )
    echo.
)
echo 空フォルダ削除処理開始
set cmd="dir %1 /ad /s /b | sort /r"
for /f "delims=" %%d in ('%cmd%') do (
  rd /q %%d>nul
)
echo 空フォルダ削除処理終了
pause
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号