Board logo

标题: [文件操作] 【已解决】批处理比对两个文件夹,删除不存在的文件及子文件夹 [打印本页]

作者: 大地    时间: 2020-11-30 08:21     标题: 【已解决】批处理比对两个文件夹,删除不存在的文件及子文件夹

本帖最后由 大地 于 2020-11-30 17:30 编辑

.
有两个文件夹分别为123和456,其下分别有许多文件及子文件夹,没有更深的子目录,有相同的文件和文件夹名,也有各不相同的。.
.
现假设123文件夹下有文件a.dll,假如在456文件夹下不存在这个a.dll,则删除123文件夹下的这个a.dll
如果存在a.dll,则不删保留,着个检查往下...
.
现假设123文件夹下有子文件名AA,假如在456文件夹下不存在这个子文件夹名AA,,则删除这个123下面的子文件夹AA
如果存在AA,则不删保留,着个检查往下...
.
456文件夹下的文件及子文件夹原汁原味保留不删不变动。
如何写,望大神代码一下,谢谢。



已解决一半,前半部份已搞定,还有一半不会,文件夹的变量代码怎么表达??
  1. cd /d "%~dp0"
  2. for %%i in (123\*) do (
  3.     if not exist "456\%%~nxi" del "123\%%~nxi"
  4. )
  5. pause
复制代码
或:
  1. cd /d "%~dp0"
  2. for %%i in (dir /s/b/a-d "123\*") do (
  3.     if not exist "456\%%~nxi" del "123\%%~nxi"
  4. )
  5. pause
复制代码
文件夹还不会弄????
这里哪里不对,哪位大神邦我改一下,谢谢了
  1. cd /d "%~dp0"
  2. for %%i in (dir /s/b/ad "123\*") do (
  3.     if not exist "456\%%j" RD /s /Q "123\%%i"
  4. )
  5. pause
复制代码

作者: ivor    时间: 2020-11-30 15:01

本帖最后由 ivor 于 2020-11-30 16:17 编辑
  1. powershell "$456=(dir 456).Name;dir 123 | %{($456 -notcontains $_.Name) -and (Remove-Item $_.FullName  -Recurse -verbose)} | Out-Null"
复制代码

作者: Batcher    时间: 2020-11-30 15:33

回复 1# 大地
  1. @echo off
  2. cd /d "%~dp0"
  3. for /f "delims=" %%i in ('dir /b /ad "123\*"') do (
  4.     if not exist "456\%%i" (
  5.         rd /s /q "123\%%i"
  6.     )
  7. )
  8. for %%i in ("123\*") do (
  9.     if not exist "456\%%~nxi" (
  10.         del /f /q "123\%%~nxi"
  11.     )
  12. )
复制代码

作者: 大地    时间: 2020-11-30 17:29

本帖最后由 大地 于 2020-12-12 20:50 编辑

多谢大师 测式成功

记录作个标记
  1. @echo off
  2. cd /d "%~dp0"
  3. attrib %CD%\123\"*" -s -r -a -h /d /s
  4. attrib %CD%\123\*.* -s -r -a -h /d /s
  5. attrib %CD%\456\"*" -s -r -a -h /d /s
  6. attrib %CD%\456\*.* -s -r -a -h /d /s
  7. for /f "delims=" %%i in ('dir /b /ad "123\*"') do (
  8.     if not exist "456\%%i" (
  9.         rd /s /q "123\%%i"
  10.     )
  11. )
  12. for %%i in ("123\*") do (
  13.     if not exist "456\%%~nxi" (
  14.         del /f /q "123\%%~nxi"
  15.     )
  16. )
  17. pause
复制代码





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