使用批处理文件基于文件名创建文件夹和子文件夹 [英] Create Folder and SubFolder based on file name using batch file

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

问题描述

编码不是我的专长,但是我遇到了一个问题,谷歌搜索已指导我使用批处理文件过程来解决它.本质上,我有成千上万的文件需要移到文件夹中,并且它们具有非常简单的文件结构,如下所示:

Coding is not my speciality but I have come across a problem and google search has guided me to using batch file process in solving it. Essentially I have a couple of thousands of files that need to be moved into folders and they have a very simple file structure, listed below:

UK--London--Filename.pdf
UK--London--Filename2.pdf
UK--Manchester--Filename3.pdf
UK--Liverpool--Filename4.pdf
UK--Chester--Filename5.pdf 

我希望脚本执行以下操作:

I would like the script to:

1. Pick up the first "--" check if the folder exists, if not create it
2. Pick up the second "--" check if the folder exists, if not create it
3. As there might be more than two "--", ignore the rest
4. Move file into the subfolder

为此,输出应类似于(请注意"FILETEST"是我用来测试脚本的文件夹):

To that end, the output should be some like (note "FILETEST" is the folder I am using to test the script):

C:\FILETEST\UK\London\UK--London--Filename.pdf
C:\FILETEST\UK\London\UK--London--Filename2.pdf
C:\FILETEST\UK\Manchester\UK--Manchester--Filename3.pdf
C:\FILETEST\UK\Liverpool\UK--Liverpool--Filename4.pdf
C:\FILETEST\UK\Chester\UK--Chester--Filename5.pdf

我试图重用stackoverflow中另一个问题的脚本(

I have had an attempt to re-using a script from another question in stackoverflow (Batch create folders based on part of file name and move files into that folder). I understand that it would not do exactly what I need, but cant seem to get any output.

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1*delims=--" %%a IN (
 'dir /b /a-d *.*.*'
 ) DO (  
 ECHO MD %%a
 ECHO MOVE "%%a.%%b" --\%%a\
)
POPD
GOTO :EOF

对造成的任何头痛表示歉意,我希望这是一个可以解决的简单方法.

Apologies for any headaches caused, I am hoping this is a simple one to solve.

谢谢

Panos

推荐答案

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
 'dir /b /a-d *--*--*.*'
 ) DO if "%%c" neq "" (  
 ECHO MD "%%a"
 ECHO MD "%%a\%%b"
 ECHO MOVE "%%a--%%b--%%c" ".\%%a\%%b\"
)
POPD
GOTO :EOF

读取当前目录(/a-d =无目录名)中与*--*--*匹配的文件的目录列表.标记,以便%%a获取第一个-序列之前的部分,%%b第二个序列,然后%%c其余部分.

Read the directory list of files in the current directory, (/a-d = no directorynames) that match *--*--*. Tokenise so that %%a acquires the part before the first --sequence, %%b the second and %%c the remainder.

如果%%c不是 empty ,则将目录设置为.\ %% a"和.\ %% a \ %% b"(因为名称中的任何空格都会否则被视为创建两个目录"),然后移动文件,再次出于相同的原因引用该文件.

If %%c is not empty then make the directories ".\%%a" and ".\%%a\%%b" (quoted because any spaces in the name would otherwise be seen as "create two directories") then move the file, again quoted for the same reason.

请注意,在delims=和右引号之间的每个字符都是定界符-不支持定界符字符串.因此,此代码将拾取-以及----的任何其他序列,并尝试对其进行处理.您可以通过在(之前的if "%%c" neq ""之后直接添加if exist "%%a--%%b--%%c"来进一步限制创建/移动.

Note that each character individually between delims= and the close-quote is a delimiter - a delimiter-string is not supported. Consequently, this code will pick up - as well as --- and any other sequence of - and try to process it. You could gate the create/move further by adding if exist "%%a--%%b--%%c" directly after the if "%%c" neq ""before the (.

如果目标名称尚不存在,md将创建一个目录,如果目标名称尚不存在,则会产生一个错误消息.要取消显示此错误消息,请将2>nul附加到md行.

The md will create a directory if the target name does not already exist, and produce an error-message if it already exists. To suppress the error message, append 2>nul to the md lines.

这篇关于使用批处理文件基于文件名创建文件夹和子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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