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
Help sorting a txt file
Moderator: DosItHelp
Re: Help sorting a txt file
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
)
)
Re: Help sorting a txt file
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!!!
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!!!
Re: Help sorting a txt file
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
)
)
Re: Help sorting a txt file
Perfect!!!!
Thank you again Foxidrive. You have made my day.. My boss will be impressed with this job I am trying to get!
Thank you again Foxidrive. You have made my day.. My boss will be impressed with this job I am trying to get!
