473,888 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2350
not sure why you need to do this...

Best way I can think of:
Use DirectoryInfo.G etFiles("testFi le*") 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*@discussion s.microsoft.com > wrote in message
news:97******** *************** ***********@mic rosoft.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_x x"?
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.GetTempFil eName() 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_3 4" 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_3 4" 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 CaseInsensitive Comparer 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
CaseInsensitive Comparer class. The following code illustrates this feature:

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

Array.Sort(aryL astNames, New CaseInsensitive Comparer)

' 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*@discussion s.microsoft.com > wrote in message
news:F9******** *************** ***********@mic rosoft.com...

Nov 16 '05 #6

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

Similar topics

4
17993
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 example is if i have a login_history table, and i'm logged in, but i want to display the date of my previous login. if the login_history table has user_id and login_date which makes up the primary key, how can i get the most recent login (minus the...
22
10373
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 scann all items/elements on the page for a z-index? Thanks, Wouter
9
2881
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 DegreeMaintenance Private arrCipCodes As New ArrayList 'populate reader with data With rdr Do While .Read arrCipCodes.Add(New CipCode(.GetString(0), .GetString(1)))
7
3361
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
2411
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 three bolded periods '...' in search-result listings. Additionally, most content is outdated; as many users need up-to-date content. Hence, filtering-through search-results becomes quite cumbersome. The newsgroup listings allow detailed...
29
2678
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 someone set me on the right path to what modules and calls to use to do that? You'd think that it would be a fairly simple proposition, but I can't find examples anywhere. Thanks.
1
1384
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!!! I have a webserver setup in my home, with an index file that is an access comtrol page, that, if the authentication is sucessfull, takes the user to a
5
3799
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 array and add them together. The output will be the result in this format: LOWEST+HIGHEST=SUM #include<stdio.h> void main (){ int r, low, hig, ans, index; for(index=0, index<4, index++){ printf("Enter number");
10
2645
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 subdirectories, which is fine. Initially I forgot to create an index.html file in each directory to prevent viewing of its folders and files without people being logged in. I've fixed it now, but there are a whole heap of directories without this...
0
9802
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11186
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.