简体   繁体   English

根据子文件夹中的文件类型创建子文件夹,然后移动该类型的所有文件

[英]Create subfolders based on file types in subfolder and move everything of that type

I have a folder in My Documents called Misc, and in that folder is 400 subfolders which has become a bit of a mess. 我的文档中有一个名为“杂项”的文件夹,该文件夹中有400个子文件夹,这变得有些混乱。 Within each of those subfolders are different file types, i'd like to be able to create subfolders in each subfolder based on the filetype, and move anything of that file type into the new subfolder. 每个子文件夹中都有不同的文件类型,我希望能够基于该文件类型在每个子文件夹中创建子文件夹,并将该文件类型的任何内容移动到新的子文件夹中。

So i'd like to have something like this: 所以我想有这样的事情:

C:\Users\Personal\Documents\Misc\
 - Subfolder1
  - PDF
  - PNG
  - PDF
- Subfolder2
  - XLSX
  - DOCX
  - PDF
- Subfolder3
  - M4A
  - MKV
  - PNG

There's no consistency with what file types are in each subfolder which is where i'm having trouble. 每个子文件夹中的文件类型不一致,这是我遇到麻烦的地方。 Is this even possible to do? 这有可能吗?

So far what i've been doing is going into each folder and running the following batch file 到目前为止,我一直在做的是进入每个文件夹并运行以下批处理文件

for %%a in (.) do md "%%~na PNG" &move "*.png" ".\%%~na PNG\"
for %%a in (.) do md "%%~na Documents" &move "*.docx" ".\%%~na Documents\"
for %%a in (.) do md "%%~na PDF" &move "*.pdf" ".\%%~na PDF\"
for %%a in (.) do md "%%~na Spreadsheets" &move "*.xlsx" ".\%%~na Spreadsheets\"

Each time I come across a new file extension, I add a new line. 每次遇到新的文件扩展名时,我都会添加一行。 But ideally i'd only want the folders creating if the file extension is in the folder, and I wouldn't have to manually go into each folder to run it. 但是理想情况下,我只希望在文件扩展名位于文件夹中的情况下创建文件夹,而我不必手动进入每个文件夹来运行它。

Your for loops do not access the file system because there are no wildcards , so they iterate only once and return . 您的for循环不会访问文件系统,因为没有通配符 ,因此它们仅循环一次并返回. in their meta-variable %%a . 在其元变量%%a

Anyway, not sure if I understood correctly what you want, but give the following code a try: 无论如何,不​​确定我是否正确理解了您想要的内容,但是尝试以下代码:

@echo off
rem // Iterate through all immediate sub-directories of the target root directory:
for /D %%D in ("%UserProfile%\Documents\Misc\*") do (
    rem // Iterate through all files in each sub-directory:
    for %%F in ("%%~D\*.*") do (
        rem /* Remove the leading `.` from the name extension of the current file;
        rem    if the files has got no extension, this loop does not iterate: */
        for /F "tokens=* delims=." %%E in ("%%~xF") do (
            rem /* Create sub-directory from file name extension;
            rem    suppress errors if it already exists from previous iteration: */
            md "%%~D\%%E" 2> nul
            rem // Actually move the current file into the sub-directory:
            move /Y "%%~F" "%%~D\%%E"
        )
    )
)

As your use of tags was limited to just , and the answer you've already accepted is a , I have updated the tags you've used and included this example based upon your stated requirements: 由于您对标签的使用仅限于 ,并且您已经接受的答案是一个 ,因此我根据您的要求更新了您已使用的标签并包括了该示例:

$ds=GCI "C:\Users\Personal\Documents\Misc"|?{$_.PSIsContainer}|Select -Exp FullName
ForEach($f In $ds){
    $gs=GCI $f|?{(!($_.PSIsContainer))}|Group Extension
    ForEach($g In $gs){
        $n=Join-Path $f($g.Name.Substring(1,($g.Name.Length-1)))
        If(-Not(Test-Path $n)){[Void](MD $n)}
        $g.Group|Move -Dest $n
    }
}

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

相关问题 从文件名的后缀创建子文件夹,然后移动文件 - Create subfolder from suffix of filename then move file 将特定子文件夹中的文件移动到另一个子文件夹 - Move files in specific subfolders to another subfolder 创建批处理文件以删除,重命名和移动多个文件的多个子文件夹 - Create batch file to delete,rename and move multiple files multiple subfolder 批处理文件以在文件夹中创建新的子文件夹,将文件移动到目录中所有文件夹的新创建的子文件夹 - batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory Windows 7 Batch - 创建子文件夹,然后查找文件名中包含特定文本的文件并将这些文件移动到新创建的子文件夹中 - Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder 根据名称批量移动文件到新的子文件夹 - Batch move files to new subfolders based on name BAT脚本按文件名将文件移动到子文件夹中 - BAT script to move files into subfolders by file name 在子文件夹中自动创建txt文件 - Auto create txt file in subfolders 在每个子文件夹中创建txt文件 - Create txt file in every subfolder 根据Win7中的列表自动将文件移动到各个子文件夹 - Automated move of files to various subfolders based on list in Win7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM