根据部分名称移动文件 - DOS/批处理

发布于 2024-12-23 00:32:11 字数 773 浏览 1 评论 0原文

我有这个代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-12-30 00:32:11

阅读HELP SET,然后尝试以下代码作为构建解决方案的起点...

您将需要处理可能出现的重复名称。

@echo off 
setlocal enabledelayedexpansion
for %%a in (*) do (
  set fldr=%%~na
  set fldr=!fldr:~0,4!
  echo md "!fldr!"
  echo move "%%a" "!fldr!"
)

尝试一下,广泛测试并删除 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.

@echo off 
setlocal enabledelayedexpansion
for %%a in (*) do (
  set fldr=%%~na
  set fldr=!fldr:~0,4!
  echo md "!fldr!"
  echo move "%%a" "!fldr!"
)

try it, test extensively and remove the ECHO commands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文