Multiple Directories and Sub Directories to txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Multiple Directories and Sub Directories to txt file

#1 Post by Defcon5 » 12 Jan 2013 20:30

Hello,

I`m verry new here and do not know much about batch files and so on, but I hope you can help me out here.
I have a problem, I want to create a txt file from a directory.. okay that works fine, now a little bit harder.
I want to create a txt file from Dir/SubDir/FileDataMap

I got a structure like this :

- Dir001

-+ SubDir001a

--+ FileData001a1
--+ FileData001a2
--+ FileData001a3
--+ FileData001a4

-+ SubDir001b

--+ FileData001b1
--+ FileData001b2
--+ FileData001b3
--+ FileData001b4

-+ SubDir001c

--+ FileData001c1
--+ FileData001c2
--+ FileData001c3
--+ FileData001c4

-+ SubDir001d

--+ FileData001d1
--+ FileData001d2
--+ FileData001d3
--+ FileData001d4

- Dir002

-+ SubDir002a

--+ FileData002a1
--+ FileData002a2
--+ FileData002a3
--+ FileData002a4

-+ SubDir002b

--+ FileData002b1
--+ FileData002b2
--+ FileData002b3
--+ FileData002b4

-+ SubDir002c

--+ FileData002c1
--+ FileData002c2
--+ FileData002c3
--+ FileData002c4

-+ SubDir002d

--+ FileData002d1
--+ FileData002d2
--+ FileData002d3
--+ FileData002d4

You get the picture ;)
Well I got this script and it works till the last part, there it hangs.
What can i do to fix it?
Please help me.

Regards, Defcon5

Code: Select all

@echo off
cls

if exist Tree.txt goto end1
dir /on /b >>Tree.txt
echo creating txt file
:end1
echo Made Tree.txt...!
pause
cls

if exist SubTree.txt goto end2
if exist Tree.txt goto file_exists_a
echo Tree.txt not found!
goto end2
:file_exists_a
for /f "delims=" %%a IN (Tree.txt) do echo Listing directory: %%a...&&dir /on /b >>SubTree.txt "%%a"
:end2
echo Made SubTree.txt....!

pause
cls

if exist SubTree.txt goto file_exists_b
echo SubTree.txt not found!
goto end3
:file_exists_b
for /f "delims=" %%c IN (Tree.txt) do cd "%%c" && for /f "delims=" %%b IN (..\SubTree.txt) do echo Listing directory: %%c \ %%b ...&& dir /on /b >>..\Directories.txt "%%b"
:end3

echo Echt wel klaar....!?
pause
cls

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

Re: Multiple Directories and Sub Directories to txt file

#2 Post by foxidrive » 13 Jan 2013 00:34

Posting a batch file that doesn't work when we don't know the task isn't going to help us solve the issue.

How about you explain what you need to do with the directory structure?

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

Re: Multiple Directories and Sub Directories to txt file

#3 Post by foxidrive » 13 Jan 2013 00:40

If you want a list of the files in the tree then is this sufficient?

@echo off
dir /b /s /a:-d /o:n >>directories.txt


The main flaw in your code is where you put "%%a" and "%%b" after the filenames. It should be in the DIR command.

EG:

dir "%%a" /b /on >>file.txt

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#4 Post by Defcon5 » 13 Jan 2013 03:14

The script does work, but after when it gets to this point :

Code: Select all

if exist SubTree.txt goto file_exists_b
echo SubTree.txt not found!
goto end3
:file_exists_b
for /f "delims=" %%c IN (Tree.txt) do cd "%%c" && for /f "delims=" %%b IN (..\SubTree.txt) do echo Listing directory: %%c \ %%b ...&& dir /on /b >>..\Directories.txt "%%b"
:end3


It get`s into a loop and doesn`t make the txt file.
He does see the first dir/subdir/FileData00%%
but when it needs to do the others, it doesn`t find it.

Ohw, BTW.. it needs to make a file of the dirs and not of the files that are in the FileData00%% ;)
Even the FileData00%% is a subdir.

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

Re: Multiple Directories and Sub Directories to txt file

#5 Post by foxidrive » 13 Jan 2013 08:03

If you want a list of the folders then use this:

Code: Select all

@echo off
dir /b /s /a:d /o:n >>directories.txt

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#6 Post by Defcon5 » 13 Jan 2013 08:08

did that already, but I don`t want to see the whole structure where everything is.
I don`t want to see this out put :

Dir001\SubDir001a\FileData001a1\dirname001
Dir001\SubDir001a\FileData001a1\dirname002
Dir001\SubDir001a\FileData001a1\dirname003
Dir001\SubDir001a\FileData001a1\dirname004

But
dirname001
dirname002
dirname003
dirname004

so it must strip all the Dir001\SubDir001a\FileData001a1\ before it.

Even thnx for the help anyhow ;)

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

Re: Multiple Directories and Sub Directories to txt file

#7 Post by foxidrive » 13 Jan 2013 08:10

Like this?

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /s /a:d /o:n ') do >>directories.txt echo %%~nxa

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#8 Post by Defcon5 » 13 Jan 2013 11:05

Will try it, could work ;)

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#9 Post by Defcon5 » 13 Jan 2013 11:46

Nope, not what i needed, but we solved it with a php file.

here for the people who needs it too.

Code: Select all

<?php
   $data = array();
   if ($root = opendir('.'))
   {
      while ($file = readdir($root))
      {
         if ($file == '.' || $file == '..')
            continue;

         if (is_dir($file))
         {
            if ($sub = opendir($file))
            {
               while ($subfile = readdir($sub))
               {
                  if ($subfile == '.' || $subfile == '..')
                     continue;
                  if (is_dir($file."/".$subfile))
                  {
                     if ($sub2 = opendir($file."/".$subfile))
                     {
                        while ($subfile2 = readdir($sub2))
                        {
                           if ($subfile2 == '.' || $subfile2 == '..')
                              continue;
                           if (is_dir($file."/".$subfile."/".$subfile2))
                              array_push($data,$subfile2);
                        }
                        closedir($sub2);
                     }
                  }
               }
               closedir($sub);
            }
         }
      }
      closedir($root);
   }
   sort($data);
   echo implode("\n",$data);
   file_put_contents("files.txt", implode("\n",$data));

?>

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Multiple Directories and Sub Directories to txt file

#10 Post by Squashman » 13 Jan 2013 13:18

We are not a php shop. I am sure your problem could be solved with batch but you are not explaining clearly what your desired output is. If you could show us a better example of your input and what your desired output is we could probably solve this with batch.

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#11 Post by Defcon5 » 13 Jan 2013 16:51

I explained myself verry clearly.

the dir structure is 3 deep and i need only the fourth dirnames of that in the output.txt
But clearly some doesn`t seem to see it.
I`m a dutch person, but I did explained it verry clearly here.

Read all of my post, than maybe you see what i ment.

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

Re: Multiple Directories and Sub Directories to txt file

#12 Post by foxidrive » 13 Jan 2013 17:03

Defcon5 wrote:I explained myself verry clearly.

the dir structure is 3 deep and i need only the fourth dirnames of that in the output.txt


Where did you say that?

Is this what you want?

FileData001a4
FileData001b4
FileData001c4
FileData001d4
FileData002a4
FileData002b4
FileData002c4
FileData002d4

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#13 Post by Defcon5 » 13 Jan 2013 17:18

foxidrive wrote:
Defcon5 wrote:I explained myself verry clearly.

the dir structure is 3 deep and i need only the fourth dirnames of that in the output.txt


Where did you say that?

Is this what you want?

FileData001a4
FileData001b4
FileData001c4
FileData001d4
FileData002a4
FileData002b4
FileData002c4
FileData002d4


Defcon5 wrote:did that already, but I don`t want to see the whole structure where everything is.
I don`t want to see this out put :

Dir001\SubDir001a\FileData001a1\dirname001
Dir001\SubDir001a\FileData001a1\dirname002
Dir001\SubDir001a\FileData001a1\dirname003
Dir001\SubDir001a\FileData001a1\dirname004

But
dirname001
dirname002
dirname003
dirname004

so it must strip all the Dir001\SubDir001a\FileData001a1\ before it.

Even thnx for the help anyhow ;)


And most before, even the first post i said it already.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Multiple Directories and Sub Directories to txt file

#14 Post by abc0502 » 13 Jan 2013 17:46

@Defcon5 , if you just need to get the final folder names in all these main DIRs and the tree and subtree files is not going to be used in any other thing "usless" then just use this code,

Code: Select all

@Echo Off
For /F "delims=" %%A in ('DIR /B /S /A:D "*.*"') Do Echo %%~nA
pause
and add >>"File.txt" at the end of the %%~nA to get the output in a file

Defcon5
Posts: 14
Joined: 12 Jan 2013 20:13

Re: Multiple Directories and Sub Directories to txt file

#15 Post by Defcon5 » 13 Jan 2013 18:09

abc0502 wrote:@Defcon5 , if you just need to get the final folder names in all these main DIRs and the tree and subtree files is not going to be used in any other thing "usless" then just use this code,

Code: Select all

@Echo Off
For /F "delims=" %%A in ('DIR /B /S /A:D "*.*"') Do Echo %%~nA
pause
and add >>"File.txt" at the end of the %%~nA to get the output in a file



Already done that, read post before

foxidrive wrote:Like this?

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /s /a:d /o:n ') do >>directories.txt echo %%~nxa



Problem is as following.
I got movies, but stored them in the following way.

-- Movies --\-- Box Sets --\Box Set - Movie name\Year - Movie 1
-- Movies --\-- Box Sets --\Box Set - Movie name\Year - Movie 2
-- Movies --\-- Box Sets --\Box Set - Movie name\Year - Movie 3
-- Movies --\-- Box Sets --\Box Set - Movie name\Year - Movie 4
etc...

and

-- Movies --\-- Year 2000 - 2009 --\Year 2000\2000 - Movie name
-- Movies --\-- Year 2000 - 2009 --\Year 2001\2001 - Movie name
-- Movies --\-- Year 2000 - 2009 --\Year 2002\2002 - Movie name
-- Movies --\-- Year 2000 - 2009 --\Year 2003\2004 - Movie name
etc...

And I would make a list of all the movies I got.
So i only need the following output.

Year - Movie 1
Year - Movie 2
Year - Movie 3
Year - Movie 4
2000 - Movie name
2001 - Movie name
2002 - Movie name
2003 - Movie name

Al the others does I not need and in the Year - Mpvie 1 are also the files like AUDIO_TS and VIDEO_TS and that I don`t want that in the output as well.
I run the script in the dir of -- Movies -- and than the script need know the first 2 depths (-- Box Sets --\Box Set - Movie Name) or (-- Year 2000 - 2009 --\Year 2000).
So thats why I first let it create Tree.txt and after that SubTree.txt, from there it needs use these 2 txt files to create a output.txt from the 3th directory name.

So I hope now you know what it must do? I hope so, cause better than this I can`t explain it anymore.

Post Reply