【问题标题】:Sorting files into folders, according to a part of filename using Windows Batch file使用 Windows 批处理文件根据文件名的一部分将文件分类到文件夹中
【发布时间】:2019-11-27 03:27:14
【问题描述】:

我在 G:\train 的数据集文件夹中有大约 12,000 个 .jpg 文件,其名称如

0002_c1s1_000451_03.jpg、0002_c1s1_000551_01.jpg...

最多

...1500_c6s3_086542_02.jpg , 1500_c6s3_086567_01.jpg

我想将它们移动到具有初始文件名的新文件夹,例如 0002, 0005, 0007,... 1496, 1500

我需要的是一个 Windows 批处理文件,可以轻松创建新文件夹和快速移动文件。我尝试了其他几个答案都无济于事。

【问题讨论】:

  • 到目前为止你尝试过什么?你可能真的很亲密!

标签: windows sorting batch-file cmd


【解决方案1】:

这样的事情应该做,作为 .cmd/.bat 脚本文件:

@rem The DEBUG_RUN variable enables a step-by-step mode, it will cause the 
@rem script to pause after processing every file. Remove the DEBUG_RUN (and 
@rem the pause instruction) when you are confident it does what you want,
@rem restart the script and enjoy :-)

:main
@setlocal
    @set DEBUG_RUN=1
    @pushd "G:\train"
        @for %%f in (*.jpg) do @(
            call :mvToSubDir "%%~nxf"
            if defined DEBUG_RUN pause
        )
    @popd
    @pause
@endlocal
@goto:eof

:mvToSubDir
    @set fn=%~1
    @set dn=%fn:~0,4%
    @if not exist "%dn%" mkdir "%dn%"
    move "%fn%" "%dn%"
@goto:eof

【讨论】:

  • 上面写着The system cannot find the file *.jpg.不应该有令牌什么的。我的知识真的很有限。你能帮忙吗?
  • 对不起,我的错误,我认为“for”命令后面没有“/f”。这是最基本的“for”调用。 FOR %%parameter IN (set) DO 命令
  • 请注意,这是可行的,因为您的所有目录都以 4 个字符长为前缀(参见 dirname=%filename:~0,4%),如果它们是可变长度的,我们必须是更聪明。
【解决方案2】:

您可以尝试一系列 for 循环来完成此操作

for /l %%f in (1,1,12000) 做 ( mkdir %%f 对于 (c:\sourcedir) 中的 %%d 执行 ( 移动 %%d %%f ) )

我不完全了解您想如何对它们进行排序,但我希望这会有所帮助。您可以使用“for /?”如果这还不够,请随时寻求更多帮助。祝您工作顺利。

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多