简体   繁体   English

如何将特定文件从多个子文件夹移动到各自的父文件夹? (Windows批处理)

[英]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(RDRD以实际删除目录。

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.

相关问题 将所有文件从多个子文件夹移至父文件夹 - Move all files from multiple subfolders into the parent folder 使用批处理脚本将所有文件从所有子文件夹移动到父文件夹 - Move All Files from all Subfolders to Parent Folder using batch script Windows批处理将文件从子文件夹复制到一个文件夹 - Windows batch copy files from subfolders to one folder 将子文件夹中的文件移动到文件夹并重命名Windows提示符 - Move files in subfolders to a folder and rename windows prompt 如何使用Windows cmd命令将所有文件移动到父文件夹 - How can I move all files to the parent folder using windows cmd command 如何从批处理文件中的特定文件夹及其子文件夹中查找名称不带下划线的所有jpeg文件,然后重命名 - How to find all jpeg files having name without underscore from a specific folder and its subfolders in batch file then rename it Windows 批处理脚本,用于从子文件夹和子文件夹中的文件中删除破折号 - Windows batch script to remove dashes from subfolders and the files within subfolders 如何使用 windows 命令将所有文件从父文件夹中移动到新的子文件夹中? - How to move all files excecpt one from parent folder into a new child folder using windows command? 如何将子目录中的文件批量移动到 Windows 中的一个文件夹下? - How to batch move files in subdirectories down one folder in Windows? 如何从文件名的一部分创建批处理文件夹,将文件移动到具有修改名称的文件夹中? - How do I create batch folder from part of file name, move files into folder with a modified name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM