[英]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 ..
我打算命令執行以下操作:
我一直在使用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.