简体   繁体   English

将特定文件移出子文件夹并删除所述子文件夹

[英]Move specific files out of subfolders and delete said subfolders

I recently exported the HDD on my panasonic camera to my notebook and noticed that the video files weren't ordered by name, but by their parent directory and said parent directory was clouded with a bunch of miscellaneous files. 我最近将我的松下相机上的HDD导出到笔记本上,并注意到视频文件不是按名称排序的,而是按其父目录排序的,并且说父目录中充斥着许多其他文件。


To put things into perspective, the directory tree looks something like this: 为了使事情更直观,目录树如下所示:

  • \\Panasonic
    • \\PRG00A
    • \\PRG00B
    • ...
    • \\PRG069

Panasonic is located inside a bunch of folders, hence why I would like to put my batch file alongside \\Panasonic . Panasonic位于一堆文件夹中,因此为什么要将批处理文件放在\\Panasonic旁边。 And have it work relatively to its location. 并使其相对于其位置起作用。


So basically I want to create a batch file move.bat , which shall traverse the subdirectories of \\Panasonic and move out any video files (with extension .MOD to simplify things) and afterwards delete the parent directory (eg \\PRG00B ). 因此,基本上我想创建一个批处理文件move.bat ,该文件将遍历\\Panasonic的子目录并移出所有视频文件(扩展名为.MOD以简化操作),然后删除父目录(例如\\PRG00B )。

The result would be that the \\Panasonic directory only includes video files instead of sub-directories with a bunch of rubbish. 结果是\\Panasonic目录仅包含视频文件,而不包含带有一堆垃圾的子目录。


What I've got so far (keep in mind that this is my first batch script, and I haven't even tested it fully). 到目前为止,我已经掌握了什么(请记住,这是我的第一个批处理脚本,我甚至还没有对其进行完整的测试)。 The choice to continue doesn't work, by the way. 顺便说一句,继续的选择无效。 Not sure why, though. 不过不知道为什么。

@echo off
cls
set dirName=%~dp0Panasonic
goto question

:start
goto move
goto end

:move
for /D %%G in ("%cd%") do (
    for %%I in ("%%G") do (
        if %%I equ "*.MOD" (
            move /Y %%I %dirName%
        )
    )
    rmdir /s /q %%G
)

:end
echo Done.
pause
endlocal
exit

:question
set /P c="Are you sure you want to proceed with moving video files from %dirName%? [Y/N]"
if /I %c% equ 'y' (
    echo Moving files...
    goto start
) else (
    goto end
)

Once again, this is my first time creating a batch file, so any help is much appreciated! 再次,这是我第一次创建批处理文件,因此,非常感谢您的帮助!

you are almost there, but just need to fix a couple of things, very easy to fix, in fact, you just need to simplify a lot your code. 您已经差不多了,但是只需要修复几件事情,非常容易修复,实际上,您只需要简化很多代码即可。

Just in three simple steps 仅需三个简单步骤

Step 1. To loop over all the directories you already had it right, your friend is for /d 步骤1.要遍历您已经拥有的所有目录,您的朋友for /d

for /d %%a in (*) do echo %%a

Step 2. To move all the .mod files in each of the directories found, to its parent directory or one directory up in the hierarchy, that happens to be the current directory, you just need to 步骤2.要将找到的每个目录中的所有.mod文件移动到其父目录或层次结构中恰好是当前目录的一个目录中,您只需要

move %%a\*.mod .

don't use /y option, so it will not overwrite existing files already moved to the parent directory (You will have the opportunity the check the results later. Keep reading) 不要使用/y选项,因此它不会覆盖已经移到父目录的现有文件(您将有机会稍后检查结果。请继续阅读)

Step 3. And finally, remove the directory, 第3步。最后,删除目录,

rd %%a

but don't use /s , so it will only work it the directory is empty, that is, if you have successfully moved out all of the files it contained. 但不要使用/s ,因此只能在目录为空的情况下使用,也就是说,如果您已成功移出它包含的所有文件。 This way you can then browse thru them to see what is left without losing any data. 这样,您就可以浏览它们以查看剩下的内容而不会丢失任何数据。

So, your moveupallmod.bat becomes simply 因此,您的moveupallmod.bat变得简单

@echo off
for /d %%a in (*) do (
   move "%%a\*.mod" .
   rd "%%a" 
)

and that's all! 就这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM