views:

39

answers:

1

hi,

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.

+2  A: 

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.

Joey
thank you Johannes. it worked. thank you, trully appreciated.