Help sorting a txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
TomBombo
Posts: 5
Joined: 12 Aug 2013 21:54

Help sorting a txt file

#1 Post by TomBombo » 12 Aug 2013 21:59

HI I need help to write a batch file that will sort a text file of a directory listing and rewite it into different files depending on folder depth.
e.g. input which contains something like

g:\folder1
g:\folder1\folder2
g:\folder3
g:\folder4\folder5
g:\folder6\folder7\folder8
Now I want to sort this by counting the delims ('\') and write them to different files so I should have 3 different files.. 1 folder deep listings(with 2 lines), 2 folder deep listings(2 lines), 3 folder deep listings(1 line).

I have this

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=\" %%a in (f.txt) do (
set /a N+=1
Set VAR1=word1 word2 word3 word4
Set count=0
For %%j in (%VAR1%) Do Set /A count+=1
Echo.%%a >> %count%.txt
)
but its not very close >.<
Thanks in advance

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help sorting a txt file

#2 Post by foxidrive » 12 Aug 2013 22:33

This uses a helper batch file called repl.bat from - viewtopic.php?f=3&t=3855

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /s /a-d ') do (
  for /f %%b in ('echo "%%a"^|repl "\\" "\r\n" x ^|find /c /v "" ') do (
    >>"depth - %%b.txt" echo %%a
  )
)

TomBombo
Posts: 5
Joined: 12 Aug 2013 21:54

Re: Help sorting a txt file

#3 Post by TomBombo » 12 Aug 2013 23:28

WOW.. Thankyou Foxi..

I got it working.. awesome!!

A couple of quick questions..

1. the file names its generating are all out by 1..
i.e. depth - 6 is actually a 5 depth folder path.. Not a big issue coz I can eaily rename all the outputs but will saying "depth - %%b-1.txt work? I guess not

Also I had to remove the ' from where I put my file name right?
i.e. I changed (' dir /b /s /a-d ') to ( input.txt /b /s /a-d ).

Thanks again!!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help sorting a txt file

#4 Post by foxidrive » 12 Aug 2013 23:41

Try this. You could use delayed expansion and calculate one number less but this allows ! characters in the pathnames.

Code: Select all

@echo off
for /f "delims=" %%a in (' type "input.txt" ') do (
  echo "%%a"
  for /f %%b in ('echo "%%a"^|repl "^....(.*)$" "$1" ^|repl "\\" "\r\n" x ^|find /c /v "" ') do (
    >>"depth - %%b.txt" echo %%a
  )
)

TomBombo
Posts: 5
Joined: 12 Aug 2013 21:54

Re: Help sorting a txt file

#5 Post by TomBombo » 13 Aug 2013 00:01

Perfect!!!!

Thank you again Foxidrive. You have made my day.. My boss will be impressed with this job I am trying to get! :wink:

Post Reply