.BAT文件按文件名将文件移动到文件夹和子文件夹中...无法使子文件夹正常工作 [英] .BAT file to move files into folders and subfolders by filename... can't get subfolders to work

查看:142
本文介绍了.BAT文件按文件名将文件移动到文件夹和子文件夹中...无法使子文件夹正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想不通.

我有一堆名称格式为"PREFIX_PREFIX2_filename.pdf"的文件

I have a bunch of files with name format: "PREFIX_PREFIX2_filename.pdf"

我想将它们分类到这样的文件夹中:"PREFIX/PREFIX2/(文件)"

I want to sort them into folders like this: "PREFIX/PREFIX2/(files)"

这是我到目前为止所拥有的:

This is what I have so far:

setlocal enableextensions disabledelayedexpansion

for %%f in (*_*_*.pdf, *_*_*.png) do (
    for /f "tokens=1, 2 delims=_" %%p in ("%%~nf") do (
        for %%d in ("%%~dpf.") do if /i not "%%~p"=="%%~nd" (
            if not exist "%%~dpf\%%~p"  md "%%~dpf\%%~p"
            if not exist "%%~dpf\%%~p"  md "%%~dpf\%%~p"
            move "%%~ff" "%%~dpf\%%~p"
        )
    )
)

这将根据第一个前缀对它们进行排序,但是我无法再进一步了,说实话我什至不知道这里到底发生了什么.这种CMD语言对我来说是我见过的最丑陋和最难以理解的东西.

This sorts them based on the first prefix, but I just can't get any further, and honestly I am not sure even sure what's going on here anyway. This CMD language has got to me the most ugly and inscrutable thing I've ever seen.

我已经阅读了许多类似的问题,但仍然无法解决.

I have read over a bunch of similar questions but still couldn't work it out.

如果有人能建议我如何完成这个相对简单的任务,并向我解释这到底是怎么回事,我将不胜感激.

I would be very grateful if someone could advise me how to finish up this surely relatively simple task and also explain to me what the hell is going on here.

推荐答案

此方法更简单:

setlocal

for /F "tokens=1,2* delims=_" %%a in ('dir /B *_*_*.pdf *_*_*.png') do (
   if not exist "%%a" md "%%a"
   if not exist "%%a\%%b" md "%%a\%%b"
   move "%%a_%%b_%%c" "%%a\%%b\%%c"
)

即:dir /B *_*_*.pdf命令产生具有PREFIX_PREFIX2_filename.pdf格式的名称列表. for /F "tokens=1,2* delims=_" %%a通过以下方式将此类名称分为3个标记:%%a=PREFIX%%b=PREFIX2%%c=filename.pdf.其余的很明显...

That is: dir /B *_*_*.pdf command produce a list of names with PREFIX_PREFIX2_filename.pdf format. for /F "tokens=1,2* delims=_" %%a divide such names in 3 tokens this way: %%a=PREFIX, %%b=PREFIX2 and %%c=filename.pdf. The rest is obvious...

PS-我建议您将标题中的sort单词更改为move.第一项提出了完全不同的任务的想法...

PS - I suggest you to change the sort word in your title to move. The first term gives the idea of an entirely different task...

这篇关于.BAT文件按文件名将文件移动到文件夹和子文件夹中...无法使子文件夹正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆