Started By
Message

Any VB.net gurus on this board?

Posted on 2/28/17 at 10:33 am
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 10:33 am
I've got a question that I hope someone can answer. Anyone up for it?
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 11:01 am to
I know a thing or two. Whatcha got?
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 1:14 pm to
Okay, here it goes


CheckBoxList1 (populated by a folder browser dialog)

AB01AA.dwg
BA01AA.dwg
CD01AA.dwg
EA01AA.dwg
FL01AA.dwg

CheckBoxList 2 (popluated by a separate folder browser dialog)

BASE ANGLE DETAIL BA01AA.dwg
CRANE TIE BACK DETAIL TOP RUNNING CRANE CD01AA.dwg
GUTTER CLIP EA01AA.dwg
POST HUNG EAVE FLASHING FL01AA.dwg
RIGID FRAME ANCHOR BOLT SETTING AB01AA.dwg

what i'm trying to accomplish is copy the selected/checked items from checkboxlist1 into another folder(selected from a third folder browser dialog) and rename them with the names from the checkboxlist2

i have the copy method working. i just have to include the renaming portion.

here's the code i'm using to copy...

quote:

For Each file As IO.FileInfo In ListBox1.CheckedItems
IO.File.Copy(file.FullName, IO.Path.Combine(TextBox3.Text, file.Name))
Next




eta: sorry for the late response. i didn't check on this thread for replies until after i got back from lunch.
This post was edited on 2/28/17 at 1:15 pm
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 1:32 pm to
Hmm...

How do you know which file name from list 1 corresponds to which file name from list 2?

Is the order guaranteed to match up?

(e.g. AB01AA.dwg -> BASE ANGLE DETAIL BA01AA.dwg)
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 1:52 pm to
quote:

How do you know which file name from list 1 corresponds to which file name from list 2?


that's what i'm trying to figure out. i don't know if the sorting of the list has anything to do with it or not. as of right now, the list just sorts in an alpha-numeric order.

what i want the program to do is change "AB01AA.dwg" to "RIGID FRAME ANCHOR BOLT SETTING AB01AA.dwg" and so on..
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 2:28 pm to
Ohh... I didn't notice the bit on the end of the file names in list 2 match the names in list 1.

I don't think there is a one-liner to do that unles you can use linq, but this should do it.

For Each file As IO.FileInfo In ListBox1.CheckedItems
....Dim NewFileName As String = FindListBox2ItemThatContains(file.name)
....If NewFileName.Trim.Length > 0 Then _
........IO.File.Copy(file.FullName, IO.Path.Combine(TextBox3.Text, NewFileName.Trim))
Next


Function FindListBox2ItemThatContains(ByVal listBox1FileName As String) As String
....For i As Integer = 0 To ListBox2.Items.Count - 1
........If ListBox2.Items(i).Text.Trim.Contains(listBox1FileName) Then
............Return ListBox2.Items(i).Text
........End If
....Next
....Return ""
End Function


I haven't tested this or anything, but it should get you on the right track.
This post was edited on 2/28/17 at 2:29 pm
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 3:01 pm to
i've got it pasted and the only issues it's have thus far is the "NewFileName" after the "Dim". it's saying, "Variable 'NewFilename' hides a variable in an enclosing block."

what's that mean?
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 3:02 pm to
nevermind, i had that same variable defined elsewhere. i got it fixed. now on to testing.
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 3:08 pm to


You can call that whatever you want if there's a conflict elsewhere.
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 3:39 pm to
this is what i got and i have no clue what it means

Posted by mctiger1985
Baton Rouge
Member since Oct 2009
3693 posts
Posted on 2/28/17 at 4:38 pm to
Do you need to use .name instead of .text?
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 4:51 pm to
holy shite, that worked. but it only did 4 of the 5 files.

what could cause that?
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 5:01 pm to
Whoops. Didn't notice you were looping over FileInfo types. Good catch, mctiger1985.

The only reason I could see it skipping a file is if there are some spaces or something that are throwing off the Contains() function.

Maybe try putting .Trim on the end of listBox1FileName and ListBox2.Items(i).Text?


Like this:

........If ListBox2.Items(i).Text.Trim.Contains(listBox1FileName.Trim) Then
............Return ListBox2.Items(i).Text.Trim
........End If
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 5:09 pm to
that didn't work either
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 2/28/17 at 5:18 pm to
okay, i caught it

the file on the left has a .DWG extension
the file on the right has a .dwg extension
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 2/28/17 at 9:41 pm to


It's always something stupid like that. Honestly, if case doesn't matter, I should've included a .ToUpper (or .ToLower) when trying to see if the filename contains a particular string.

Usually pretty standard, but I was going off the top of my head. So much of that stuff just becomes automatic the more you do it.

Glad you got it going.
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 3/1/17 at 8:46 am to
where does the .ToUpper/.ToLower go? i tried putting it where you suggested and i get the red squiggly lines.
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 3/1/17 at 8:53 am to
This line:

quote:

........If ListBox2.Items(i).Text.Trim.Contains(listBox1FileName) Then


should be

quote:

........If ListBox2.Items(i).Text.Trim.ToUpper.Contains(listBox1FileName.ToUpper) Then



This makes the string comparisons non-case-sensitive.
Posted by finchmeister08
Member since Mar 2011
35624 posts
Posted on 3/1/17 at 1:36 pm to
that worked

Thanks for the help man. Just so I don't feel like you did the work for me(because you basically did), can you break down the code and walk me through what each portion does and what it means? If you have the time of course. I'm kinda learning on the fly.
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 3/2/17 at 2:24 pm to
Loop over every filename that is checked in ListBox1
For Each file As IO.FileInfo In ListBox1.CheckedItems

Use the function FindListBox2ItemThatContains (see below) to find the item in ListBox2 that contains the text of the current file
....Dim NewFileName As String = FindListBox2ItemThatContains(file.name)

If there is an item in ListBox2 corresponding to the current file in ListBox1
....If NewFileName.Trim.Length > 0 Then _

Make a copy of the source filepath (file.Fullname) and save it in the new directory (TextBox3.Text) with the filename from ListBox2 (NewFileName.Trim)
........IO.File.Copy(file.FullName, IO.Path.Combine(TextBox3.Text, NewFileName.Trim))

We're done with this file in ListBox1. Move on to the next one, if there are any left
Next


This Function will search through ListBox2 and Return the Filename that contains the text in listBox1FileName
Function FindListBox2ItemThatContains(ByVal listBox1FileName As String) As String

Loop over all items in ListBox2

....For i As Integer = 0 To ListBox2.Items.Count - 1

If the current item (at index i) contains the text in listBox1FileName
........If ListBox2.Items(i).Text.Trim.Contains(listBox1FileName) Then _

We've found what we're looking for. Exit the function and return that filename
............Return ListBox2.Items(i).Text

The current item (at index i) didn't contain the text in listBox1FileName, move on to the next item in ListBox2
....Next


We've run out of items in ListBox2 to check. There aren't any that contain the text in listBox1FileName. So, we return a blank string. (Have to return something)
....Return ""
End Function
first pageprev pagePage 1 of 2Next pagelast page

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram