473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

searching for the highest index within a directory

Ada
hello folks,

just wanna get some feedback from someone here who has more experience. :-)

i'm developing a small app that require searching for the highest index
within a directory.

i was thinking of comparing string.
not sure if this is the most efficient way to do it.

here's an example.......
on the FORM, there will be a TEXTBOX with filename.
e.g. testFile
within the directory, there could be several different files:

testFile_1.txt
testFile_2.txt
testFile_3.txt
myTest_54.txt
jay005.jpg
testFile_4.txt
document.doc

in the above sample test, the answer would be "testFile_4.txt".
preferably, the code should ignore other files except those that starts with
"testFile".


Nov 16 '05 #1
5 2324
not sure why you need to do this...

Best way I can think of:
Use DirectoryInfo.GetFiles("testFile*") to get a FileInfo array. I don't
believe that FileInfo inherits the IComparable interface, so I don't think
you can sort using the Array.Sort method. You could copy the filenames out
into a simple string array, sort, and pull off the last entry...

Note that
testFile_4
will come after
testFile_34

so if you want the highest number, you may want to make sure that the
numbers have leading zeros:
testFile_004
will always come before
testFile_034

Care to share why you need this? There may be a better way to do what you
are trying to do.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Ada" <Ad*@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com...
hello folks,

just wanna get some feedback from someone here who has more experience.
:-)

i'm developing a small app that require searching for the highest index
within a directory.

i was thinking of comparing string.
not sure if this is the most efficient way to do it.

here's an example.......
on the FORM, there will be a TEXTBOX with filename.
e.g. testFile
within the directory, there could be several different files:

testFile_1.txt
testFile_2.txt
testFile_3.txt
myTest_54.txt
jay005.jpg
testFile_4.txt
document.doc

in the above sample test, the answer would be "testFile_4.txt".
preferably, the code should ignore other files except those that starts
with
"testFile".

Nov 16 '05 #2
Ada,

You mean something as this?

\\\
DirectoryInfo di = new DirectoryInfo(@"c:\");
FileSystemInfo[] dirs = di.GetFiles();
string[] str = new string[dirs.Length];
for (int i = 0;i < dirs.Length;i++)
{str[i] = dirs[i].Name;}
Array.Sort(str);
MessageBox.Show(str[str.Length-1]);
///

I hope this helps?

Cor
Nov 16 '05 #3
Ada
Cor, thanks for the snippet.
according to your snippet, doesn't it create an array for all the files
inside that directory and not just the "testFile_xx"?
Nick, to answer your question....
it's a small exercise for myself. :-)
an application for this type of naming is a sequential graphic animation
files.
i'm doing this to avoid overwriting the existing file and resume sequence
from the highest index file.

can you elaborate why
testFile_4
comes after
testFile_34?

"Cor Ligthert" wrote:
Ada,

You mean something as this?

\\\
DirectoryInfo di = new DirectoryInfo(@"c:\");
FileSystemInfo[] dirs = di.GetFiles();
string[] str = new string[dirs.Length];
for (int i = 0;i < dirs.Length;i++)
{str[i] = dirs[i].Name;}
Array.Sort(str);
MessageBox.Show(str[str.Length-1]);
///

I hope this helps?

Cor

Nov 16 '05 #4
Ada,

Yes however, Nick showed you already the overloaded constructor with the
(selection)

Cor
Nov 16 '05 #5
Hi Ada,

(I used to write code in the Ada programming language... ;-)

The reason that I asked about your intent for incrementing file names: if
you just needed a unique file name once, that you were going to delete when
you are done, and you didn't want to overwrite another application using
their own unique file name on the same computer, you could have used
Path.GetTempFileName() which will return a unique name in the users
"Temporary Files" folder. Sounds like that isn't what you are doing.

Nick, to answer your question....
it's a small exercise for myself. :-)
an application for this type of naming is a sequential graphic animation
files.
i'm doing this to avoid overwriting the existing file and resume sequence
from the highest index file.

can you elaborate why
testFile_4
comes after
testFile_34?
Because "testFile_34" is a string, not a number. If a string cannot be
converted to a number, there is no way to compare them as numbers. We
compare strings lexically (alphabetical order... kinda... see below). That
means we look at the first character in each string. If one is less than
the other (occurs earlier in the character table), then we stop because we
have found the "earlier" string. If the characters in that position are the
same, we move to the next position and compare.

In this case, the 10th character of testFile_4 is '4' while the 10th
character of testFile_34 is '3'. Since '3' comes before '4' in the
character table, then "testFile_34" comes before "testFile_4". That is why
I suggested that you would want to embed leading zeros in your number
field... In the character table, '0' comes before the rest of the digits, so
your alphabetical sort will be the same as a numeric sort (until you run out
of digits).

Important Note: in the character tables, All of the upper case characters
come before the first lower case character. Therefore, if you use a "case
sensitive sort" (the default), then "ZestFile" will come before "testFile"
even though in a dictionary, it wouldn't work that way. That's because the
capital 'Z' occurs before the lowercase 't'.

If you want to see what the character tables look like, visit
http://www.unicode.org/charts/
Note that we are most familiar with the "Basic Latin" character set.
Another note: read the charts one column at a time (top to bottom, left to
right). I have no idea why they did that :-(.

There is a way around this, of course. You can tell the system to ignore
the case of the letters when sorting your strings.
I've included a snippet of text from an article on DevX.com:

<snippet>
The .NET Framework defines several comparer classes for you to use. One of
them, the CaseInsensitiveComparer class, allows you to sort strings by
ignoring their casing. So in this case, the .NET framework ignores the
String class' CompareTo method, and instead uses the rules defined in the
CaseInsensitiveComparer class. The following code illustrates this feature:

Dim aryLastNames() As String = {"sMiTh, ZULU", "smith, john&", "SMITH,
TerrY"}

Array.Sort(aryLastNames, New CaseInsensitiveComparer)

' aryLastNames order:
'
' smith, john
' SMITH, TerrY
' sMiTh, ZULU
</snippet>You can find the full article at:
http://www.devx.com/dotnet/Article/21089I hope this helps,--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Ada" <Ad*@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another...
22
by: DJ WIce | last post by:
Hi, I'm looking for a script to get the hi-est z-index on a page. I want my javascript menu to be always on top of the page (moves along on top when you scroll down). Does anyone know how to...
9
by: Paul Nations | last post by:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like: Public Class...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
5
by: justobservant | last post by:
When more than one keyword is typed into a search-query, most of the search-results displayed indicate specified keywords scattered throughout an entire website of content i.e., this is shown as...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
1
by: Psapg | last post by:
Hi! I'm new to javasript, and i must confess to have borowed a few free scripts from the net to satisfie my needs.... Still i can't find even an idea of ascipt to do this... Please Help!!! ...
5
by: ryuchi311 | last post by:
In C++ using arrays. I need to create a C Program that will ask for five integers input from the user, then store these in an array. Then I need to find the lowest and highest integers inside that...
10
by: beary | last post by:
Hello all, I've done something a bit stupid and am hoping some kind soul out there can help me out. There's a piece of code that goes through and creates a high number of subdirectories within...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.