windows 根据文件名的一部分批量创建文件夹并将文件移动到该文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19992530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch create folders based on part of file name and move files into that folder
提问by JakOwnin
I have 1.6 million(!) PDF files in a single folder. The files are all named similar to this:
我在一个文件夹中有 160 万(!)个 PDF 文件。这些文件的名称都类似于:
LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
LAST_FIRST_7-24-1936 Glasses RX 6-1-11_3.pdf
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.
我需要根据文件的第一部分创建一个文件夹,然后将该文件和具有相同文件名第一部分的所有其他文件移动到该文件夹中。在这种情况下,文件夹将被命名为“LAST_FIRST_7-24-1936”。该文件夹将始终与文件的第一部分相同,直到空格为止。
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:\Data\Medinfo PDFs
for %%F in (*.pdf) do (
2>nul md "%%~nF"
>nul move /y "%%~nF*.*" "%%~nF"
)
popd
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.
但是,它不允许我仅从文件名的一部分创建文件夹名称。如果我能弄清楚那部分,我认为它会起作用。我知道我需要为文件夹名称创建变量,但我不知道如何编辑文件名变量以删除空格后的所有内容。任何帮助,将不胜感激。我不反对在 PowerShell 或其他东西中执行此操作,只要它在 Windows Server 2008 R2 中本地运行即可。
回答by Magoo
@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.
这应该完成所需的任务 - 或至少显示所需的说明。
If you are satisfied with the commands issued, set sourcedir
to your required root directory and remove the two echo
keywords to activate.
如果您对发出的命令感到满意,请设置sourcedir
为您需要的根目录并删除两个echo
关键字以激活。
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.
MD
尝试重新创建现有目录时生成的“目录已存在”消息可以通过附加2>nul
到该MD
行来抑制。
Similarly, the report that one file has been moved may be suppresed by appending >nul
to the MOVE
line.
类似地,一个文件已被移动的报告可以通过附加>nul
到该MOVE
行来抑制。
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.
2>nul
抑制错误消息(尝试创建现有目录是错误的),而“文件已移动”消息是普通的输出消息,因此有所不同。
Addendum - how it works.
附录 - 它是如何工作的。
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, SPACE,;TABare used.
首先,PUSHD
将当前目录设置为目标。
的DIR
命令输出由标记化FOR/F
。该tokens=1*
子句指示将第一个标记 (1) 分配给指定的元变量 ( %%a
) 并隐式地将第二个标记 (*) 分配给%%b
- 仅按字母顺序排列下一个。令牌的*
意思everything after those token numbers explicitly mentioned
。没有使用delims
子句,因此使用默认分隔符(SEPARATORS 集)SPACE,;TAB。
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.
该DIR
是针对上的面具*_*_*-* *.*
,所以只有文件匹配的面具-在*手段any number of any characters
-将设。因为掩码是"quoted"
空格包含在掩码中。如果没有引号,将指定两个单独的掩码。该/b
选项以基本形式生成一个列表,即只有名称,没有标题或摘要。该/a-d
选项禁止任何可能符合掩码的目录名称。
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 SPACEas a delimiter.
因此,for LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
的dir
列表LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
和FOR/F
标记为LAST_FIRST_7-24-1936
to%%a
和Diagnostic - Topography 11-18-10_1.pdf
to%%b
使用SPACE作为分隔符。
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."
然后可以通过在%%a
和之间重新插入空格来重建文件名%%b
。任何包含分隔符的文件名都需要被引用以对字符进行分组并表明它们不是分隔元素。移动的目标\以指定“这是一个目录名称”而终止。
The POPD restores the original logged directory.
POPD 恢复原始记录目录。
回答by PhotoMike
This really helped me out, and I made a slight modification to process my digital photographs (I'm a photographer) and called the batch file FolderAndMove. Here is my code:
这真的帮助了我,我对处理我的数码照片(我是一名摄影师)进行了轻微的修改,并将批处理文件命名为 FolderAndMove。这是我的代码:
@ECHO OFF
SETLOCAL
SET "sourcedir=***PASTE_DIRECTORY_HERE***"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=_" %%a IN (
'dir /b /a-d "*_*_*_*.*"'
) DO (
MD %%a_%%b 2>nul
MOVE "%%a_%%b_%%c_%%d" .\%%a_%%b\ >nul
)
POPD
GOTO :EOF
Here is the simple description I made for other photographers:
以下是我为其他摄影师做的简单描述:
FolderAndMove will take a folder of files of format *_*_*_*.*
(like LastName_FirstInitial_Event#_Sequence#.JPG
), create a single folder within selected root folder for each unique *_*
file prefix (in example case, LastName_FirstInitial), and move all files following *_*_*_*.*
convention into the *_*
folder corresponding to their prefix.
FolderAndMove 将采用格式*_*_*_*.*
(如LastName_FirstInitial_Event#_Sequence#.JPG
)的文件文件夹,在选定的根文件夹中为每个唯一的*_*
文件前缀(例如,LastName_FirstInitial)创建一个文件夹,并将所有文件按照*_*_*_*.*
约定移动*_*
到与其前缀对应的文件夹中。
Note that file format is exact: variables (*) must be delimited (~segmented) by underscores with no spaces in order for FolderAndMove to work.
请注意,文件格式是精确的:变量 (*) 必须由下划线分隔(~分段),且不带空格,以便 FolderAndMove 工作。