[英]batch file to create directory based on file name, move like files to new directory
I have hundreds of files in a directory that all have different names but some of those are related.我在一个目录中有数百个文件,它们都有不同的名称,但其中一些是相关的。
The files are mostly.AI and PDF and are all names like this:文件大多是.AI和PDF,都是这样的名字:
What I want to do is have a batch file create directories for FILE1, FILE2, FILE3, etc. and move all files that start with FILE1, FILE2, FILE3, etc. into those newly created directories.我想要做的是有一个批处理文件为 FILE1、FILE2、FILE3 等创建目录,并将所有以 FILE1、FILE2、FILE3 等开头的文件移动到这些新创建的目录中。
I have used this, but it just makes a DIR for the whole filename.我已经使用了它,但它只是为整个文件名创建了一个 DIR。
@echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
This doesn't do what I need, can anyone help massage this into what I need or give me a new batch file to try?这不能满足我的需要,任何人都可以帮我把它按摩成我需要的东西或给我一个新的批处理文件来尝试吗?
Thanks!谢谢! This will save me countless hours of manual work!这将节省我无数小时的体力劳动!
You need to split the filename by _
with another for /f
loop:您需要使用另一个for /f
循环将文件名拆分为_
:
@echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
for /f "delims=_" %%a in ("%%~ni") do (
md "%%a" 2>nul
move "%%~i" "%%~a\"
)
)
2>nul
suppresses the error message in case the folder already exists. 2>nul
在文件夹已经存在的情况下抑制错误消息。
Please consider using for %%i in (*_*) do (
or for %%i in (*_*_*.ai *_*_*.pdf) do (
- according to your actual needs/file names to pre-select files to be moved.请考虑使用for %%i in (*_*) do (
或for %%i in (*_*_*.ai *_*_*.pdf) do (
- 根据您的实际需要/文件名来预先-选择要移动的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.