batch-filefile-organization

Running a .bat only within sub-directory from a .bat in root directory?


I have a series of commands that moves files from folders A-E to one folder X, e.g.:

for /f "delims=" %%a in (
    'dir /s /b ^| find /i "\Folder A\"'
    ) do move "%%a" "G:\Folder X"
for /f "delims=" %%a in (
    'dir /s /b ^| find /i "\Folder B\"'
    ) do move "%%a" "G:\Folder X"

etc

and once they're in X, running a .bat within X to sort those files based on their names into folders by name:

for /f "tokens=2 delims= " %%b in ('dir /b *.pdf') do (
    md %%b > nul 2>&1
    move *%%b*.pdf %%b
    )

but it seems to want to run the .bat within X in the root folder instead of only \X\. Is there any way to tell it to run X\*.bat only within X? Aside from changing *.pdf to X\*.pdf? I'm trying to write so that each .bat is as portable as possible.


Solution

  • If I understand you right, the only problem is that the current directory doesn't change, and you'd like to change it to G:\Folder X when you run the batch file in that folder.

    You could do this from the "parent" batch file:

    pushd "G:\Folder X"
    call "G:\Folder X\process.bat"
    popd
    

    Or from within the "child" batch file you could force it to always set its current directory to the directory containing the batch file:

    pushd "%~dp0"
    :: do your stuff
    popd