Search code examples
batch-filecommand-linewindowwindows-8.1

batch script moving multiple file extensions to sub folder passing parameters to a single subroutine


So, I am trying to code a batch script operation that handles moving certain types of file extensions (photos = .jpg, .png etc) to their respective sub directory.

The problem I am in counter is when I go to run the loop doesn't stop and I can't track if certain types are being looped through.

Is there a way of doing this operation within a single subroutine? Or a better way? Also, if you can leave helpful links as I really would like to understand what I am coding more thoroughly.

I am running Windows 8.1

    :cleanup
set Fdocs=%CD%\1_docs
set Fphotos=%CD%\2_photos
set Fdesigns=%CD%\3_designs
set Freviews=%CD%\4_reviews
set Ffinal=%CD%\5_final
set $ext=
set action=Cleaning

:: Create a subroutine function loop : FOR , go through array file extentions and move to destination
:: FOR /F
:fMover
for %%a in (%1) do move "%CD%\*%%a" "%CD%\%2"
echo %%a
timeout 1 >nul

:: CLEAN :: documents
set $ext=.txt .doc .docx .xls .csv .ppt .pptx
call :fMover %$ext% %Fdocs%
cls
echo %action% project folder.


:: CLEAN :: images
set $ext=.jpg .png .tiff .raw .nef .crw .dng
:: for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fphotos%"
call :fMover %$ext% %Fphotos%
cls
echo %action% project folder..
timeout 1 >nul


:: CLEAN :: designs
set $ext=.psd .psb
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\2_psd"
set $ext=.ai .svg .svgz .esp
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\1_ai"
set %ext=.indd .idml 
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\3_indd"
cls
echo %action% project folder...
timeout 1 >nul


cls
echo done.
timeout 2 >nul

Solution

  • Your batch structure could be as follows (you should skip over predefined subroutine and properly return from it):

        :cleanup
    set "Fdocs=%CD%\1_docs"
    set "Fphotos=%CD%\2_photos"
    set "Fdesigns=%CD%\3_designs"
    set "Freviews=%CD%\4_reviews"
    set "Ffinal=%CD%\5_final"
    set "$ext="
    set "action=Cleaning"
    
    :: CLEAN :: documents
    set "$ext=.txt .doc .docx .xls .csv .ppt .pptx"
    call :fMover "%$ext%" "%Fdocs%"
    cls
    echo %action% project folder.
    
    :: CLEAN :: images
    set "$ext=.jpg .png .tiff .raw .nef .crw .dng"
    :: for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fphotos%"
    call :fMover "%$ext%" "%Fphotos%"
    cls
    echo %action% project folder..
    timeout 1 >nul
    
    :: CLEAN :: designs
    set "$ext=.psd .psb"
    call :fMover "%$ext%" "%Fdesigns%"
    ::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\2_psd"
    set "$ext=.ai .svg .svgz .esp"
    call :fMover "%$ext%" "%Fdesigns%"
    ::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\1_ai"
    set "$ext=.indd .idml" 
    call :fMover "%$ext%" "%Fdesigns%"
    ::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\3_indd"
    cls
    echo %action% project folder...
    timeout 1 >nul
    
    cls
    echo done.
    timeout 2 >nul
    :: skip over predefined subroutine(s) and return from script
    goto :eof
    
    :: Create a subroutine function loop : FOR , go through array file extentions and move to destination
    :: FOR /F
    :fMover
    for %%a in (%~1) do move "%CD%\*%%~a" "%CD%\%~2"
    echo %%a
    timeout 1 >nul
    :: return from subroutine
    goto :eof
    

    Resources: