Windows命令在目录中搜索特定名称,然后将其文件移动到父

时间:2017-06-15 18:13:09

标签: windows cmd

我一直试图让命令工作:

for /r /d %d in (*) do if /I "%~nxd"=="_Success" for /f %f in (%d\*) do if exist %f move %f ..

我打算让命令执行以下操作:

  1. 下降目录树并查找名为" _Success"
  2. 的目录
  3. 将文件(如果存在)移动到父目录。
  4. 我一直在使用help和跟随线程让我开始:

    How to move all files with specific extension from all subdirectories to their parent using CMD?

    Batch Script to Find a Folder inside Sub Folders and get Folder Path

    但是我在输出中收到了一些错误:

    C:\migration_files\vault>if /I "document" == "_Success" for /F %f in (C:\migration_files\vault\document\*) do if exist %f move %f ..
    ...
    C:\migration_files\vault>if /I "_Success" == "_Success" for /F %f in (C:\migration_files\vault\faqs\_Success\*) do if exist %f move %f ..
    The system cannot find the file C:\migration_files\vault\faqs\_Success\*.
    C:\migration_files\vault>if /I "_Success" == "_Success" for /F %f in (C:\migration_files\vault\patch\_Success\*) do if exist %f move %f ..
    The system cannot find the file C:\migration_files\vault\patch\_Success\*.
    

    编辑 - 它似乎主要工作,除了文件被移动到工作目录。

1 个答案:

答案 0 :(得分:0)

感谢@mihai_mandis在此链接上的解决方案:

batch moving file from subfolder to parent folder

我稍微修改了以下内容:

@echo  off
setlocal enabledelayedexpansion

for /r /d %%d in (*) do (
  if /I "%%~nxd"=="_Success" (
    for /f %%f in ("%%d\*") do (
      call :GETPARENTPARENT "%%f" ret
      echo ret=!ret!
      move /Y "%%f" "!ret!"
    )
  )
)

goto:EOF

:GETPARENTPARENT
set fileP=%1
echo received=%fileP%
for %%a in (%fileP%) do (
    set parent=%%~dpa
    cd !parent!\..
    set PPPath=!cd!
    for %%x in ("!PPPath!") do (
         set "%~2=%%~dpnx"
    )
)
GOTO:EOF