问题
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