简体   繁体   English

如何通过批处理文件基于可变长度文件名创建文件夹

[英]How to create folders based on variable length filenames through batch file

I have a set of files I wish to copy to a new set of subfolders. 我有一组文件希望复制到一组新的子文件夹中。

For instance: 例如:
0_107_206.tif 0_107_206.tif
1_0_69.tif 1_0_69.tif
1_16_75.tif 1_16_75.tif
1_40_117.tif 1_40_117.tif
2_0_36.tif 2_0_36.tif
2_26_62.tif 2_26_62.tif
35_0_19.tif 35_0_19.tif

These files are stored in folders based on the first substring of the filename such as 0, 1 or 35. I want to create subfolders based on the second substring between the 2 '_' characters. 这些文件基于文件名的第一个子字符串(例如0、1或35)存储在文件夹中。我想基于2个'_'字符之间的第二个子字符串创建子文件夹。 I have tried several things along the lines of 我已经尝试了几种方法

SETLOCAL ENABLEDELAYEDEXPANSION SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%B in (*.tif) DO ( FOR %% B in(* .tif)DO(
SET FileName=%%B SET FileName = %% B
SET FileName1=!FileName:~2! SET FileName1 =!FileName:〜2!
SET FileName2=!FileName1:~0,-7! SET FileName2 =!FileName1:〜0,-7!
MD %TargetPath%!FileName2! MD%TargetPath%!FileName2!
)
ENDLOCAL ENDLOCAL

But this is not flexible enough. 但这还不够灵活。 Is there a way to get the position of the '_' characters and feed that into a SUBSTRING function? 有没有一种方法来获取'_'字符的位置并将其输入到SUBSTRING函数中? Naturally this needs to work in a loop since there are thousands of files I need to proces. 自然,这需要循环工作,因为我需要处理成千上万个文件。
Thanks in advance. 提前致谢。

FOR %%B in (*.tif) DO (
    for /f "tokens=1,2 delims=_" %%x in ("%%~B") do (
        md "%TargetPath%%%~y"
        copy %%~B "%TargetPath%%%~y\%%~B"
    )

)

?

Given a file structure 给定文件结构

u:\0\0_107_206.tif
u:\1\1_0_69.tif
u:\1\1_16_75.tif
u:\1\1_40_117.tif
u:\2\2_0_36.tif
u:\2\2_26_62.tif
u:\35\35_0_19.tif

Then this batch: 然后这批:

@ECHO OFF
SETLOCAL
SET "sourcedir=u:"
SET "destdir=c:\destdir"
 FOR /f "tokens=1*delims=" %%a IN (
  'dir /s /b /a-d "%sourcedir%\*_*_*.tif" '
  ) DO FOR /f "tokens=1,2,3delims=_" %%B IN ("%%~na") DO (
   ECHO MD "%destdir%\%%C"
   ECHO COPY /b "%%a" "%destdir%\%%C\"
 )
GOTO :EOF

would generate 会产生

MD "c:\destdir\107"
COPY /b "u:\0\0_107_206.tif" "c:\destdir\107\"
MD "c:\destdir\0"
COPY /b "u:\1\1_0_69.tif" "c:\destdir\0\"
MD "c:\destdir\16"
COPY /b "u:\1\1_16_75.tif" "c:\destdir\16\"
MD "c:\destdir\40"
COPY /b "u:\1\1_40_117.tif" "c:\destdir\40\"
MD "c:\destdir\0"
COPY /b "u:\2\2_0_36.tif" "c:\destdir\0\"
MD "c:\destdir\26"
COPY /b "u:\2\2_26_62.tif" "c:\destdir\26\"
MD "c:\destdir\0"
COPY /b "u:\35\35_0_19.tif" "c:\destdir\0\"

So you'd just need to change source and destination directory names to suit; 因此,您只需要更改源和目​​标目录名称即可。 change the copy command to move if appropriate; 如果合适,将copy命令更改为move remove the ECHO keywords to activate. 删除要激活的ECHO关键字。

You could also append 2>nul to the MD line to suppress the 'diretory already exists' message. 您还可以在MD行中附加2>nul以禁止显示“目录已经存在”消息。

OR, you could replace the COPY with XCOPY and then the MD would become irrelevant. 或者,您可以将COPY替换为XCOPY ,然后MD将变得无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM