Search code examples
batch-filedesktop-applicationfile-renamefile-copying

Copy first file for multiple folders to some another location


I have about 1000 folders on my hard drive. Each folder contains about 100 .jpg files. Now I need to copy from each folder "page (1).jpg" file in to some specific folder(each folder contain "page (1).jpg" file). And one more thing... On the end, after copying, each .jpg file from specific folder need to have name like folder from which it was copied.

How to do this on easiest way. Maybe with some batch file or something else...


Solution

  • not tested (you need to change the names of root_dir and target_dir):

    @echo off
    
    set "target_dir=E:\scriptests\redirection"
    set "root_dir=E:\scriptests\labels"
    pushd %root_dir%
    setlocal enableDelayedExpansion
    
    for /f "tokens=* delims=" %%a in  ('dir /b /s  "page ?1?.jpg"') do (
        set "fdir=%%~dpa"
        set stripped=!fdir:~0,-1!
       for %%# in ("!stripped!") do ( 
         echo %%~n#
         set "dir_name=%%~n#"
        )
       copy /y "%%~fa" "%target_dir%\!dir_name!.jpg"  >nul 2>nul
    ) 
    endlocal 
    popd
    pause