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.

  • 1
    Use 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 Oct 26 '18 at 12:36
  • 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 Oct 26 '18 at 12:41
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

| improve this answer | |
  • How can I "hide" the Level path ? – Wyz Oct 26 '18 at 14:38
  • Simply comment/remove the last line, it was implemented to help if no matching folder for all 4 pnr elements is found. – LotPings Oct 26 '18 at 14:40
  • You mean the set Level ? – Wyz Oct 26 '18 at 14:45
  • Yes, changed above. – LotPings Oct 26 '18 at 14:46
  • 1
    In comment I had an error but by deleting the line it works just fine thanks for your support. – Wyz Oct 29 '18 at 7:28
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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.