XPath Like Regex One or More - xpath

I want to select something is a child of 1 to N of the same kind of nodes nested. The xml would look something like this:
<Folder>
<Folder>
<Folder>
<Folder>
<File></File>
</Folder>
</Folder>
</Folder>
</Folder>
or
<Folder>
<File></File>
</Folder>
The Files I am selecting must be a child of folder and none of those folders can have anything other than a folder as an ancestor. I don't want to use descendants to get all Files only to check each of their ancestors. I would rather follow the Folder chain and then stop at any Files directly after a Folder. Something like regex's + is what I'm looking for (./Folder+/File) but I don't know what that syntax is if it exists.

XPath has no "repeating axis" construct; you'll need to use a combination of the XPath axes and predicate conditions to achieve your desired result.
Here's an expression that gets all File elements that are children of Folder elements, and then checks that the Folder element's ancestors are all Folder elements:
//Folder/File[fn:not(ancestor::*/fn:local-name(.) != "Folder")]

Related

Copy directories if their subdirectories contain "connect.txt"

I want to copy multiple directories from one location to another location only if any of the subdirectories of those contain connect.txt file in them.
Example:
ANIMAL\DOG\CONNECT.TXT
PLANET\EARTH\CONNECT.TXT
SYSTEM\USER\ADMIN.TXT
Then I ONLY want to copy ANIMAL & PLANET directories to C:\DESKTOP.
move *\*\connect.txt C:\Desktop
This uses a regular expression which would work if you're really wanting to look only under the subdirectories of all directories at some location.

7z: Is it possible to exclude files conditionally?

It is possible to exclude files from zipping them with the 7zip -x switch, which allows wildcards too. So I can exclude all text files like this
7z a output.zip myfolder -x\!*.txt.
Now I want some txt files not to be excluded if they have a special name, like all text files named like this: *-KEYWORD.txt
I tried to use the exclude switch with the include switch together, like 7z a -xr\!*.txt -ir\!*KEYWORD.txt output.zip myfolder, but once the exclude switch is invoked, the include switch doesn't seem to reinclude excluded files again.
Is it possible to only include text files named like this, while excluding all other text files, inside the 7z syntax?
So this seems not to be possible in one command, especially not with the include and the exclude switch used both.
The solution I use in my script now is just to make two commands, the first excludes all files ending on *.txt, then another 7z command attaches all files like *-KEYWORD.txt to the package. It's not great but it works.

How to include a directory of files with RST and Sphinx

I am trying to write documentation and want and have multiply files used by multiple toc trees. Previously I used an empty file with .. include:: <isonum.txt> however, this does not work for multiply files in a directory with sub directories. Another solution I have used was to use a relative file path to the index file I am linking to. However this messes up the sphinx nav tree. So my question is how to include a directory of files with RST and Sphinx?
It can't be done, unfortunately.
The toctree directive has a glob option, which you would use like so:
.. toctree::
:glob:
generated/*
But this option is not available in the include directive.
Maybe start an issue for it?
Perhaps indicate the start and end of the section where the files should go with a comment (.. START_GLOB_INCLUDE etc), and then have a build pre-process step that finds the files you want and rewrites that section of the master file.

list all files in multiple directories

I would like to list all files in a certain directory, and list them with their full path.
I have a series of directories like this:
user.newskims.131017222704/
user.newskims.131017222741/
user.newskims.131017222822/
user.newskims.131017222949/
If I do
ls user.newskims.131017222*
The output has lines like this which I want to eliminate:
user.newskims.131017222822:
It also doesn't give the full path. Is there a way to make it list all of the files inside, and only those files and no additional rows, and with the full path?
You can list file with full path of a given directory using printf:
printf "$PWD%s\n" user.newskims.131017222/*

filtering out FileUtils.cp_r

FileUtils.cp_r does exactly what I need, but it's not able to filter out files by some pattern.
So, I would like to copy recursively a complex folder/files structure to another directory, but I'd like to only include files with txt extension. How would I do that?
FileUtils.cp_r Dir['**/*.txt'], target_dir

Resources