Search code examples
filebatch-filedirectorymove

bat file to move files in sub-folder to current folder


I am looking for a .bat file that will move all FILES inside subfolders to the parent folder.

Example:
C:/games/nintendo/N/gamename/gamename.iso

I want to run a file that will move that gamename.iso to the folder N to be:

C:/games/nintendo/N//gamename.iso

Thanks so much for the help :_)


Solution

  • set root_folder=C:\games
    
    for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".iso" ') do (
        move /y "%%G" "%%~dpG..\%%~nxG"
    )
    

    Update

    Here is the explanation:
    set root_folder=C:\games - We're setting a variable that we're going to use later; This is the root folder that we want to start scanning

    for /f - you can get documentation on usage, by typing for /? in the command prompt OR this LINK

    dir %root_folder% /b /o:-n /s - we're listing all the file/folders and sub-folders by using /b for bare format; /o:-n for descending sort order; /s for recursively walking down the folder structure

    ^| findstr /i ".iso" - we're piping | all that information through and then looking for things that contain .iso in the name. Please note that and escape character ^ is required

    do ( - for every instance that give output in this operation do the following:

    move /y - move and overwrite /y if necessary:

    %%G - string that is the output from the criteria uptop.

    %%~dpG - d = drive letter, p = path of the output string

    .. - this is the folder above the current folder

    \%%~nxG - n = filename, x = extension for the file that matches the output.

    Here is what it would look like expanded (therefore copying the file to C:\games\nintendo\N\:

    move /y "C:\games\nintendo\N\gamename\gamename.iso" "C:\games\nintendo\N\gamename\..\\gamename.iso"