How can i solve this?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

How can i solve this?

#1 Post by renzlo » 19 Aug 2011 05:41

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?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How can i solve this?

#2 Post by dbenham » 19 Aug 2011 06:37

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

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: How can i solve this?

#3 Post by renzlo » 19 Aug 2011 07:37

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How can i solve this?

#4 Post by dbenham » 19 Aug 2011 08:33

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

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: How can i solve this?

#5 Post by Batcher » 19 Aug 2011 09:28

Why not use command tree?

Code: Select all

tree /f C:\test

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: How can i solve this?

#6 Post by renzlo » 19 Aug 2011 21:47

@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.

Post Reply