【问题标题】:Move files to folders with partial names将文件移动到具有部分名称的文件夹
【发布时间】:2010-07-05 20:20:55
【问题描述】:

我有大约 250 个文件需要移动到特定文件夹。问题是文件夹只有文件的部分名称。

例如,我需要将文件“12345.txt”移动到文件夹“12345 - hello”,因为每个文件夹都以实际文件名开头。

我可以在 DOS 的批处理文件中执行此操作吗?

谢谢。

【问题讨论】:

  • 真的 DOS?你从哪里得到长文件名的?
  • 文件夹的名称是否具有常规结构,例如您的示例中的“文件名 - 某物”?是否可能出现以下情况:文件名“a.txt”,第一个文件夹“a000”,第二个文件夹“a111”,即匹配是否有歧义?

标签: batch-file dos


【解决方案1】:

假设是 Windows,其实并不难:

@echo off
rem loop over all files
for %%f in (*) do call :process "%%f"

rem this is necessary to avoid running the subroutine below
rem after the loop above ended
goto :eof

rem subroutine that gets called for every file
rem this finds the first matching folder and moves the file there
:process
rem the /d loops over all directories - the mask ensures that
rem the directory name starts with the given file name (without
rem extension)
for /d %%d in ("%~n1*") do (
    echo Moving "%~1" to "%%d" ...
    move "%~1" "%%d"
    rem Return since the file was moved already
    goto :EOF
)

也可以在my SVN repository找到。

【讨论】:

  • 谢谢约翰内斯。有效。谢谢你,真的很感激。
猜你喜欢
  • 2017-06-07
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
相关资源
最近更新 更多