Log
Log

Reputation: 239

Display directory structure using relative paths

I want to generate a file that contains the directory structure of a given directory, as relative paths. Currently I have the following batch file:

@echo off
pushd "C:\TEST"
dir /AD /B /ON /S
popd

Its current output is this:

C:\TEST\one
C:\TEST\three
C:\TEST\two
C:\TEST\one\a
C:\TEST\three\d
C:\TEST\three\e
C:\TEST\three\f
C:\TEST\two\b
C:\TEST\two\c

I want the output to be:

one
three
two
one\a
three\d
three\e
three\f
two\b
two\c

EDIT: this question is not a duplicate. The other question shows files exclusively, now I need to get directories exclusively (not mixed with files).

Upvotes: 0

Views: 65

Answers (1)

Magoo
Magoo

Reputation: 80193

@echo OFF
SETLOCAL enabledelayedexpansion
SET "targetdir=c:\106x"
pushd "%targetdir%"
FOR /f "delims=" %%a IN ('dir /AD /B /ON /S') DO (SET "dirname=%%a"&ECHO(!dirname:%targetdir%\=!)
popd
GOTO :EOF

This should work for you provided the directoryname does not contain !

The directorynames are applied to %%a and transferred to dirname for manipulation. The target directoryname + a closing \ are then replaced by nothing for display.

Upvotes: 1

Related Questions