Search code examples
batch-filefor-loopcd

How to navigate to subfolder in for loop in batch?


With cd %%~i I want to navigate the script to the next subfolder after U:\testbmbf\0012..... There are a lot of such subfolders on the same hierarchy level. Then the script should search for the files with 5 following extensions in all of these subfolders ONLY on the same hierarchy level.

The problem is that the script is going through entire hierarchy tree and searching on all possible sublevels.

How to "lock" the script in order to search for the file extensions ONLY on the next hierarchy level, but not deeper? CD %%i doesn't seems to work here.

@echo off
SETLOCAL enabledelayedexpansion
for  %%i in (U:\testbmbf\0012\) do (
    cd %%~i 
    for /r %%j in (*.tif) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
    for /r %%j in (*.txt) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
    for /r %%j in (*.pdf) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
    for /r %%j in (*.tei) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
    for /r %%j in (*.xml) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
    for /r %%j in (*.xls) do U:\md5.exe %%j >> U:\check_before.txt && echo checking md5sum %%j
)

Solution

  • Is this what you mean?

    @Echo Off
    For /D %%A In (U:\testbmbf\0012\*) Do (
        PushD "%%A"
        For %%B In (*.pdf *.tei *.tif *.txt *.xls *.xml) Do (
            Echo checking md5sum %%B
            U:\md5.exe "%%B">>U:\check_before.txt
        )
        PopD
    )