[英]How can I move specific files from multiple subfolders to their respective parent folder? (Windows batch)
I have the following file and folder structure (using real names): 我有以下文件和文件夹结构(使用真实姓名):
Carabidae/Pterostichinae/FolderNameXXX/dor/StackXXX/files.tif
My problem is that I need to get one specific file, PM*.*, from the StackXXX folders into their respective /dor parent folders. 我的问题是我需要从StackXXX文件夹中将一个特定文件PM *。*放入各自的/ dor父文件夹中。 The StackXXX folder can then be deleted.
然后可以删除StackXXX文件夹。
There are hundreds of FolderName. 有数百个FolderName。 Ideally I would like a batch file I can run from the Carabidae folder.
理想情况下,我想要一个可以从Carabidae文件夹运行的批处理文件。
This needs to be a batch file because there will be new FolderNames added constantly. 这需要是一个批处理文件,因为会不断添加新的FolderNames。
After a lot of searching, I found a semi-working solution from this StackOverflow answer : 经过大量的搜索,我从StackOverflow的答案中找到了一个半工作的解决方案:
for /f "delims==" %%i in ('dir /a:d /b') do for /f "delims==" %%f in ('dir %%i /a:d /b') do (move "%%i\%%f\PM*.*" "%%i"&&rd "%%i\%%f" /s /q)
It moves the file and deletes the folder, just as I want. 它会移动文件并删除文件夹,就像我想要的那样。 But the problem is that it only works when run from a FolderName folder, which defeats the time-saving purpose of the script.
但问题是它只能在从FolderName文件夹运行时起作用,这会破坏脚本的省时目的。 I don't know how to modify it to recurse into subfolders so I can run it from the top folder.
我不知道如何修改它以递归到子文件夹,所以我可以从顶部文件夹运行它。
Thank you very much for any help! 非常感谢您的帮助!
@ECHO OFF
SETLOCAL
SET "sourcedir=u:\Carabidae"
FOR /f "tokens=1*delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\pm*.*" '
) DO IF EXIST "%%a" (
FOR %%p IN ("%%~dpa..\.") DO IF /i "%%~nxp"=="dor" (
ECHO %%a|FINDSTR /i "\\dor\\Stack" >NUL
IF NOT ERRORLEVEL 1 (
ECHO MOVE /y "%%~dpa\pm*.*" "%%~dpa..\"
ECHO RD /s /q "%%~dpa"
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances. 您需要更改
sourcedir
的设置以适合您的具体情况。
Find all of the pm*.*
files, filenames to %%a
找到所有
pm*.*
文件,文件%%a
Ensure the parent directory is dor
and ensure that \\dor\\stack\\
is in the path. 确保父目录为
dor
并确保\\dor\\stack\\
位于路径中。 If so, move the file(s) and remove the directory. 如果是,请移动文件并删除目录。
The if exist
gate ensure no hiccoughs if a target directory contains more than one pm*.*
file. if exist
目标门如果目标目录包含多个pm*.*
文件, if exist
确保没有打嗝。
The required MOVE commands are merely ECHO
ed for testing purposes. 所需的MOVE命令仅仅
ECHO
编用于测试目的。 After you've verified that the commands are correct , change ECHO(MOVE
to MOVE
to actually move the files. Append >nul
to suppress report messages (eg. 1 file moved
) 在确认命令正确后 ,更改
ECHO(MOVE
to MOVE
以实际移动文件。附加>nul
以抑制报告消息(例如, 1 file moved
)
The required RD commands are merely ECHO
ed for testing purposes. 所需的RD命令仅仅
ECHO
编用于测试目的。 After you've verified that the commands are correct , change ECHO(RD
to RD
to actually delete the directories. 在确认命令正确后 ,更改
ECHO(RD
到RD
以实际删除目录。
Add >nul
at the end of the move
command to suppress the move-report if required. 如果需要,在
move
命令结束时添加>nul
以禁止移动报告。
As usual, I'd suggest you test against a representative subtree first. 像往常一样,我建议你首先测试代表性子树。
Here is a possible solution, given that only the XXX
parts in your path sample are variable: 这是一个可能的解决方案,因为只有路径示例中的
XXX
部分是可变的:
rem // Enumerate `FolderName*` directories:
for /D %%R in ("Carabidae\Pterostichinae\FolderName*") do (
rem // Enumerate `Stack*` sub-directories within `dor` sub-directories:
for /F "delims= eol=|" %%D in ('dir /B /A:D "%%~R\dor\Stack*"') do (
rem // Check for `PM*.*` files in `Stack*` sub-directories:
(
rem // Enumerate `PM*.*` files:
for /F "delims= eol=|" %%F in ('dir /B /A:-D "%%~R\dor\%%D\PM*.*"') do (
rem /* Move `PM*.*` file one directory level up, overwriting
rem an already existing file, if applicable: */
ECHO move /Y "%%~R\dor\%%D\%%F" "%%~R\dor\%%F"
)
) && (
rem /* Remove `Stack*` sub-directory after file movement;
rem this is skipped if no `PM*.*` files have been found in the `Stack*`
rem sub-directory, so when the `for /F %%F` loop did never iterate: */
ECHO rd /S /Q "%%~R\dor\%%D"
)
)
)
After having successfully tested whether or not the correct items are returned, remove the upper-case ECHO
commands to actually move PM*.*
files and remove Stack*
directories! 成功测试是否返回了正确的项目后, 删除大写的
ECHO
命令以实际移动PM*.*
文件并删除Stack*
目录!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.