Page 1 of 1

How can i solve this?

Posted: 19 Aug 2011 05:41
by renzlo
Hi All,

I have directory with structure like this:

Code: Select all

00004569 - 00004569_001
           00004569_002
           00004569_003

I want to write it in a text file so I did this:

Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=1 delims=" %%a in ('dir /b "*."') do (
set "main=%%a"
for /f "tokens=1 delims=" %%b in ('dir /b "!main!\*."') do (
set "sec=%%b"
echo !main! - !sec! >>list.txt
)
)


but the output is like this:

Code: Select all

00004569 - 00004569_001
00004569 - 00004569_002
00004569 - 00004569_003

I want it like this:

Code: Select all

00004569 - 00004569_001
           00004569_002
           00004569_003


Is this can be done?

Re: How can i solve this?

Posted: 19 Aug 2011 06:37
by dbenham
I'm going to assume that your starting directory structure does not have leading spaces in the names, so it would look like this:

Code: Select all

00004569 - 00004569_001
00004569_002
00004569_003
Otherwise DIR /B would not sort this way because space sorts before 0.

If I am correct then this should work:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=- " %%a in ('dir /b "*."') do (
  if "%%~b" neq "" (
      set "main=%%a"
      set "sec=%%b"
  ) else (
      set "sec=%%a"
  )
  echo !main! - !sec!>>list.txt
)


Dave Benham

Re: How can i solve this?

Posted: 19 Aug 2011 07:37
by renzlo
Hi Dave,

thanks for the reply, but the batch only outputted the !main!.

I think you misunderstood me Dave, I have a folder with this name 00004569 and inside that folder are 00004569_001, 00004569_002 and 00004569_003 subfolders, you can see it in my code.

Re: How can i solve this?

Posted: 19 Aug 2011 08:33
by dbenham
Oh my - I completely misread your post. Sorry.

Assuming the width of the directory name is constant, then (untested):

Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=1 delims=" %%a in ('dir /ad /b "*."') do (
  set "main=%%a - "
  for /f "tokens=1 delims=" %%b in ('dir /b "!main!\*."') do (
    set "sec=%%b"
    echo !main!!sec! >>list.txt
    set "main=           "
  )
)
It's probably a good idea to restrict the outer loop to directory names with the /AD option.

If the directory name width varies then main needs to have spaces added in front or behind to make it a fixed width, or you need to use the DOSTIPS strlen function to compute the length of main and then build a string of spaces with the correct width.

Dave Benham

Re: How can i solve this?

Posted: 19 Aug 2011 09:28
by Batcher
Why not use command tree?

Code: Select all

tree /f C:\test

Re: How can i solve this?

Posted: 19 Aug 2011 21:47
by renzlo
@dave,

your given code is not working due to dash (-) that you have declared in variable main, i have modified it and got it working, thanks a bunch.

Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=1 delims=" %%a in ('dir /ad /b "*."') do (
  set "main=%%a"
  for /f "tokens=1 delims=" %%b in ('dir /b "!main!\*."') do (
    set "sec=%%b"
    echo !main!   !sec! >>list.txt
    set "main=         "
  )
)


@batcher, i have tried it already but i need to list it with tab delimited. thanks for the reply.