I have a series of files that have various file paths and filenames in, all the file names have the same extension but the directory names or paths are all different and have set about loading the files into a Tstringlist and I am trying to sort them into filename order even though they have the paths as well.
Here is an example of the strings in the Tstringlist:-
c:\directory 1\AboutUs.lnk
c:\directory something\AAHelp.lnk
c:\directory anything\AAATalk.lnk
When sorted by the filename part of the string I would like to end up with.
c:\directory anything\AAATalk.lnk
c:\directory something\AAHelp.lnk
c:\directory 1\AboutUs.lnk
In other words I would like to be able to sort the strings with path by the filename part of the string.
Any help would be appreciated!.
Use TStringList.CustomSort():
function Compare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := CompareStr(
LowerCase(ExtractFileName(List[Index1])),
LowerCase(ExtractFileName(List[Index2]))
);
end;
// Then, just call:
YourStrList.CustomSort(Compare);
Related
everyone. So, what I basically want to do is to search for all files that start with "dm" or end with ".tmp" in storage_path("app/public/session").
I already tried File::allFiles() and File::files() but what I get is all files that are into that session folder and I can't figure out how to do it. What I could find in here is questions on how to empty a folder but that's not what I am looking for. Thanks.
Try this code :
$files = File::allFiles(storage_path("app/public/session"));
$files = array_filter($files, function ($file) {
return (strpos($file->getFilename(), 'dm') === 0) || (substr($file->getFilename(), -4) === '.tmp');
});
Or you can use the glob function like this :
$files = array_merge(
glob(storage_path("app/public/session/dm*")),
glob(storage_path("app/public/session/*.tmp"))
);
In Laravel, you can use the File facade's glob() method to search for files that match a certain pattern. The glob() function searches for all the pathnames matching a specified pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.
You can use the glob() method to search for files that start with "dm" or end with ".tmp" in the "app/public/session" directory like this:
use Illuminate\Support\Facades\File;
$storagePath = storage_path("app/public/session");
// Find files that start with "dm"
$files = File::glob("$storagePath/dm*");
// Find files that end with ".tmp"
$files = File::glob("$storagePath/*.tmp");
You can also use the ? and [] wildcard characters,
for example ? matches one any single character and [] matches one character out of the set of characters between the square brackets,
to search for files that match more specific patterns, like this:
// Find files that starts with "dm" and ends with ".tmp"
$files = File::glob("$storagePath/dm*.tmp");
Note that, File::glob() method return array of matched path, you can loop and see the files or use it according to your needs.
I am trying to write something to a file but pascal adds a bunch of whitespaces between each record's value and also puts 2 records on one line.
The first file is just a list of strings.
The second file (the one I create through the program) should have title and description.
How can I get rid of the whitespaces pascal is adding?
program Wiki;
{$mode objfpc}
TYPE wiki=record
title:string;
description:string;
end;
var
f:text ;
g:file of wiki ;
row:wiki;
fileName: string;
oldFileName:string;
begin
writeln('Old file name:');
readln(oldFileName);
ASSIGN(f,oldFileName);
RESET(f);
writeln('New file name:');
readln(fileName);
ASSIGN(g,fileName);
REWRITE(g);
REPEAT
Readln(f,row.title);
writeln('give a description:');
Writeln(row.title);
Readln(row.description);
Write(g,row)
until EOF(f);
CLOSE(f);
CLOSE(g);
writeln;
writeln('press enter to close.');
readln();
end.
In objfpc mode without {$H+}, I guess that row.description is a fixed size Turbo Pascal style ShortString. It is 255 characters long, and that is probably why you get all that whitespace.
Rather write the output file as a text file:
var
f: Text;
g: Text;
and:
Writeln(g, row.title, ';', row.description);
That should produce text output like:
Finding Nemo;The adventures of two fish trying to find the lost son of one of them
Toy Story;The adventures of a merry bunch of toys
etc.
I have a set of images located in a folder and I'm trying to read these images and store their names in text file. Where the order of images is very important.
My code as follow:
imagefiles = dir('*jpg');
nfiles = length(imagefiles); % Number of files found
%*******************
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
The images stored in the folder in the following sequence : {1,2,3,4,100,110}.
The problem that Matlab read and write the sequence of images as { 1,100,110,2,3,4}. Which is not the correct order.
How can this be overcome?
I would suggest to use scanf to find the number of the file. For that you have to create a format spec which shows how your file name is built. If it is a number, followed by .jpg, that would be: '%d.jpg'.
You can call sscanf (scan string) on the name's of the files using cellfun:
imagefiles = dir('*jpg');
fileNo = cellfun(#(x)sscanf(x,'%d.jpg'),{imagefiles(:).name});
Then you sort fileNo, save the indexes of the sorted array and go through these indexes in the for-loop:
[~,ind] = sort(fileNo);
for ii=ind
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
I have a list of xml files in a directory for example:
file1.xml
file2.xml
file3.xml
file4.xml
file5.xml
file10.xml
file11.xml
file12.xml
file22.xml
file23.xml
file24.xml
file31.xml
file32.xml
file33.xml
When I use os.listdir(path) and print the file names, the output is as follows:
file1.xml
file10.xml
file11.xml
file12.xml
file2.xml
file22.xml
file23.xml
file24.xml
file3.xml
file31.xml
file32.xml
file33.xml
file4.xml
file5.xml
Expected Outptut
file1.xml
file2.xml
file3.xml
file4.xml
file5.xml
file10.xml
file11.xml
file12.xml
file22.xml
file23.xml
file24.xml
file31.xml
file32.xml
file33.xml
Can any1 tell me if there is a solution for this problem. Thanks in advance!!
As per the docs for os.listdir (emphasis mine):
os.listdir(path)
Return a list containing the names of the entries in the directory
given by path. The list is in arbitrary order. It does not include the
special entries '.' and '..' even if they are present in the
directory.
Given your example, perhaps the easiest way is to sort the list by extracting the numbers from the name, and sorting by those:
import os, re
filenames = os.listdir('/path/to/your/files')
filenames.sort(key=lambda L: map(int, re.findall('\d+', L)))
I am writing a function for remote sensing purposes using matlab
the user will enter a folder containing 7 files into the program each file is a band of an image and the names of them is:
"b1.dat"
"b2.dat"
"b3.dat"
"b4.dat"
"b5.dat"
"b6.dat"
"b7.dat"
for example if 2 is entered as the argument of the function it will search in seven file names that are in the access and then will show b2.dat
how do you suggest me to write the code
You can use uigetfiles to select the directory and dir to get a list of the folders contents. Once you have the list, strfind will tell you a file contains a given number.
Or, using uigetdir:
dirName = uigetdir('C:\', 'select a directory');
contents = dir(dirName);
for c = contents
name = c.name;
if strfind(name,'3')
fileToOpen = name{1};
end
end
I used these two lines of codes:
folder = uigetdir('D:\','Select the folder containing bands')
filenames = dir(folder)
the first line returns the path to the folder as I expected:
folder =
D:\RS\911130 TM bands
but the second line not. I have 7 files in my folder and it returns a 9x1 struct
filenames =
9x1 struct array with fields:
name
date
bytes
isdir
datenum
for example the contents of the filenames(1,1) is: