将文件移动到具有部分名称的文件夹

时间:2010-07-05 20:20:55

标签: batch-file dos

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

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

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

谢谢。

1 个答案:

答案 0 :(得分:3)

假设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中找到。