【问题标题】:Windows command to search directory for a specific name, then move its files to parentWindows 命令在目录中搜索特定名称,然后将其文件移动到父目录
【发布时间】:2017-11-18 08:17:42
【问题描述】:

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

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 和以下线程来帮助我入门:

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

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

【问题讨论】:

  • 我会推荐 PowerShell 而不是 cmd.exe shell 脚本。

标签: windows cmd


【解决方案1】:

感谢@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

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2019-09-26
    • 2014-06-13
    相关资源
    最近更新 更多