Bouger tous les fichiers d'une listview vers un dossier

Résolu
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012 - 8 mars 2012 à 18:00
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012 - 9 mars 2012 à 14:01
Bonjour à tous,

J'ai un openfiledialog qui ajoute les fichiers sélectionnés dans une listview sous forme disque:\dossier:\nom.ext (SafeFileNames)

Je voudrais déplacer ces fichiers de diverses provenances vers un répertoire C:\TEST en gardant les noms originaux!

J'ai essayé avec Interger et ListView.Items.Count - 1 mais rien n'y fait...

Quelqu'un pourrait il m'aider svp ?

Merci d'avance
Florent

11 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
8 mars 2012 à 19:33
Salut

pourquoi te servir d'un listview
copie la liste des path comme string dans une
list of string et après utilise le File.copy
le bouton button1 pour afficher ton openfiledialog
le bouton2 pour copier les fichiers à leur destination
Veux tu effacer le fichier de sa source ?

Imports System.IO
Private Structure Filedata
    Public thefilename As String
    Public thesafefile As String
End Structure
Private listfilename As New List(Of Filedata)

Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
     OpenFileDialog1.ShowDialog()
End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If e.Cancel = True Then Exit Sub
        Dim thefiledata As New Filedata
        thefiledata.thefilename = OpenFileDialog1.FileName
        thefiledata.thesafefile = OpenFileDialog1.SafeFileName
        If Not listfilename.Contains(thefiledata) Then
            listfilename.Add(thefiledata)
        End If
    End Sub
    Private Sub CopyFile(ByVal destpath As String)
        For Each item In listfilename
            If Directory.Exists(destpath) Then
                File.Copy(item.thefilename, destpath & item.thesafefile)
   'veux tu effacer le fichier ? 
    File.Delete(item.thefilename)            End If
        Next
  listfilename.clear
End Sub
Private Sub Button2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseClick
        CopyFile("C:\TEST")
    End Sub
3
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
9 mars 2012 à 11:51
Salut
ce que tu demandes est un peu différent du code
envoyé

je t'envois un ex avec un listbox mettre la propriété selection mode à multisimple

le bouton button1 te permet d'afficher le filedialogbox
choisi les fichiers press ok
les fichiers choisis vont etre affichés dans
la listbox
tu peux choisir les fichier que tu veux déplacer
cliques sur button2 pour effectuer

voila

Imports System.IO
Private Structure Filedata
     Public thefilename As String
     Public thesafefile As String
     Public flagdel As Boolean
End Structure
Private listfilename As New List(Of Filedata)
Private Sub Button2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseClick
   Dim thefiledata As New Filedata
   Dim iter As Integer
   For Each item In ListBox1.SelectedItems
      For iter = 0 To listfilename.Count - 1
          If item.ToString = listfilename(iter).thefilename Then
              thefiledata.flagdel = True
              thefiledata.thefilename = listfilename(iter).thefilename
              thefiledata.thesafefile = listfilename(iter).thesafefile
              listfilename.Remove(listfilename(iter))
              listfilename.Insert(iter, thefiledata)
           Exit For
       End If
    Next
 Next
        CopyFile("C:\TEST")
    End Sub

    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        OpenFileDialog1.ShowDialog()
    End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If e.Cancel = True Then Exit Sub
        Dim thefiledata As New Filedata
        Dim iter As Integer

        For iter = 0 To OpenFileDialog1.FileNames.Length - 1
            thefiledata.thefilename = OpenFileDialog1.FileNames(iter)
            thefiledata.thesafefile = OpenFileDialog1.SafeFileNames(iter)
            thefiledata.flagdel = False
            If Not listfilename.Contains(thefiledata) Then
                listfilename.Add(thefiledata)
                ListBox1.Items.Add(thefiledata.thefilename)
            End If
        Next
        End Sub


    Private Sub CopyFile(ByVal destpath As String)
        If Directory.Exists(destpath) Then
            For iter = 0 To listfilename.Count - 1
                If listfilename(iter).flagdel Then
                    File.Copy(listfilename(iter).thefilename, destpath & listfilename(iter).thesafefile)
                                      File.Delete(item.thefilename)
                End If
            Next
            For iter = listfilename.Count - 1 To 0 Step -1
                If listfilename(iter).flagdel Then
                    ListBox1.Items.RemoveAt(iter)
                    listfilename.RemoveAt(iter)
                End If
            Next
        End If

    End Sub


pourquoi te sers tu d'un picturebox et non
d'un bouton ?
3
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
8 mars 2012 à 19:55
Merci pour ta réponse complète!

Mlaheureusement lorsque je selectionne plusieurs fichiers il ne m'en déplace qu'un 1
0
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
8 mars 2012 à 19:59
Il suffit juste de désactiver le multiselect en fait!

Merci de ton aide :)
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
8 mars 2012 à 20:38
que signifie déplacer
cut and past
ou
copy and past

Mlaheureusement lorsque je selectionne plusieurs fichiers


remplace la sub OpenFileDialog1_FileOk

par

Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
   If e.Cancel = True Then Exit Sub
    Dim thefiledata As New Filedata
    Dim iter As Integer

        For iter = 0 To OpenFileDialog1.FileNames.Length - 1
            thefiledata.thefilename = OpenFileDialog1.FileNames(iter)
            thefiledata.thesafefile = OpenFileDialog1.SafeFileNames(iter)
            If Not listfilename.Contains(thefiledata) Then
                listfilename.Add(thefiledata)
            End If
        Next
        End Sub
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
8 mars 2012 à 20:42
maintenant ça marche avec multiselect aussi
0
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
8 mars 2012 à 20:57
Un grand merci à toi!
0
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
8 mars 2012 à 23:20
Finalement j'ai gardé la listview pour connaitre les fichiers contenu dans listfilename

Public Class Main

Private Structure Filedata
Public thefilename As String
Public thesafefile As String
End Structure

Private listfilename As New List(Of Filedata)

Private Sub OpenFileDialog_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog.FileOk
If e.Cancel = True Then Exit Sub
Dim thefiledata As New Filedata
Dim iter As Integer
For iter = 0 To OpenFileDialog.FileNames.Length - 1
thefiledata.thefilename = OpenFileDialog.FileNames(iter)
thefiledata.thesafefile = OpenFileDialog.SafeFileNames(iter)
If Not listfilename.Contains(thefiledata) Then
listfilename.Add(thefiledata)
End If
Next
End Sub

Private Sub CopyFile(ByVal destpath As String)
For Each item In listfilename
If Directory.Exists(destpath) Then
File.Copy(item.thefilename, destpath & item.thesafefile)
File.Delete(item.thefilename)
End If
Next
listfilename.Clear()

End Sub

#Region "Move Button"

Private Sub MoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveButton.Click
CopyFile("C:\HMF")
End Sub
#End Region

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
OpenFileDialog.ShowDialog()
For Each fName In OpenFileDialog.FileNames
ListView.Items.Add(fName)
End Sub



Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
For Each lvItem As ListViewItem In ListView.SelectedItems
lvItem.Remove()
Next
End Sub

End Class

Quand j'ajoute un fichier il s'ajoute bien à la listview et à la listfilename...mais lorsque je selectionne un fichier de la listview pour le supprimer je ne trouve pas le moyen de le supprimer la listfilename...

Merci d'avance!

PS: je n'ai mis que le code vb utile ici.
0
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
9 mars 2012 à 12:46
Salut,

Merci pour ton aide!

Et tu sûr pour File.Delete(item.thefilename)

Je n'ai déclaré nul part item!

PictureBox c'est juste par soucis d'esthétisme
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
9 mars 2012 à 13:03
sorry
File.Delete(listfilename(iter).thefilename)


PictureBox c'est juste par soucis d'esthétisme


ah bon tu veux mettre une image ?
0
Larmada Messages postés 16 Date d'inscription vendredi 22 décembre 2000 Statut Membre Dernière intervention 12 juin 2012
9 mars 2012 à 14:01
Problème résolu,
un grand merci à toi!
0
Rejoignez-nous