2

I faced a problem trying to implement a .bat file and I didn't find an answer. I try to display all folder names without the system path.

I've tried something like that but it doesn't work.

@echo off
set back=%cd%
for /d %%I in (C:\test\test*) do (
    cd %%I 2>&1 >nul
    echo %%~nxI:~10,5
)
cd %back% 2>&1 >nul

Do you have a solution please ? :-)

3 Answers 3

1

Substrings do only work with normal variables, not the for meta variables.
Copy content to a var and as this is inside a (code block) you need DelayedExpansion
Using the pseudo call method:

:: Q:\Test\2018\07\26\SU_1345000.cmd
@echo off
PushD .
for /d %%I in (A:\test\test*) do (
    Set "Dir=%%I"
    Call echo %%Dir:~10,5%%
)
PopD

In a tree like this:

> tree 
└───test
    ├───test1
    ├───test2
    └───test3

that returns

> Q:\Test\2018\07\26\SU_1345000.cmd
st1
st2
st3

Possibly your offset wasn't correct.

0

You could use dir /ad /b C:\test\test*

@echo off
set back=%cd%
for /d %%I in (C:\test\test*) do (
    dir /ad /b %%I
)
cd %back% 2>&1 >nul

This script would print all folder names in the folders C:\test\test*

0

Thanks for your answers. Here is a solution that worked for me :

@echo off

setlocal EnableDelayedExpansion

for /d %%I in (C:\test\testV*) do (     
    set current_folder=%%I
    set test_version=!current_folder:~12,6!
    set test_version=!test_version:_=.!

    echo !current_folder!
    echo !test_version!
)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .