Find folder using batch

1

I'm trying to make a script that helps to go through a big library with lots of folders, names and numbers. The part numbers are divided in 4 parts XXX.XX.XX.XXXX. To help the searching the library folders are set like this: (example names) 100_Vegetal\01_Flower\01_Red\0001_Rose

My issue is the name of the folders, if the folder was only called "100" it would be easy to go through. Here is the code that I use to separate the part number and to try to open the folder.

set /p pnr="Please enter the PNR : " 
echo %pnr%
echo %pnr:~0,3% 
echo %pnr:~4,2%
echo %pnr:~7,2%
echo %pnr:~10,4%
explorer ".library\%pnr:~0,3%(*)"

I would like to open a folder that has the first 3 digits of the part number. Can you please help me with that issue. I tried with the star but it open the explorer...

Thank you.

Wyz

Posted 2018-10-26T12:03:51.967

Reputation: 25

1Use for /f "tokens=1-4 delims=." %%A in ("%pnr%") do Echo %%A,%%B,%%C,%%D to split the pnr into four for meta variables %%A..%%D – LotPings – 2018-10-26T12:36:05.553

Explorer doesn't accept wild-cards in a passed directory name: you need something like for /d %%d in (".library\%pnr:~0,3%*") do start /w explorer "%%d". – AFH – 2018-10-26T12:41:25.663

Answers

1

Provided the numbers from the pnr are unique in the folder tree, the following batch will open directly the folder matching all 4 numbers by iterating with successive for /d loops

Take care, the for meta variables distinguish between upper/lower case,
so the pnr is split into %%A..%%D and the found folders are in %%a..%%d

Sample tree on my ram drive A:

> tree
A:.
└───.library
    └───100_Vegetal
        └───01_Flower
            └───01_Red
                └───0001_Rose

:: Q:\Test\2018\10\26\SU_1370234.cmd
@Echo off 
set "Base=A:\.library"
set /p pnr="Please enter the PNR : " 
:: set pnr=100.01.01.0001
echo %pnr%

:: reset Level variables
for /l %%L in (1,1,4) do Set "Level%%L="

:: first split pnr, then dive into folders
for /f "tokens=1-4 delims=." %%A in ("%pnr%" ) Do (
  for /d %%a in ("%Base%\%%A*") Do (Set Level1=%%a
    for /d %%b in ("%%a\%%B*") Do  (Set Level2=%%b
      for /d %%c in ("%%b\%%C*") Do (Set Level3=%%c
        for /d %%d in ("%%c\%%D*") Do (Set Level4=%%d
          Explorer "%%d
        )
      )
    )
  )
)
:: set Level

Sample output:

> Q:\Test\2018\10\26\SU_1370234.cmd
Please enter the PNR :
100.01.01.0001
Level1=A:\.library\100_Vegetal
Level2=A:\.library\100_Vegetal\01_Flower
Level3=A:\.library\100_Vegetal\01_Flower\01_Red
Level4=A:\.library\100_Vegetal\01_Flower\01_Red\0001_Rose

Explorer opens here in A:\.library\100_Vegetal\01_Flower\01_Red\0001_Rose

LotPings

Posted 2018-10-26T12:03:51.967

Reputation: 6 150

How can I "hide" the Level path ? – Wyz – 2018-10-26T14:38:54.237

Simply comment/remove the last line, it was implemented to help if no matching folder for all 4 pnr elements is found. – LotPings – 2018-10-26T14:40:56.277

You mean the set Level ? – Wyz – 2018-10-26T14:45:20.903

Yes, changed above. – LotPings – 2018-10-26T14:46:57.607

If I put it as a comment it gives me a syntax error... – Wyz – 2018-10-26T14:51:01.133

It runs here as posted above without flaw. – LotPings – 2018-10-26T14:53:21.497

1In comment I had an error but by deleting the line it works just fine thanks for your support. – Wyz – 2018-10-29T07:28:34.103

2

You can use the dir command with the /S /B /AD parameters in a for /f loop and make it recursively traverse the source folder for directories only and then iterate those folders with the numbers you type using a wildcard to be opened in Explorer.

Script Example

set /p pnr="Please enter the PNR : " 
set pnr=%pnr:~0,3%
FOR /F "TOKENS=*" %%a IN ('DIR /S /B /AD ".library\%pnr%*"') DO explorer "%%~fa"

Further Resources

  • For /F
  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
    
  • Dir

Pimp Juice IT

Posted 2018-10-26T12:03:51.967

Reputation: 29 425