簡體   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