问题描述
我在一个文件夹中有 160 万(!)个 PDF 文件.这些文件的名称都类似于:
I have 1.6 million(!) PDF files in a single folder. The files are all named similar to this:
LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
LAST_FIRST_7-24-1936 Glasses RX 6-1-11_3.pdf
我需要根据文件的第一部分创建一个文件夹,然后将该文件和具有相同文件名第一部分的所有其他文件移动到该文件夹中.在这种情况下,文件夹将被命名为LAST_FIRST_7-24-1936".文件夹的名称将始终与文件的第一部分相同,直到空格为止.
I need to create a folder based on the first part of the file and then move that file and all other files with that same first part of the file name into that folder. In this case the folder would be named "LAST_FIRST_7-24-1936". The folder will always be named the same as the first part of the file up until the space.
我想创建批处理文件来执行此操作.凭借我的基本编程知识,我想出了这个逻辑过程:
I would like to create batch file that will do this. With my rudimentary programming knowledge I came up with this logical process for doing this:
1 Take the first file and name it var1
2 Remove everything after the space in var1 and name it var2
3 Create a folder named var2
4 Move the file var1 into the folder var2
5 If there are more files Go to line 1, otherwise end
我不知道正确的语法是什么.
I don't know what the proper syntax would be for this.
我确实找到了这个链接 需要一个脚本来根据文件名创建文件夹,并自动移动文件我根据那个链接制作了这批
I did find this link Need a script to create folders based on file names, and auto move filesI made this batch based on that link
pushd D:DataMedinfo PDFs
for %%F in (*.pdf) do (
2>nul md "%%~nF"
>nul move /y "%%~nF*.*" "%%~nF"
)
popd
但是,它不允许我仅从文件名的一部分创建文件夹名.如果我能弄清楚那部分,我认为它会起作用.我知道我需要为文件夹名称创建变量,但我不知道如何编辑文件名变量以删除空格后的所有内容.任何帮助,将不胜感激.我不反对在 PowerShell 或其他东西中执行此操作,只要它在 Windows Server 2008 R2 中本地运行即可.
But, it doesn't allow me to create the folder name from just part of the file name. If I can figure that part out I think it would work.I know I need to create variable for the folder name but I don't know how to edit the file name variable to remove everything after the space.Any help would be appreciated. I'm not opposed to doing this in PowerShell or something else as long as it works natively in Windows Server 2008 R2.
推荐答案
@ECHO OFF
SETLOCAL
SET "sourcedir=c:sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*_*-*-* *.*"'
) DO (
ECHO MD %%a
ECHO MOVE "%%a %%b" .\%%a
)
POPD
GOTO :EOF
这应该完成所需的任务 - 或者至少显示所需的说明.
This should accomplish the required task - or at least show the required instructions.
如果您对发出的命令感到满意,请将 sourcedir
设置为您需要的根目录并删除两个 echo
关键字以激活.
If you are satisfied with the commands issued, set sourcedir
to your required root directory and remove the two echo
keywords to activate.
在尝试重新创建现有目录时由 MD
生成的目录已存在"消息可以通过将 2>nul
附加到 来抑制MD
行.
The "directory already exists" message generated by the MD
on attempting to re-create an existing directory may be suppresed by appending 2>nul
to the MD
line.
同样,可以通过将 >nul
附加到 MOVE
行来抑制报告一个文件已被移动.
Similarly, the report that one file has been moved may be suppresed by appending >nul
to the MOVE
line.
2>nul
抑制错误消息(尝试创建现有目录是错误的),而文件已移动"消息是普通的输出消息,因此有所不同.
2>nul
suppresses error messages (trying to create an existing directory is an error) whereas the 'files moved' message is an ordinary output message, hence the difference.
附录 - 它是如何工作的.
Addendum - how it works.
首先,PUSHD
将当前目录设置为目标.DIR
命令输出由 FOR/F
标记.tokens=1*
子句指示将第一个标记 (1) 分配给指定的元变量 (%%a
),并将第二个标记 (*) 隐式分配给 %%b
- 按字母顺序排列下一个.令牌*
表示那些明确提到的令牌编号之后的所有内容
.没有使用 delims
子句,所以默认分隔符(SEPARATORS 的集合, .
First, the PUSHD
sets the current directory to the target.
The DIR
command output is tokenised by the FOR/F
. The tokens=1*
clause instructs that the first token (1) is assigned to the nominated metavariable (%%a
) and implicitly the second token (*) to %%b
- simply the next alphabetically. Token *
means everything after those token numbers explicitly mentioned
. No delims
clause is used, so the default delimiters (the set of SEPARATORS, are used.
DIR
的目标是 *_*_*-* *.*
掩码,因此只有匹配该掩码的文件 - 其中 表示 任意数量的任意字符
- 将被定位.因为掩码是 "quoted"
,所以掩码中包含空格.如果没有引号,将指定两个单独的掩码./b
选项以基本形式生成一个列表,即只有名称,没有标题或摘要./a-d
选项禁止任何可能符合掩码的目录名称.
The DIR
is targeted on a mask of *_*_*-* *.*
, so only files matching that mask - where means any number of any characters
- will be located. Because the mask is "quoted"
the spaces is included in the mask. Without the quotes, two separate masks would be specified. The /b
option produces a list in basic form, that is, names only, no headers or summary. The /a-d
option suppresses any directory names that may have fitted the mask.
因此,对于 LAST_FIRST_7-24-1936 诊断 - 地形 11-18-10_1.pdf
,dir
列出 LAST_FIRST_7-24-1936 诊断 - 地形11-18-10_1.pdf
和 FOR/F
标记为 LAST_FIRST_7-24-1936
到 %%a
和 诊断 - 地形 11-18-10_1.pdf
到 %%b
使用 作为分隔符.
Hence, for LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
, the dir
lists LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
and FOR/F
tokenises as LAST_FIRST_7-24-1936
to %%a
and Diagnostic - Topography 11-18-10_1.pdf
to %%b
using the as a delimiter.
然后可以通过在 %%a
和 %%b
之间重新插入空格来重建文件名.任何包含分隔符的文件名都需要引用以将字符分组并表示它们不是分隔元素.移动的目标以 终止,以指定这是一个目录名称."
The filename can then be reconstructed by re-inserting the space between %%a
and %%b
. Any filename containing a separator needs to be quoted to group the characters and signal that they are not separated elements. The target of the move is terminated with to specify "This is a directory name."
POPD 恢复原始记录目录.
The POPD restores the original logged directory.
这篇关于根据文件名的一部分批量创建文件夹并将文件移动到该文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!