[Solved] Get a list of directories

Talk about anything at all....
Post Reply
User avatar
RusselB
Moderator
Posts: 6575
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

[Solved] Get a list of directories

Post by RusselB »

I know how to get a list of directories with no file limitations and how to get a list of files (with the directory name) if I know the filename and/or extension.
What I'm wondering about is getting a list of directories that do not contain a specific extension.
To be more specific, I want to search all of the directories and sub-directories on a drive and only have the directories that do not contain any files that have the !qb extension.
I realize I could make a list of all of the directories then a second list of all of the directories that have files that have that extension, then remove the directories in the second list from the first list, but I'm hoping that there's another (and hopefully better) option.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
Villeroy
Volunteer
Posts: 30666
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get a list of directories

Post by Villeroy »

RusselB wrote: Sat May 21, 2022 8:38 pm I know how to get a list of directories with no file limitations
OK, how do you do that? What is your approach?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RusselB
Moderator
Posts: 6575
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: Get a list of directories

Post by RusselB »

Villeroy wrote: Sat May 21, 2022 10:53 pm
RusselB wrote: Sat May 21, 2022 8:38 pm I know how to get a list of directories with no file limitations
OK, how do you do that? What is your approach?
I would use the DOS command DIR.
Something like dir c:\ /ad /s > cdrive.txt

This would give me a list of the directories and sub-directories of the C drive and have that list sent to the file cdrive.txt
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
John_Ha
Volunteer
Posts: 8968
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Get a list of directories

Post by John_Ha »

This is a Windows question. Google for help - I quickly found lots of hits.
Last edited by John_Ha on Sun May 22, 2022 1:02 am, edited 1 time in total.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
Zizi64
Volunteer
Posts: 10481
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Get a list of directories

Post by Zizi64 »

https://finance.uw.edu/recmgt/resources ... re-windows

Or do you want to achieve it with AOO/LO macros?

I suppose it is will be very slow with an Office macro on the C:\ - what contains the whole Windows and many other things...
Tibor Kovacs, Hungary; LO6.1.6, 6.4.7 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.1.1;AOO4.1.9
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
robleyd
Moderator
Posts: 4342
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Get a list of directories

Post by robleyd »

If your version of Windows has findstr you could pipe the output of dir c:\ /ad /s through that before redirecting to a file.
Cheers
David
Apache OpenOffice 420m2(Build:9821) - Slackware 15 - 64 bit
LibreOffice 6.4.6.2 (SlackBuild for 6.4.6 by Eric Hameleers) - Slackware 15 - 64 bit
Apache OpenOffice 4.1.4 - Windows 7 Virtual machine
User avatar
MrProgrammer
Moderator
Posts: 4320
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Get a list of directories

Post by MrProgrammer »

RusselB wrote: Sat May 21, 2022 8:38 pm I want to search all of the directories and sub-directories on a drive and only have the directories that do not contain any files that have the !qb extension.
X:
+- A
+---- Foo.txt
+- B
+---- C
+-------- Foo.!qb

If we have two files, X:\A\Foo.txt and X:\B\C\Foo.!qb, it is clear to me that directory X:\A does not contain any files that have the !qb extension. And it is clear that directory X:\B\C does contain a file with the !qb extension.

But do you want X:\B or not? That is, does "contains" mean contains directly, or does it mean contains within its tree? Does directory X:\B "contain" a file with the !qb extension?

John_Ha wrote: Sun May 22, 2022 12:56 am This is a Windows question.
Agreed. I note that this has been posted in General Discussion, not in Macros and UNO API. Unless you intend to accomplish this with an OpenOffice/LibreOffice macro, it is off-topic for our forums, as a question about soccer would be. A *NIX shell script or a Perl program could easy produce this list.

robleyd wrote: Sun May 22, 2022 1:21 am If your version of Windows has findstr you could pipe the output of dir c:\ /ad /s through that before redirecting to a file.
Windows findstr is the weak equivalent of *NIX grep and may be a good tool for your project.
Mr. Programmer
AOO 4.1.7 Build 9800 on MacOS 10.14.6.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
RusselB
Moderator
Posts: 6575
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: Get a list of directories

Post by RusselB »

In the above example, for this project, I'd be wanting the X:\A returned, but not the X:\B\C
If I had X:\B\Foo.txt then X:\B would be returned.

Hope this helps.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
RoryOF
Moderator
Posts: 33546
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Get a list of directories

Post by RoryOF »

I can't help specifically but from memory the task is called "tree-walking", which keyword might be worth searching for.
Apache OpenOffice 4.1.12 on Xubuntu 20.04.4 and very infrequently on Win2K/XP
User avatar
Villeroy
Volunteer
Posts: 30666
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get a list of directories

Post by Villeroy »

Python searching subdirectories in my documents directory not having any ODF documents:

Code: Select all

import glob
	lst = glob.glob('/home/andreas/Dokumente/LibreOffice/**/', recursive=True)
	for s in lst:
		glob.glob(s +'*.od?') and lst.remove(s)
print(lst)
https://docs.python.org/3/library/glob.html

The result looks plausible. If LibreOffice/sub1/sub2/sub3/ has *.od? documents, it is not in the result set but parent directory LibreOffice/sub1/sub2 remains in the list because it has no office documents.
 Edit: Sorry, instead of print(lst) which dumps the textual list representation to the console I should write the result to a text file. 
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 30666
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get a list of directories

Post by Villeroy »

The attached script seems to do the job on my system writing the resulting directory list to the specified output file.
Edit the path and pattern declarations on top of the script. The very first line declares the interpreter to be used when the script file is marked as executable on a Unix like system.

On Windows you have to "open" the script with a Python interpreter like the one in the program folder of your office installation.
Attachments
list_dirs.txt
(325 Bytes) Downloaded 19 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
MrProgrammer
Moderator
Posts: 4320
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Get a list of directories

Post by MrProgrammer »

RusselB wrote: Sun May 22, 2022 3:40 pm If I had X:\B\Foo.txt then X:\B would be returned.
Based on that we have the simple case, since X:\B does not have a file with extension !qb. If you had a good operating system (MacOS, UNIX, Linux) you could use:

find / -type d | while IFS="" read d
   do ls "$d"/*.\!qb &>/dev/null || echo "$d"
done

15 years ago using second-class Windows at work I used for /r for this type of task. Maybe the following will do what you want. I have no Windows system to run any tests. I don't remember exactly how quotes work with filename wildcards so you might need "%d/*.!qb" instead. Or you might need to escape the ! character as ^!. The script depends on dir settting the exit code in Windows to indicate if any files with the extension are found, as ls does elsewhere. You may have to enable command extensions to use for /r. Script writing in Windows is hard because Microsnot's documentation for it is so poor. But long study and much testing eventually allowed me to write many useful scripts for the company.

@echo off
for /r C:\ %d in (.) do (
   dir "%d"\*.!qb 1>nul 2>nul || echo "%d"
)

Villeroy wrote: Sun May 22, 2022 4:44 pm Sorry, instead of print(lst) which dumps the textual list representation to the console I should write the result to a text file.
A redirection operator takes care of that in MacOS, UNIX, Linix, and even Windows:

( «commands» ) >«file»

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800 on MacOS 10.14.6.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
Villeroy
Volunteer
Posts: 30666
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get a list of directories

Post by Villeroy »

MrProgrammer wrote: Tue May 24, 2022 5:15 pm A redirection operator takes care of that in MacOS, UNIX, Linix, and even Windows:
Well, my print(arrayVar) in Python printed a hard coded array like: ['a', 'b', 'c', 4, 5, 6] to standard out. Meanwhile I tried my script on a Windows PC. It works with Windows system paths as well.

This version prints to standard out all directories without *.od? files:

Code: Select all

S:\Data\Documents>"C:\Program Files\LibreOffice\program\python.exe" list_dirs.txt
where list_dirs.txt is this Python script:

Code: Select all

import glob
sdir = 'S:\Data\Documents\\'
spattern = '*.od?'
lst = glob.glob(sdir +'**/', recursive=True)
for s in lst:
    if not glob.glob(s + spattern): print(s +'\n')
The \\ in 'S:\Daten\Documents\\' denotes a literal backslash that does not escape the apostroph.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RusselB
Moderator
Posts: 6575
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: Get a list of directories

Post by RusselB »

Thanks to all. Got a few options to try when I find the time,
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
Post Reply