Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with copy/move files on windows

Status
Not open for further replies.

minus0

Programmer
Feb 20, 2003
73
US
Hello all,

I have a bunch of files that I am trying to organize
in a better way on my PC at home.

Filename for example is "Name1 - Name2 -
FileName.doc". I am looping through all the files in a
particular folder (named Misc) and use split funciton
to get "Name1", "Name2" and "FileName.doc". In case I
already have a folder called Name1 under c:\ then I
just move the file "FileName.doc" to this folder else,
I create a folder and then move the file.
I am pretty much done with all the code but when I run
my code, I am getting an exception when I try to do a
File.Copy(sourceFile,destinationFile) as "File already
exists". Now I know, there is a flag with File.Copy
that would let me overwrite but I am not trying to
overwrite any files but trying to copy a file in to a
folder so I shouldnt be getting this exception.
Code:
Try
            Dim i As Integer
            Dim j As Integer
            Dim TargetDir As String
            Dim strFolder1 As String
            Dim strFolder2 As String
            Dim strFileName As String
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TargetDir = FolderBrowserDialog1.SelectedPath
            End If
            Dim di As New DirectoryInfo(TargetDir)
            Dim arrDirectoryNames() As DirectoryInfo
            arrDirectoryNames = di.GetDirectories

            'Name1 - Name2 - FileName.doc
            ' the response of split() -> 
            'split(1) would be Name1
            'split(2) would be Name2 if exists
            'split(3) would be the name of the file.

            Dim fi As FileInfo()
            fi = di.GetFiles

            'Dim path As String = Directory.GetCurrentDirectory
            'MsgBox(path)
            Dim fiTemp As FileInfo
            For Each fiTemp In fi
                Dim SplitFiles() As String = Split(fiTemp.Name, "-")
                For i = 0 To SplitFiles.Length - 1
                    ListBox1.Items.Add(SplitFiles(i))
                    ' if length is 2 then I got Name1 and FileName
                    If SplitFiles.Length = 2 Then
                        strFolder1 = SplitFiles(0).ToString.Trim()
                        strFileName = SplitFiles(1).ToString.Trim()
                        For j = 0 To arrDirectoryNames.Length - 1
                            If (StrComp(strFolder1, arrDirectoryNames(j).ToString.Trim())) = 0 Then
                                ' directory by the folder name already exists 
                                File.Copy(fiTemp.FullName, strFolder1)
                                'File.Move(fiTemp.FullName, strFolder1)
                            End If
                        Next
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

Appreciate your suggestions.
-0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top