根据部分名称移动文件 - DOS/批处理
我有这个代码
pushd "C:\Folders\"
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"
)
popd
pause
exit
将同名的文件移动到同名的文件夹中,如果该文件夹不存在,它将创建一个新文件夹。好的,太好了。
我遇到的这个问题是我想要与上面相同的功能,但我只是为了查看前 4 个字符。例如,
1234 - sample.jpg
1234 - sample-sm.jpg
1234 - sample-new.jpg
1234 - sample-right.jpg
1235 - sample.jpg
1234 个文件将被移动到同一文件夹,因为前 4 个字符相同,但 1235 个文件将被移动到新文件夹,因为字符的第一个字符不同。
谢谢
@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 个字母的所有内容移动到正确的文件夹。
所以不,我不需要它来创建一个目录,而是将其移动到已经创建的目录。
I have this code
pushd "C:\Folders\"
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"
)
popd
pause
exit
This move files with the same name into a folder with the same name, if the folder does not exist it will create a new folder. Ok great.
This issue I have is that I want the same function as above however I only was it to look at the first 4 characters. E.g.
1234 - sample.jpg
1234 - sample-sm.jpg
1234 - sample-new.jpg
1234 - sample-right.jpg
1235 - sample.jpg
1234 files will be moved to the same folder as the first 4 characters are the same, however 1235 will be moved to a new folder because the fist for characters are different.
Thank you
@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
However it creates new folders with the first 4 letters and moves the files to these created folders... It does move everything with the same first 4 letters to the correct folder.
So no I need it NO to make a directory but to move it to the already create directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读
HELP SET
,然后尝试以下代码作为构建解决方案的起点...您将需要处理可能出现的重复名称。
尝试一下,广泛测试并删除
ECHO
命令。Read
HELP SET
and then try the following code as an starting point to build your solution...you will need to cope with duplicated names that may occur.
try it, test extensively and remove the
ECHO
commands.