Domanda

I am new to batch scripting. I am working on the following,

I have many folders named as follow

02_25-06_00
02_25-06_10
02_25-06_20
02_25-06_30
02_25-06_40
02_25-06_50
02_25-07_00

where 02 is the month, 25 is the date, 06 is the hours and 00 is the minutes

Within each of these files, there are two folders named A and B. Each of the these folders has 5 text files.

I need a batch script that gets a time interval, like 06:00 to 06:20 as input and displays all the text files within each of the two sub folders, as follows

02_25-06_00-A1.txt
02_25-06_00-A2.txt
02_25-06_00-A3.txt
02_25-06_00-A4.txt
02_25-06_00-A5.txt
02_25-06_00-B1.txt
02_25-06_00-B2.txt
02_25-06_00-B3.txt
02_25-06_00-B4.txt
02_25-06_00-B5.txt
02_25-06_10-A1.txt
02_25-06_10-A2.txt
02_25-06_10-A3.txt
02_25-06_10-A4.txt
02_25-06_10-A5.txt
02_25-06_10-B1.txt
02_25-06_10-B2.txt
02_25-06_10-B3.txt
02_25-06_10-B4.txt
02_25-06_10-B5.txt

Please help me in this batch script...

È stato utile?

Soluzione

@echo off
    setlocal enableextensions enabledelayedexpansion

    set /p "start=Start (hh:mm): "
    set /p "end=End (hh:mm): "

    set "somewhere=%cd%"

    for /f "tokens=1-2 delims=-" %%a in ('dir /b /on /ad "%somewhere%"') do (
        if "%%b" geq "%start::=_%" if "%%b" leq "%end::=_%" (
            for %%c in ( "%somewhere%\%%a-%%b\a\*.*" "%somewhere%\%%a-%%b\b\*.*") do (
                for /f "tokens=*" %%d in ("%%~dpc.") do (
                    echo(%%a-%%b-%%~nxd%%~nxc
                )
            )
        )
    )

pause

Here is a batch file to create the test case folders and files:

@echo off
for %%a in (1 2 3 4 5) do for %%b in (A B) do for %%c in (
02_25-06_00
02_25-06_10
02_25-06_20
02_25-06_30
02_25-06_40
02_25-06_50
02_25-07_00
) do md %%c\%%b 2>nul& type nul>%%c\%%b\%%a.txt

EDITED - Include code changes to adapt to new folder naming (stated in comments). Changes included:

  • folder a = 9k (changes in for %%c files set)

  • folder b = c2 (changes in for %%c files set)

  • time named folders = hh-mm_hh-mm (changes in for %%a delims, hour comparision variable substitution, for %%c files set)

The rest of the code remains without changes

for /f "tokens=1-2 delims=_" %%a in ('dir /b /on /ad "%somewhere%"') do (
    if "%%b" geq "%start::=-%" if "%%b" leq "%end::=-%" (
        for %%c in ( "%somewhere%\%%a_%%b\9k\*.*" "%somewhere%\%%a_%%b\c2\*.*") do (
            for /f "tokens=*" %%d in ("%%~dpc.") do (
                echo(%%a-%%b-%%~nxd%%~nxc
            )
        )
    )
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top