Hello DaniWeb,

I have a new idea for an app:

I have an external harddrive and I usually backup my desktop with a cleanup folder which I usually move to the external harddrive.

The problem is: when I search for a file (i.e. example.mp3) I will find multiple copies which are taking up way too much space.

Is there an app or some code that can search for an extension (i.e. .mp3)
and if it finds more than one copy of the same file, it can delete the extra file?

The reason I ask is: I have around 1300 different mp3s on my external harddrive and most of them are extra copies of the same mp3.
When I search for the extension mp3 and I want to move all of the mp3s to a single folder I am prompted way too many times for overwriting or keeping the same mp3 file.

I need an app that can force the move without prompting and delete the extra copies; so, I am left with only one copy of each.

i.e.

mp3 files on external hard drive for backup:
song.mp3 > in one folder
song.mp3 > in another folder
song.mp3 > in another folder
song2.mp3 > in one folder
song2.mp3 > in another folder
etc...

I want to consolidate all my mp3s in one folder:
foldertomoveto/ song.mp3
foldertomoveto/song2.mp3

Below is start:

For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
FileIO.SearchOption.SearchAllSubDirectories, "*. & textbox1.text.trim())

'HERE WILL BE AN IF STATEMENT TO VALIDATE IF THERE IS MORE THAN ONE MP3 (ETC...) FILE
'WHICH WILL DELETE THE MULTIPLE COPIES AND KEEP ONLY ONE
'I MIGHT NEED A CHECKEDLIST BOX TO VALIDATE

Dim foundFileInfo As New System.IO.FileInfo(foundFile)
My.Computer.FileSystem.MoveFile(foundFile, "C:\StorageDir\" & foundFileInfo.Name)
Next

The other copies have been deleted.
Any hints?

M1234ike

‘Hello Daniweb,
I am having some trouble determining how to use a checkedlistbox, add all files from the subdirectories with a common extension, and then check one of each similar files.
i.e.
checkedlistbox contains:
C:/song.mp3 <<<<this one not selected
C:/subfolderone/song.mp3
C:/subfoldertwo/song.mp3
C:/song2.mp3 <<<<this one not selected
C:/subfolderone/song2.mp3
(the rest of the files are selected)

I want to select all of each except for one or vise versa (I would prefer all but one file).

Below is the code that uses: 2 textboxes, 3 buttons, 2 radiobuttons, and a checkedlistbox

******I commented where I need to add the code that selects all of the same file but one**********
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox1.Text.Trim() = "" Or TextBox1.Text.Trim.Contains(".") Or TextBox1.Text.Trim.Contains("/") Or TextBox1.Text.Trim.Contains("\") Then
Else
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.CurrentDirectory, _
FileIO.SearchOption.SearchAllSubDirectories, "*." & TextBox1.Text.Trim())

If Not CheckedListBox1.Items.Contains(foundFile) Then
CheckedListBox1.Sorted = True

End If

‘Here I want to add some code to either check one of each items in the ‘checkbox
‘The way I want to do this is by using the ‘System.IO.Path.GetFileName(foundfile) as a searching pattern but I don’t ‘know how to search each match
‘________________________________________________________________

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Enabled = True

If TextBox2.Text.Trim() = "" Or TextBox2.Text.Trim.Contains(".") Or TextBox2.Text.Trim.Contains("/") Or TextBox2.Text.Trim.Contains("\") Then
Else
Dim myPath As String = IO.Path.Combine(My.Computer.FileSystem.CurrentDirectory, TextBox2.Text.Trim())
IO.Directory.CreateDirectory(myPath)
Dim count As Integer = 1

If Not IO.Directory.Exists(myPath) Then IO.Directory.CreateDirectory(myPath)

For Each foundFile As String In CheckedListBox1.CheckedItems

My.Computer.FileSystem.MoveFile(foundFile, myPath & "/" & IO.Path.GetFileName(foundFile))

Next
End If

End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each foundFile As String In CheckedListBox1.CheckedItems

My.Computer.FileSystem.DeleteFile(foundFile)

Next
CheckedListBox1.Items.Clear()

End If
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
TextBox2.Enabled = True
TextBox2.Visible = True
TextBox2.Enabled = False
TextBox2.Visible = False
End If
End Sub
End Class

‘ANY HINTS???
M1234ike

See if this helps.
1.Button andAlso you will need to set your "Original" Folder.Path.

Imports System.IO
Public Class Form1
Private myDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\"
Private myOriginalFilesFolder As String = myDesktopFolder & "vb.samples - Copy\", _
myDestinationFilesFolder As String = myDesktopFolder & "TEMP\"

Private Sub moveFiles(ByVal selOriginalLocationFolder As String, ByVal selDestinationFolder As String)
If Directory.Exists(selOriginalLocationFolder) Then
If Not Directory.Exists(selDestinationFolder) Then Directory.CreateDirectory(selDestinationFolder)
Dim iMoved As Integer = 0, iDeleted As Integer = 0
With My.Computer.FileSystem
For Each foundFile As String In .GetFiles(myOriginalFilesFolder, FileIO.SearchOption.SearchAllSubDirectories, "*")
'// check if File already.Exists in Destination.Folder
If Not File.Exists(selDestinationFolder & Path.GetFileName(foundFile)) Then
.MoveFile(foundFile, selDestinationFolder & Path.GetFileName(foundFile))
iMoved += 1
Else '// If it Exists in Destination.Folder, .Delete from Original.Folder.
.DeleteFile(foundFile, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
iDeleted += 1
End If
Next
End With
MsgBox("Files.Moved: " & iMoved & vbNewLine & "Files.Deleted: " & iDeleted, MsgBoxStyle.Information)
End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
moveFiles(myOriginalFilesFolder, myDestinationFilesFolder)
End Sub
End Class

Thanks Codeorder,

I have most of the code together with: 2 buttons (1 to get the files by the extension and the other for moving copying or deleting) , 2 textboxes (1 for the extension and the other for the path), 3 radiobuttons (move copy or delete), a checkedlistbox (to list the files), and a combobox (to select rename or overwrite).

With the combobox, the user can select to rename the files if the files in the destination path are the same name (getfilename) as in the source path or if there is more than one of the same file name (getfilename) in the checkedlistbox.

The only problem I have now is renaming: Do you or does anybody know a Windows-Like algorithm to "rename" similar files pf similar file names being moved or copied into the new directory?

i.e. In my case the user will initially select to rename all the similar files via selecting "rename" from the combobox, but: in Windows 7 the user is prompted with something like: "Keep both files". If the user selects this option the next similar named file will have an incremented number appended to the file name:

newfolder/song.mp3
newfolder/song(1).mp3
newfolder/song(2).mp3

In my case: if the user selects "rename" then no files will be overwritten.
The reason for this is if the user names two files the same name but the contents are different.

What I have so far is something like:

If My.,Computer.fileexists(foundfile)
For
‘some kind of for loop like
for each found2 as string in checkedlistbox1.checkeditems
if system.io.path.getfilename(found2) = system.io.path.getfilename(foundfile) and then etc…
dim s as string = “”
dim i as integer = 1
‘for loop of some sort etc..

S = (I + 1).tostring()
i = i + 1

next

My.computer.filesystem.movefile(foundfile, mypath & “\” & system.io.path.getfilenamewithoutextension(foundfile) & “(“ & s & “)” & system.io.path.getfilextension(foundfile))

I think the movefile naming convention above is correct but I need the right algorithm to name each similar file listed in the checkedlistbox1.checkeditems that are being copied or moved to a similar folder and renamed.

I WANT TO USE A CHECKEDLISTBOX BUT: the problem is when you sort the files they sort via path and not file name (getfilename). So, the index is also not sorted.

Any hints?

M1234ike

See if this helps.
New.project, 1.Button, 1.CheckedListBox

Imports System.IO
Public Class Form1
Private arlFiles As New ArrayList
Private arDummyFiles() As String = {"c:\blah.txt", "a:\blah again.txt", "d:\another blah.txt", "b:\end of blah.txt"}
Private mySplitter As String = "|", sT As String '// TEMP.String.

Private Sub loadFilesToArrayList(ByVal selArrayList As ArrayList)
For Each itm As String In arDummyFiles
selArrayList.Add(Path.GetFileName(itm) & mySplitter & Path.GetDirectoryName(itm)) '// dis.align coordinates. xD
Next
selArrayList.Sort()
' MsgBox(selArrayList(0)) '// FOR TESTING.
End Sub
Private Sub loadFilesToCheckedListBox(ByVal selCheckedListBox As CheckedListBox)
With selCheckedListBox.Items
.Clear()
For Each itm As String In arlFiles
Next
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End Sub

Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
With CheckedListBox1
If Not .SelectedIndex = -1 Then
sT = arlFiles(.SelectedIndex)
sT = sT.Substring(sT.IndexOf(mySplitter) + 1) & sT.Substring(0, sT.IndexOf(mySplitter)) '// re.align coordinates. xD
MsgBox(sT)
End If
End With
End Sub
End Class

Thanx,

I'll try that out in my program.

-M1234ike

Hello Codeorder,

How can I assign the following to your "Private arDummyFiles() As String" array?

For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.CurrentDirectory, _
FileIO.SearchOption.SearchAllSubDirectories, "*." & TextBox1.Text.Trim())

I want to use a textbox to specify a certain pattern: i.e. ".*txt" files.
Then I want to either move, copy, or delete the files.
If the files file name (system.io.path.getfilename(foundfile)) is the same in the destination path and in the source path or if the there are more than one of the same file names on the list (getfilename) or both:
then they must be renamed by incrementing the end of the file:
i.e.

C:/Desktop/folderone/song.mp3
C:/Desktop/folder/song.mp3

After the move:

C:/Desktop/newfolder/song.mp3
C:/Desktop/newfolder/song(1).mp3

Any hints?

M1234ike

>>How can I assign the following to your "Private arDummyFiles() As String" array?
Instead of looping thru the Dummy.Array in loadFilesToArrayLis , loop thru your For/Next loop that locates the Files and add the founFile to the ArrayList.

Regarding If File.Exists Then .Rename, I'll get back to you in a little while.:)

Took a lot less.time to do than expected, even took a sh.t in the .Process.Start(xD).

Private Sub moveFiles(ByVal selOriginalLocationFolder As String, ByVal selDestinationFolder As String)
If Directory.Exists(selOriginalLocationFolder) Then
If Not Directory.Exists(selDestinationFolder) Then Directory.CreateDirectory(selDestinationFolder)
Dim iMoved As Integer = 0, iRenamed As Integer = 0
'///////////////////////////////////////////////////////////////////////
Dim iFileCopyNumber As Integer = 1 '// ADDED THIS.
'///////////////////////////////////////////////////////////////////////
With My.Computer.FileSystem
For Each foundFile As String In .GetFiles(myOriginalFilesFolder, FileIO.SearchOption.SearchAllSubDirectories, "*")
'// check if File already.Exists in Destination.Folder
If Not File.Exists(selDestinationFolder & Path.GetFileName(foundFile)) Then
.MoveFile(foundFile, selDestinationFolder & Path.GetFileName(foundFile))
iMoved += 1
Else '// If it Exists in Destination.Folder, .Rename.File
'///////////////////////////////////////////////////////////////////////
Do Until Not File.Exists(selDestinationFolder & Path.GetFileNameWithoutExtension(foundFile) & " - Copy (" & iFileCopyNumber.ToString & ")" & Path.GetExtension(foundFile))
iFileCopyNumber += 1
Loop
.MoveFile(foundFile, selDestinationFolder & Path.GetFileNameWithoutExtension(foundFile) & " - Copy (" & iFileCopyNumber.ToString & ")" & Path.GetExtension(foundFile))
iRenamed += 1
'///////////////////////////////////////////////////////////////////////
End If
Next
End With
MsgBox("Files.Moved: " & iMoved & vbNewLine & "Files.Renamed: " & iRenamed, MsgBoxStyle.Information)
End If
End Sub

Thanx Codeorder,

I'll give it a try.

-M1234ike

Hello Codeorder and Daniweb,

The project is almost complete the moved files can be renamed, but while debugging, when I select the "OVERWRITE" option to overwrite similar existing files a permissions error pops up. The message says that I do not have sufficient permissions.

i.e. C:/Cleanup/Music/rednation.mp3
...Desktop/rednation.mp3
The code for overwriting the files looks like this:

My.Computer.FileSystem.MoveFile(foundFile, myPathfour & "/" & IO.Path.GetFileName(foundFile), True)

I tried modifying the app.manifest by changing the line:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to:

and it did not work.

I also tried changing the ClickOnce security settings but no dice.

Any hints?

M1234ike

Hello CodeOrder and Daniweb,

Thanx for your help. I can now successfully move and rename the files from my Desktop to my Cleanup folder.

However, I cannot overwrite certain files in the Cleanup folder even when the files are not in use.

When I try to move certain files from my Desktop to my Cleanup folder but an error message shows up while debugging saying: that I do not sufficient permissions.

Here is the modified code with a Try/Catch:

For Each foundFile As String In My.Computer.FileSystem.GetFiles _
(My.Computer.FileSystem.SpecialDirectories.Desktop, _
FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")
Try
If My.Computer.FileSystem.FileExists(foundFile) Then

My.Computer.FileSystem.MoveFile(foundFile, myPathfour & "/" & IO.Path.GetFileName(foundFile), True)

End If
Catch ex As Exception
MsgBox("Error: " & vbNewLine & "The found file: " & IO.Path.GetFileName(foundFile) & vbNewLine & "cannot overwrite its matching file in the Cleanup folder. " & vbNewLine & "You do not have sufficient permissions to overwrite the file. " & vbNewLine & "Please close the file and try again OR " & vbNewLine & "you can select the RENAME option and rename the file.")
End Try

Next

'___________________________________________________________

I want to able to override the permission problems and simply overwrite the file in the Cleanup folder.

I tried to modify the app.manifest by changing the requested Permissions code to:

but it returns a ClickOnce error.

Any hints?

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />