Listing Files Using a Variable Filter

Listing files using a variable filter

You could use eval to achieve what you want (although I'm not a big fan of it; eval should be avoided whenever possible IMO):

filter="{A1,A2}"
result=$(eval "ls *$filter*.txt")
echo "$result"

There is probably no other way if you insist on using braces as brace expansion is done before any other expansion (e.g. variable expansion). See man bash, sections EXPANSION and Brace Expansion.

However, a possible workaround is given in this answer.

BASH: Filter list of files by return value of another command

You can replace your egrep with the following so you are still inside the find command (-iname is case insensitive and -o represents a logical OR):

find test1 test2 -type f                                       \
\( -iname "*.mpg" -o -iname "*.avi" -o -iname "*.mpeg" \) \
NEXT_BIT

The NEXT_BIT can then -exec bash and exit with status 0 or 1 depending on whether you want the current file included or excluded. So it will look like this:

-exec bash -c 'H=$(mediainfo -output ... "$1"); [ $H -lt 480 ] && exit 1; exit 0' _ {} \;

So, taking note of @tripleee advice in comments about superfluous exit statements, I get this:

find test1 test2 -type f                                       \
\( -iname "*.mpg" -o -iname "*.avi" -o -iname "*.mpeg" \) \
-exec bash -c 'h=$(mediainfo ...options... "$1"); [ $h -lt 480 ]' _ {} \; -print

How to filter directory listing by using a property from the resultant list itself in Java?

I think what you're expecting as an output is:

List<String> dirToProcess = list.stream()
.filter(name -> name.contains(".ok") && list.contains(name.substring(0, name.indexOf(".ok"))))
.map(name -> name.substring(0, name.indexOf(".ok")))
.collect(Collectors.toList());

Get a filtered list of files in a directory

import glob

jpgFilenamesList = glob.glob('145592*.jpg')

See glob in python documenttion

Using Variables with Directories & Filtering

There are a few things I can see that could use some adjustment.

My first thought on this is to reduce the number of places a script has to be edited when changes are needed. I suggest assigning the working directory variable first.

Next, reduce the number of times information is pulled. The Get-ChildItem cmdlet offers an integrated -Filter parameter which is usually more efficient than gathering all the results and filtering afterward. Since we can grab the filtered list right off the bat, the results can be piped directly to the ForEach block without going through the variable assignment and secondary filtering.

Then, make sure to initialize $name inside the loop so it doesn't accidentally cause issues. This is because $name remains set to the last value it matched in the if/elseif statements after the script runs.

Next, make use of the fact that $name is null so that files that don't match your criteria won't be renamed to ".txt".

Finally, perform the rename operation using the $_ automatic variable representing the current object instead of pulling the information with Get-ChildItem again. The curly braces have also been replaced with parenthesis because of the change in the Rename-Item syntax.

Updated script:

$dir = "C:\MYPATH\SAMPLE\"
Set-Location $dir
Get-ChildItem $dir -Filter "e02*" |
Foreach-Object {
$name = $null #initialize name to prevent interference from previous runs
$name = if ($_.BaseName -eq "e024") {"Four"}
elseif ($_.BaseName -eq "e023") {"Three"}
if ($name -ne $null) {
Rename-Item $_ -NewName ($name + ".txt")
}
}

Filter a List of Files by Date String

The problem here is that str_detect is vectorized over pattern or string. Not both at the same time.

some patterns:

filter_mon <- c("20160929", "20160927")

now check str_detect:

library(stringr)
str_detect(file_list, filter_mon)
#output
[1] FALSE FALSE FALSE FALSE TRUE FALSE

just the first pattern is matched in all strings. One TRUE is returned.

One way around that is to run str_detect for each file_dates_mon$file_dates (or as in this example filter_mon) and then Reduce the logical vectors:

file_list[Reduce("|", lapply(filter_mon, function(x) str_detect(file_list, x)))]

#output
[1] "C:\\Users\\uname\\files/DailyOpenOrders_NET_20160927.csv" "C:\\Users\\uname\\files/DailyOpenOrders_NET_20160929.csv"

grepl is also an alternative

file_list[Reduce("|", lapply(filter_mon, function(x) grepl(x, file_list)))]

How to I get all files from a directory with a variable extension of specified length?

I don't believe there's a way you can do this without looping through the files in the directory and its subfolders. The search pattern for GetFiles doesn't support regular expressions, so we can't really use something like [\d]{7} as a filter. I would suggest using Directory.EnumerateFiles and then return the files that match your criteria.

You can use this to enumerate the files:

private static IEnumerable<string> GetProprietaryFiles(string topDirectory)
{
Func<string, bool> filter = f =>
{
string extension = Path.GetExtension(f);
// is 8 characters long including the .
// all remaining characters are digits
return extension.Length == 8 && extension.Skip(1).All(char.IsDigit);
};

// EnumerateFiles allows us to step through the files without
// loading all of the filenames into memory at once.
IEnumerable<string> matchingFiles =
Directory.EnumerateFiles(topDirectory, "*", SearchOption.AllDirectories)
.Where(filter);

// Return each file as the enumerable is iterated
foreach (var file in matchingFiles)
{
yield return file;
}
}

Path.GetExtension includes the . so we check that the number of characters including the . is 8, and that all remaining characters are digits.

Usage:

List<string> fileList = GetProprietaryFiles(someDir).ToList();


Related Topics



Leave a reply



Submit