Search code examples
pdfmvcmder

Recursively move pdf into a given name folder


I have this situation:

Parental-folder

1Sub
      some pdf file
      1Sub-sub
      Other-sub [empty]
 2Sub
      some pdf file
      2Sub-Sub
      Other-sub [empty]

I want to move recursively the pdf into the related existing subfolder "Other-sub" (every subfolder has the same "other-sub").

I try this (on windows, with CMDER):

find . -maxdepth 2 -type f -exec sh -c 'exec mv $1 ./*/Other-sub/$1' sh {} ;

But it doesn't works. Any help? Thank you in advance!

Matteo Fadini


Solution

  • I suggested it would perhaps be best to use windows to provide a generic cmd file that you can run different ways for different cases.

    So here is a template you can call from cmder with a folder name as a start directory (I set the default to desktop) but you can also drag and drop a folder onto the .cmd to start it.

    MoveDown.cmd

    @echo off & Title Move files down into sub folders
    
    REM Set Vars presuming _TOP default is user\desktop but can be a dropped folder as a given unknown argument or manually run with a "drive:\folder path"
    
    SETLOCAL EnableExtensions DisableDelayedExpansion
    set "_TOP=%userprofile%\desktop"
    if exist "%~1" set "_TOP=%~1"
    
    set "_TARGET=Other-sub"
    set "_TYPE=*.PDF"
    
    :main
    for /R "%_TOP%" %%a in (.) do (
        if exist "%%a\%_TARGET%" (
            move /Y "%%a\%_TYPE%" "%%a\%_TARGET%"
        )
    )
    REM remove the pause if you are happy its not need to confirm actions when drag and dropping a folder
    pause
    endlocal & exit /B
    

    There are many ways to build your own scripts in windows so the above is using native cmd but you can use power-shell or VBscript or Jscript or yes even cmder macros and bash scripts. The big issue is which commands behave better or are easier to construct with error checking in one or the other.

    I included no error checks in the above so do test if adapting to other uses.