2010-07-05 135 views
1

我有大約250個文件需要移動到特定文件夾。問題是該文件夾只有文件的部分名稱。將文件移動到部分名稱的文件夾

例如,我需要將文件「12345.txt」移動到文件夾「12345 - hello」,因爲每個文件夾都以實際文件名開頭。

我可以在DOS的批處理文件中執行此操作嗎?

謝謝。

+2

*真的* DOS?你在哪裏得到長文件名? – Joey 2010-07-05 20:29:39

+1

文件夾的名稱是否有規則的結構,即像你的例子中的「filename - something」?是否可能出現以下情況:文件名「a.txt」,第一個文件夾「a000」,第二個文件夾「a111」,即匹配是否模糊? – chalup 2010-07-05 20:43:09

回答

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找到。

+0

謝謝Johannes。有效。謝謝你,非常感激。 – user383963 2010-07-05 20:47:40

相關問題