[英]Batch create folder based on partial file name and move files to folder
I have a series of .wav files with similar file names.我有一系列具有相似文件名的 .wav 文件。 Each file has a first and last name followed by the date and a number.每个文件都有一个名字和姓氏,后跟日期和一个数字。 I would like to create a series of files based on the first and last name and then move the files to the matching file path.我想根据名字和姓氏创建一系列文件,然后将文件移动到匹配的文件路径。
Example:例子:
John_Doe_1-6-6-2021 15-23_123453245.wav
John_Doe_1-6-6-2021 15-23_12345874.wav
John_Doe_1-6-6-2021 15-23_1239964.wav
Mary_Walker_1-6-6-2021 15-23_123453245.wav
Mary_Walker_1-6-6-2021 15-23_1778941.wav
Mary_Walker_1-6-6-2021 15-23_8741556899.wav
I would like those files to be organized similar to the following:我希望这些文件的组织方式类似于以下内容:
SourceDirectory
|> John_Doe_1
|> John_Doe_1-6-6-2021 15-23_123453245.wav
|> John_Doe_1-6-6-2021 15-23_12345874.wav
|> John_Doe_1-6-6-2021 15-23_1239964.wav
|> Mary_Walker_1
|> Mary_Walker_1-6-6-2021 15-23_123453245.wav
|> Mary_Walker_1-6-6-2021 15-23_1778941.wav
|> Mary_Walker_1-6-6-2021 15-23_8741556899.wav
At the moment I have this script.目前我有这个脚本。 The script will create a folder based on the First Name and moves the files there (Which is great), the only issue is I need the first name, last name and the 1 at the end.该脚本将根据名字创建一个文件夹并将文件移动到那里(这很棒),唯一的问题是我需要名字、姓氏和最后的 1。 So the Folder should be like John Doe 1
not just John
or John Doe
.所以文件夹应该像John Doe 1
而不仅仅是John
或John Doe
。
@echo off
SETLOCAL
SET "sourcedir=C:\Users\JohnUser\Desktop\Test\"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "* *_*-* *.*"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
pause
Here's a quick example using the -
character as the delimiter, Delims
, to perform the fiilename string split.这是一个使用-
字符作为分隔符Delims
来执行文件名字符串拆分的快速示例。 (You could use Tokens=1
, but as that is the default, I have left it out) : (您可以使用Tokens=1
,但由于这是默认设置,我已将其省略) :
@Echo Off
SetLocal EnableExtensions
Set "SourceDir=%UserProfile%\Desktop\Test"
For /F "Delims=" %%G In (
'%SystemRoot%\System32\where.exe "%SourceDir%":"?*-?*-?*-???? ??-??_?*.wav"'
) Do For /F "Delims=-" %%H In ("%%~nG") Do (
%SystemRoot%\System32\Robocopy.exe "%SourceDir%" "%SourceDir%\%%H" "%%~nxG" /Mov 1>NUL
)
To understand how any command works, open a Command Prompt window, type Command /?
要了解任何命令的工作原理,请打开命令提示符窗口,键入Command /?
. . For example for /?
例如for /?
, where /?
, where /?
, robocopy /?
, robocopy /?
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.