繁体   English   中英

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

[英]Windows command to search directory for a specific name, then move its files to parent

我一直在尝试使命令起作用:

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

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

  1. 下降目录树并找到名为“ _Success”的目录
  2. 将文件(如果存在)移动到父目录。

我一直在使用help和以下主题来帮助我入门:

如何使用CMD将所有具有特定扩展名的文件从所有子目录移动到其父目录?

批处理脚本以在子文件夹中查找文件夹并获取文件夹路径

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

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\*.

编辑-似乎大部分工作,除了文件被移动到工作目录。

感谢@mihai_mandis在此链接中提供的解决方案:

将文件从子文件夹批量移动到父文件夹

我对它进行了以下修改:

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM