一批基于文件​​名的一部分创建文件夹和文件移动到该文件夹 [英] Batch create folders based on part of file name and move files into that folder

查看:259
本文介绍了一批基于文件​​名的一部分创建文件夹和文件移动到该文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹中160万(!)的PDF文件。这些文件都被命名为类似于这样:

  LAST_FIRST_7-24-1936诊断 - 地形11-18-10_1.pdf
LAST_FIRST_7-24-1936眼镜RX 6-1-11_3.pdf

我需要创建基于该文件的第一部分的一个文件夹,并然后移动该文件和与文件名相同的第一部分到该文件夹​​的所有其他文件。在这种情况下,该文件夹将被命名为LAST_FIRST_7-24-1936。该文件夹将总是被命名为相同的文件,直到空间的第一部分

我想创建批处理文件,将做到这一点。随着我的基本的编程知识,我想出了这个逻辑过程这样做的:

  1在第一个文件,并将其命名为VAR1
2 VAR1空间后删除一切,并将它命名VAR2
3创建一个文件夹命名为VAR2
4将文件VAR1到该文件夹​​VAR2
5如果有更多文件转到1号线,否则结束

我不知道正确的语法是什么这一点。

我发现这个链接<一个href=\"http://stackoverflow.com/questions/11441317/need-a-script-to-create-folders-based-on-file-names-and-auto-move-files\">Need一个脚本来基于文件名创建文件夹和自动移动文件
我做了基于该链接的此批

  pushd将D:\\ DATA \\ PDF文件用药指导
为%% f由于(* .PDF)做(
  2 - ; NUL MD%%〜nF的
  &GT; NUL移动/ Y%%〜nF的*。*,%%〜nF的

POPD

但是,这并不让我从刚才的文件名的一部分创建的文件夹名称。如果我可以计算的那部分,我认为这是可行的。
我知道我需要为文件夹名称创建变量,但我不知道如何编辑的文件名可变空间后删除所有内容。
任何帮助将是AP preciated。我不反对,只要其他人,因为它本身的工作在Windows Server 2008 R2在PowerShell中什么这样做。


解决方案

  @ECHO OFF
SETLOCAL
SETsourcedir = C:\\ sourcedir
PUSHD%sourcedir%
FOR / F令牌= 1 *%%一个IN(
 '目录/ B / A-D* _ * _ * - * - * * *。'
 )DO(
 ECHO MD %%一
 ECHO MOVE%%一个%% B。\\ %%一个\\

POPD
GOTO:EOF

这应该完成所需的任务 - 或者至少显示所需的说明

如果您满意发行,集 sourcedir 为您所需的根目录下,删除命令两个回声关键字激活。

在通过追加 MD 上尝试重新创建现有目录可能psed燮$ P $生成的消息> 2的方式&gt; NUL 到 MD

同样,一个文件已被移动可能通过附加psed SUP $ P $报告&GT; NUL 移动行。

2 - ; NUL 燮presses错误消息(尝试创建一个现有目录是错误的),而文件移到消息是一个普通的输出信息,因此差


附录 - 它是如何工作

首先, PUSHD 设置当前目录为目标。结果
DIR 命令的输出是由 FOR / F 标记化。在标记= 1 * 子句指示第一令牌(1)被分配给指定的metavariable( %% A )和隐含第二令牌(*),以 %% b - 只要下一个字母。令牌 * 办法一切都明确提及的令牌数量后。否 delims 子句中使用,所以默认分隔符(设置隔板,<大骨节病> SPACE <大骨节病>; < /骨节病> <大骨节病>标签被使用。

DIR 是针对上面具 * _ * _ * - * * * ,所以只有文件匹配的面具 - 其中<大骨节病> * 办法任意数量的任何字符 - 将设。因为掩模是援引的空间被包括在掩模。不带引号,两个独立的面具将被指定。在 / B 选项生成的基本形式列表,也就是说,只有名字,没有标题或摘要。在 / A-D 选项燮presses可能已经装面膜的目录名。

因此​​,对于 LAST_FIRST_7-24-1936诊断 - 地形11-18-10_1.pdf DIR 名单 LAST_FIRST_7-24-1936诊断 - 地形11-18-10_1.pdf FOR / F tokenises为 LAST_FIRST_7-24-1936 %% A 诊断 - 地形11-18-10_1.pdf %% b 使用<大骨节病> SPACE 作为分隔符。

文件名可以被重新插入空间之间的 %% A 重建%% b 。需要被引用到组,他们是不分离元件的字符和信号含有一个分离器的任何文件名。此举的目标是终止<大骨节病> \\ 指定这是一个目录名。

该POPD恢复原来的登录目录。

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

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 files I 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.

解决方案

@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.

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.

Similarly, the report that one file has been moved may be suppresed by appending >nul to the MOVE line.

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.

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 , ; TAB are used.

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.

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 SPACE as a delimiter.

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."

The POPD restores the original logged directory.

这篇关于一批基于文件​​名的一部分创建文件夹和文件移动到该文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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