move two different file types with same name into a new folder with same name

时间:2016-10-20 19:04:25

标签: batch-file cmd

assume that we have a number of video files (.mp4) along with their subtitle files (.srt) in a folder, i want to loop through all files, if two files of type (mp4 & srt) have the same name, i want to move them into a new folder having the same name of either of them. if the folder exist, then just move if not then create a new one.

i used two approaches to get the list of files

@echo off

for %%a in (*.mp4 *.srt) do (
     comparison here )
pause

and

@echo off
for /f "delims=" %%f in ('dir /b *mp4* *srt*') do (
comparison procedure )
pause

i tried to store the each file name of extension (mp4) in a variable to check it against all the other files that have the extension (srt) but that did not work, i can work with text tokens with no problems

any ideas ?

1 个答案:

答案 0 :(得分:3)

This should get what you need. Using the FOR variable modifiers is the key to getting done what you need.

@echo off

for %%G in (*.srt) do (
    IF EXIST "%%~nG.mp4" (
        MD "%%~nG" 2>nul
        IF NOT EXIST "%%~nG\%%~nG.mp4" move "%%~nG.mp4" "%%~nG"
        IF NOT EXIST "%%~nG\%%~G" move "%%~G" "%%~nG"
    )
)