codice vba per modificare il nome del file

Anonimo
2016-01-16T13:24:08+00:00

salve a tutti

in una maschera carico, attraverso un cmdFile, i file provenienti da qualsiasi directory e popolo due txtbox di nome txtFile e txtPath

Da un altro comando cmdcopyfile3 "riesco" a copiare il file in un percorso predefinito e creare contemporaneamente una cartella, in base al valore riportato nella txtCategoria e una sottocartella in base al valore txtSottocategoria.

Ho inserito una nuova cmdRinominaFIle, da dover utilizzare in caso di voler rinominare il file presente nel txtFile, e al click il setfocus va su txtFile da modificare e successivamente su pulsante cmdCopy inserisci il file rinominato

Per fare tuttociò occorre inserire una nuova txtNuovoNomeFile per gestire meglio?

In pratica vorrei far si che se il nome del file caricato è corretto di procedere o in alternativa modificarlo

Il codice attuale:

Private Sub cmdCopyFiles3_Click()

'Const myPath  As String = "B:\Documenti" ' <----- personalizza il path!!

If Not folderExists(myPath) Then MkDir myPath

If Not FileExists(Me.txtFile) Then VBA.MsgBox "il file non esiste", vbCritical, "warning": Exit Sub

Dim strFile     As String

Dim i           As Integer

Dim strFileName As String

strFile = justFileName(Me.txtFile)

If Not folderExists(myPath & Me.txtCategoria) Then

    MkDir myPath & Me.txtCategoria

End If

If Not folderExists(myPath & Me.txtCategoria & "" & Me.txtSottocategoria) Then

    MkDir myPath & Me.txtCategoria & "" & Me.txtSottocategoria

End If

strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & strFile

i = 1

Do While Len(Dir(strFileName)) > 0

    strFileName = "copia di (" & i & ") " & strFile

    i = i + 1

    strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & strFileName

Loop

strFile = strFileName

FileCopy Me.txtFile, strFile

If FileExists(strFile) Then VBA.MsgBox "file copiato con successo", vbInformation, "Info"

End Sub

Grazie a Voi tutti.

Microsoft 365 e Office | Access | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2016-02-03T06:53:51+00:00

ciao NebbiaDB,

per la formattazione della data prova a studiare la funzione nativa format$.

Ragiona su questo...

se vuoi anteporre ai files che carichi la data o qualsiasi altra cosa e fare in modo che il file acquisisca anche la data nel suo nome, il file deve essere comunque rinominato, e per comunque intendo dire SEMPRE.

quindi qui :

[....]

 Ad esempio dopo caricato il file e rinominato oppure no,

[...]

Grazie ancora infinite Sandro

Oppure no...mmm....ci sta poco...

Se si tratta, ma non credo, di inserire all'interno del nome anche la data SOLO nel tuo db con format$ risolvi.

Se invece come penso vuoi che anche il file fisico salvato su disco assuma questa modifica, devo procedere con la rinomina sistematica.

[...]

Grazie ancora infinite Sandro

[...]

ciao, Sandro

ps. apri un altro 3d qui sfioriamo il teraByte ormai ed è diventato un sguazzabuglio di issues....

La risposta è stata utile?

0 commenti Nessun commento

43 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2016-01-20T21:45:49+00:00

    ciao NebbiaDb,

    [...]

     - mi rinomina il file ma ci manca il punto tra il Nome File Nuovo e l'estensione e pertanto in fase di copia del file risulta inesistente.

    in pratica rinomina il file ad esempio sandrotxt al posto di sandro.txt

    [...]

    eh si, ha ragione lui come sempre...avevo apportato una modifica che ho scordato di farti fare...

    questa :

    Public Function justExtentionFileName(ByVal strFullPath As String) As String

    justExtentionFileName = Mid$(strFullPath, InStrRev(strFullPath, "."), Len(strFullPath))

    End Function

    sostituisce questa:

    Public Function justExtentionFileName(ByVal strFullPath As String) As String

    justExtentionFileName = Mid$(strFullPath, InStrRev(strFullPath, ".") + 1, Len(strFullPath))

    End Function

    quel +1 in più toglie il punto!

    la modifica è migliorativa altrimenti nella rinomina del file ti tocca concatenare il punto e poi l'estensione, il codice diventa meno leggibile, così lo è di più :-)

    Ciao, Sandro.

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-01-20T21:12:00+00:00

    Ciao caro Sandro

    carico file e voglio rinominare il file

    • mi rinomina il file ma ci manca il punto tra il Nome File Nuovo e l'estensione e pertanto in fase di copia del file risulta inesistente.

    in pratica rinomina il file ad esempio sandrotxt al posto di sandro.txt

    Grazie ancora

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2016-01-20T17:52:21+00:00

    ciao NebbiaDb,

    apporta le seguenti modifiche per rinominare il file con l'estensione e visualizzare il nome del file copiato nella nuova struttura delle cartelle con relativo messaggio di avviso:

    modifica il modulo di classe della tua form in questo modo :

    Option Compare Database

    Option Explicit

    Dim justExtention As String

    Dim bool          As Boolean

    modifica l'evento click del cmdCopyFiles3 in questo modo:

    Private Sub cmdCopyFiles3_Click()

    If Len(Me.txtCategoria & vbNullString) = 0 Then

        VBA.MsgBox "Categoria MANCANTE!!!", vbCritical, "Attenzione"

        Exit Sub

    End If

    If Len(Me.txtSottocategoria & vbNullString) = 0 Then

        VBA.MsgBox "SottoCategoria MANCANTE!!!", vbCritical, "Attenzione"

        Exit Sub

    End If

    Const myPath  As String = "C:\reports" ' <----- personalizza il path!!

    If Not folderExists(myPath) Then MkDir myPath

     If Not FileExists(Me.txtNuovoNomeFile) Then VBA.MsgBox "il file non esiste", vbCritical, "warning": Exit Sub

     Dim strFile     As String

     Dim i           As Integer

     Dim strFileName As String

     'strFile = justFileName(Me.txtNuovoNomeFile)

    strFile = myPath & Me.txtCategoria & "" & Me.txtFile

     If Not folderExists(myPath & Me.txtCategoria) Then

         MkDir myPath & Me.txtCategoria

     End If

     If Not folderExists(myPath & Me.txtCategoria & "" & Me.txtSottocategoria) Then

         MkDir myPath & Me.txtCategoria & "" & Me.txtSottocategoria

     End If

     strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & Me.txtFile

     i = 1

     Do While Len(Dir(strFileName)) > 0

         strFileName = "copia di (" & i & ") " & Me.txtFile

         i = i + 1

         strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & strFileName

     Loop

     'strFile = strFileName

     FileCopy Me.txtNuovoNomeFile, strFileName

     If FileExists(strFile) Then VBA.MsgBox "file copiato con successo", vbInformation, "Info"

      If i > 1 Then

        VBA.MsgBox "il file :" & strFile & vbNewLine & _

                   " è stato rinominato in :" & vbNewLine & _

                    strFileName, vbInformation

     End If

     Me.txtNuovoPercorso = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & ""

    end sub

    modifica il before update del controllo txtNuovoNomeFile in questo modo :

    Private Sub txtNuovoNomeFile_BeforeUpdate(Cancel As Integer)

    justExtention = justExtentionFileName(Me.txtPath & Me.txtFile)

    If bool Then

        If FileExists(Me.txtPath & Me.txtNuovoNomeFile) Then

             VBA.MsgBox Me.txtPath & Me.txtNuovoNomeFile & vbNewLine & _

                        " esiste già assegna un altro nome!", vbCritical, "Warning"

            Cancel = True

        Else

            Name Me.txtPath & Me.txtFile As Me.txtPath & Me.txtNuovoNomeFile & justExtention

            Me.txtNuovoNomeFile.BackColor = vbWhite

            VBA.MsgBox "Ho rinominato :" & vbNewLine & _

                        Me.txtPath & Me.txtFile & vbNewLine & _

                        " così:" & vbNewLine & _

                        Me.txtPath & Me.txtNuovoNomeFile & justExtention, vbInformation

            bool = False

        End If

    End If

    End Sub

    inserisci l'evento after update del controllo txtNuovoNomeFile :

    Private Sub txtNuovoNomeFile_AfterUpdate()

    Me.txtNuovoNomeFile = Me.txtNuovoNomeFile & justExtention

    End Sub

    Facci sapere.

    Ciao, Sandro

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2016-01-20T15:34:44+00:00

    Ciao Sandro,

    sto testando e sembra andar bene con il seguente codice

    Private Sub cmdFile_Click()

    Dim lngSlashPos As Long

    Dim fullPath    As String

    fullPath = cmdFileDialog

    If Len(fullPath & vbNullString) = 0 Then Exit Sub

    lngSlashPos = InStrRev(fullPath, "")

    Me.txtPath = Mid$(fullPath, 1, lngSlashPos)

    Me.txtFile = justFileName(fullPath)

    Dim iRisposta As Integer

    iRisposta = MsgBox("Vuoi modificare il nome del file ???", vbYesNoCancel)

    Select Case iRisposta  'impostiamo il Select Case con riferimento al messaggio restituito dalla variabile iRisposta

    Case vbYes                'se risponderemo "Si" :

         Me.txtNuovoNomeFile = vbNullString  ' aggiunta

         Me.txtNuovoNomeFile.SetFocus

         Me.txtNuovoNomeFile.BackColor = vbRed

    Case vbNo                  'se sceglieremo "No":

          Me.txtNuovoNomeFile = Me.txtFile

    End Select

    bool = True

    End Sub

    e con

    Private Sub txtNuovoNomeFile_BeforeUpdate(Cancel As Integer)

    If bool Then

        If FileExists(Me.txtPath & Me.txtNuovoNomeFile) Then

             VBA.MsgBox Me.txtPath & Me.txtNuovoNomeFile & vbNewLine & _

                        " esiste già assegna un altro nome!", vbCritical, "Warning"

            Cancel = True

        Else

            Name Me.txtPath & Me.txtFile As Me.txtPath & Me.txtNuovoNomeFile

            Me.txtNuovoNomeFile.BackColor = vbWhite

            VBA.MsgBox "Ho rinominato :" & vbNewLine & _

                        Me.txtPath & Me.txtFile & vbNewLine & _

                        " così:" & vbNewLine & _

                        Me.txtPath & Me.txtNuovoNomeFile, vbInformation

            bool = False

        End If

    End If

    End Sub

    Ho modificato il comando copia files sostituendo il txt.File con txt.NUovoNomeFIle

    Private Sub cmdCopyFiles3_Click()

    Const myPath  As String = "C:\Users\Nebbia\Desktop" ' <----- personalizza il path!!

    If Not folderExists(myPath) Then MkDir myPath

    If Not FileExists(Me.txtNuovoNomeFile) Then VBA.MsgBox "il file non esiste", vbCritical, "warning": Exit Sub

    Dim strFile     As String

    Dim i           As Integer

    Dim strFileName As String

    strFile = justFileName(Me.txtNuovoNomeFile)

    If Not folderExists(myPath & Me.txtCategoria) Then

        MkDir myPath & Me.txtCategoria

    End If

    If Not folderExists(myPath & Me.txtCategoria & "" & Me.txtSottocategoria) Then

        MkDir myPath & Me.txtCategoria & "" & Me.txtSottocategoria

    End If

    strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & strFile

    i = 1

    Do While Len(Dir(strFileName)) > 0

        strFileName = "copia di (" & i & ") " & strFile

        i = i + 1

        strFileName = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & "" & strFileName

    Loop

    strFile = strFileName

    FileCopy Me.txtNuovoNomeFile, strFile

    If FileExists(strFile) Then VBA.MsgBox "file copiato con successo", vbInformation, "Info"

    Me.txtNuovoPercorso = myPath & Me.txtCategoria & "" & Me.txtSottocategoria & ""

    Sembra andar bene, a parte l'estensione :-))) e, forse, a mio avviso, non influente, sarebe opportuno far uscire un messaggio se il file che viene copiato è già presente nella cartella/sottocartella

    Grazie ancora... che fatica...

    La risposta è stata utile?

    0 commenti Nessun commento