that makes a listbox filled with the path and filename of all the .lib files in a folder.
I also have this code:
VB Code:
For Each itm As String In ListBox1.Items
<insert code here>
Next
that will run through the lsitbox and do something with each item.
On to the question.
I was wondering how I, inside the second code, add something to the pathname.
i.e. Change "c:\libraries\1234.lib" to "c:\libraries\ab_1234.lib"
I know I could use
VB Code:
Name "c:\libraries\1234.lib" as "c:\libraries\ab_1234.lib"
but I have to do that with a bunch of different files.
You simply loop through each element in the array and perform a string manipulation procedure which I'm assuming you already know how to do. And once the array has been processed, THEN addrange() it.
Ok, I understand it a bit more but the lines dimensioning new variables are confusing me.
What are the output variables? i.e. What is the renamed file's variable? Is it "Item" or "NewName"?
You've got it in NewName, if you don't rename the item, then the original is untouched. Of course, you might want to have both the old and new, and there are various ways to do this. Because NewName is declared within the For....Next loop, it doesn't have scope outside of that loop (no code can see it outside of that loop). Because of this, you might want to Dim the variables at a higher level of scope, such as in the sub, or even within the form class, or higher (though that's not advisable). It all depends on what other code has to be able to see that variable.
The other issue is how long you want that other name to persist. As it stands, each time through the loop it will be overwritten by the next name. If you need to keep it longer than that, you will need an array of the modified names....but now I'm off on a tangent.
What I'm trying to do is populate three textboxes with different but similar info 1000's of times over. I need to keep the original file but copy the filename and put that in textbox1. Then I need to modify the pathname and put that in textbox2. Finally, I need to modify the extension and put it into textbox3.
Imaging having to do this a thousand times over in commandline and now you can see why I'm making this proggy
However, in your example, you alter the filename, not the pathname.
And while we're on the subject, let me mention that there are some excellent tools for working with file names and path names that might be of more use than playing with the strings as you are doing:
Darn, just realized that I stripped all of that code out, so I don't have a good example for you.
Ok, look up the Path class. In particular, look at Path.GetDirectory, Path.GetFileName, Path.HasExtension, and Path.GetExtension.
In each case, you pass in the path that you have in the listbox, and you can use the first one to fill the first textbox, the second one to fill the second text box, and the fourth one to fill the third text box. Then you don't have to deal with all the substring stuff. Also, there are several other members of Path that might be of some passing interest to you.
Ok, well. It looks like MSDN is going to be fo little use. I get what the path.whatever is used for, but I also need to know how to change the filename/extension.
Ex: -note: Changes from first path are marked in bold
What is in the listbox: u:\files\work\library.lib
What I need for textbox1: u:\files\work\library.lib
What I need for textbox2: u:\converted_files\job\ab_library.lib
What I need for textbox3: u:\converted_files\job\ab_library.log
Assume that what is in the listbox is moved into a variable called st1.
Path.GetFileName(st1) will return Library.lib.
It looks like textbox1 can be st1
It looks like textbox2 could be simply "u:\converted_files\job\ab_" & st1
For textbox3, you could use "u:\converted_files\job\ab_" & st2
where st2 = st1.substring(0,st1.length - st1.IndexOf(".")) & ".log"
I may be off on the second argument of substring, I may have the subtraction off by one. I'd want to test it before I used it.
You can also use simple Strings.Replace if you dont mind using Runtime Functions. Or you can also look into Regex.Replace if Runtime Functions seem to perturb you, although .Substring (that shaggy posted) will probably yield the best performance. Below is an example of using Strings.Replace...
VB Code:
Dim MyString As String = "u:\files\work\library.lib"
Dim Second As String = Strings.Replace(MyString, "files\work\", "converted_files\job\ab_")
Dim Third As String = Strings.Replace(Second, ".lib", ".log")
You don't declare Path as anything. It is a class in the System.IO namespace. You can either refer to it as System.IO.Path or just IO.Path, or else import the System.IO namespace and refer to it as Path. You import a namespace either project-wide in the project properties or else file-specific at the top of the code file.
On first test, I got really weird errors. I looked through the code and found that Path.GetFileName(st1) returns the entire path and filename. Thus breaking the program. Does anyone know what I should use to get the filename alone?
The thing I like most about the members of the Path class are that they are self-documenting. IO.Path.ChangeExtension makes it pretty obvious what's happening, whereas the use of string manipulation is much more generic and it's not obvious at a glance what is happening unless appropriate commenting is used. The only issue I have is that multiple uses of Combine gets a bit long-winded. I'd not use it more than twice in one line of code. Here's the actual implementation of ChangeExtension: