将文件移动到同名的文件夹 [英] Move Files to Folders with same name
问题描述
我有1000个文件,后缀为 -PRO1
和 -PPR2
具有相同的名称但没有后缀...
I have 1000 files with the suffix -PRO1
and -PPR2
(1000 each) so I have 1000 folders with the same names but without the suffix...
例如,我有一个名为 Abstract_Colorful
文件 Abstract_Colorful-PRO1
和 Abstract_Colorful-PPR2
等等...
For example I have a folder called Abstract_Colorful
and I have the files Abstract_Colorful-PRO1
and Abstract_Colorful-PPR2
and so on...
我想要批量移动所有文件,我有这个代码(来自另一篇文章)
I want to make a batch to be able to move all files automatically, I have this code (from another post)
@echo off
setlocal enabledelayedexpansion
pushd "C:\Folders\"
for %%a in (*) do (
set fldr=%%~na
set fldr=!fldr:~0,4!
md "!fldr!"
move "%%a" "!fldr!"
)
popd
pause
exit
但它的作用是,如果文件有超过4个字符,它创建一个文件夹与前4个字符...我想做的是批处理识别文件名,停在 -
并移动到
but what it does is that if the file has more than 4 characters it creates a folder with the first 4 chars... What I want to do is that the batch recognizes the filename and stops at the -
and moves to the folder...
感谢您的时间:)
推荐答案
@echo off
pushd "C:\Folders"
rem Process all files in this folder separating the names at "-"
for /F "tokens=1* delims=-" %%a in ('dir /B *.*') do (
rem At this point %%a have the name before the "-" and %%b the rest after "-"
rem Create the folder, if not exists
if not exist "%%a" md "%%a"
rem Move the file there
move "%%a-%%b" "%%a"
)
popd
这篇关于将文件移动到同名的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!