Move files to folders with partial names

霸气de小男生 提交于 2019-12-24 10:04:45

问题


I have about 250 files that I need to move to a specific folder. The problem is that folder only have the partial name of the files.

For example, I need to move file "12345.txt" to folder "12345 - hello" as each folder starts by the actual file name.

Can I do this in a batch file in DOS?

Thank you.


回答1:


Assuming Windows, it's actually not hard:

@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
)

Can also be found in my SVN repository.



来源:https://stackoverflow.com/questions/3181953/move-files-to-folders-with-partial-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!