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-24T10:25:54+00:00

    ciao NebbiaDb,

    non capisco...

    nel senso che la modifica del nome del file deve essere eseguite tramita la procedure dell'evento click del post precedente.

    Se modifichi il nome del file prima di lanciare la procedura dell'evento click di cui al precedente post, il file per forza il file non esiste ed esce il messaggio.

    ciao, Sandro.

    *** edit ***

    forse ho capito...

    modifica questo :

    Private Sub txtNuovoNomeFile_AfterUpdate()

    Me.txtNuovoNomeFile = Me.txtNuovoNomeFile & justExtention

    Me.txtFile = Me.txtNuovoNomeFile

    End Sub

     e questo :

    Private Sub cmdCopyFiles3_Click()

    On Error GoTo errorHandler

    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

    Dim strFile     As String

    strFile = Me.txtFile

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

     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

     Dim strFileName As String

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

     If FileExists(strFileName) Then

         Dim style    As VBA.VbMsgBoxStyle

         Dim style2   As VBA.VbMsgBoxStyle

         Dim strInfo  As String

         Dim strInfo2 As String

         Dim Title    As String

         strInfo = "il file:" & vbNewLine & _

               strFileName & vbNewLine & _

               "già esiste." & vbNewLine & _

               "ok per rinominare, annulla per sovrascrivere"

         Title = "Warning!"

         style = vbCritical + vbOKCancel

         style2 = vbCritical

         If VBA.MsgBox(strInfo, style, Title) = vbOK Then

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

                    Do While Len(Dir(strFileName)) > 0

                       strFile = InputBox("nome del file:", "Inserisci il nome del file", , 12000, 7000)

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

                       If FileExists(strFileName) Then

                            strInfo2 = "il file:" & vbNewLine & _

                            strFileName & vbNewLine & _

                            "già esiste."

                            VBA.MsgBox strInfo2, style2, Title

                        End If

                        strFile = strFile & justExtention

                    Loop

        End If

     End If

     Me.txtNuovoNomeFile = IIf(Len(strFile & vbNullString) = 0, Me.txtFile, strFile)

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

     FileCopy Me.txtPath & Me.txtFile, strFileName

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

    ext_errorLoadAccountHandler:

        Exit Sub

    errorHandler:

        With Err

           MsgBox "ERR#" & .Number _

                & vbNewLine & .Description _

                , vbOKOnly Or vbCritical

         End With

         Resume ext_errorLoadAccountHandler

    End Sub

    *** end edit ***

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-01-24T09:53:05+00:00

    buona domenica caro Sandro

    quasi ci siamo ...:-(((

    nel senso che se dopo carica file voglio modificare il nome del file e vado a copiare dice file non esiste

    il resto sembra rispondere perfettamente...

    Grazie ancora

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2016-01-24T09:03:24+00:00

    ciao NebbiaDB,

    ok, prova a vedere se con queste modifiche ci siamo :

    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.txtFile) Then VBA.MsgBox "il file non esiste", vbCritical, "warning": Exit Sub

     Dim strFile     As String

     Dim i           As Integer

     Dim strFileName As String

     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.txtNuovoNomeFile

     If FileExists(strFileName) Then

         Dim style    As VBA.VbMsgBoxStyle

         Dim style2   As VBA.VbMsgBoxStyle

         Dim strInfo  As String

         Dim strInfo2 As String

         Dim Title    As String

         strInfo = "il file:" & vbNewLine & _

               strFileName & vbNewLine & _

               "già esiste." & vbNewLine & _

               "ok per rinominare, annulla per sovrascrivere"

         Title = "Warning!"

         style = vbCritical + vbOKCancel

         style2 = vbCritical

         If VBA.MsgBox(strInfo, style, Title) = vbOK Then

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

                    Do While Len(Dir(strFileName)) > 0

                       strFile = InputBox("nome del file:", "Inserisci il nome del file", , 12000, 7000)

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

                       If FileExists(strFileName) Then

                            strInfo2 = "il file:" & vbNewLine & _

                            strFileName & vbNewLine & _

                            "già esiste."

                            VBA.MsgBox strInfo2, style2, Title

                        End If

                    Loop

        End If

     End If

     Me.txtNuovoNomeFile = IIf(Len(strFile & vbNullString) = 0, Me.txtFile, strFile & justExtention)

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

     FileCopy Me.txtPath & Me.txtFile, strFileName

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

    End Sub

    ciao, Sandro.

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2016-01-23T20:37:51+00:00

    Grazie Sandro...quasi perfettamente funzionante

    ho aggiunto alla fine del codice

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

    per aggiornare il txtNuovoPercorso

    infine su ok "inserisci il nome del file" dovrebbe riaggiornare il txtNuovoNomeFIle

    Grazie infinite :-)

    La risposta è stata utile?

    0 commenti Nessun commento