Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1

    avrei bisogno di una piccola funzione

    dato un certo percorso

    a = "c:\fff\ggg\pippo.exe"
    mettere in una variabile b il valore c:\fff\ggg\

    naturalmente la lunguezza del file finale in questo caso pippo.exe è variabile
    quindi potrebbe esserci un nome corto oppure un nome lungo


    come posso fare questo in vb

    aiutatemi perfavore grazie mille

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    867

    ...eccola....

    eccoti la funzione,
    non sono sicuro che funzioni perchè ho modificato un po' una
    funzione che avevo fatto per me.
    Comunque mi sembra tutto a posto.

    Private function percorso(path As String) as string
    Dim j As Integer
    j = Len(path)
    Do While (Mid(path, j, 1) <> "\")
    j = j - 1
    Loop
    percorso = Mid(path, 1, j - 1)
    End Sub

    Ciao ciao

  3. #3
    grazie mille ora la provo

  4. #4
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Ma perchè non usare un semplice Mid ?
    codice:
    Dim a As String
    Dim b As String
    
    a = "c:\fff\ggg\pippo.exe"
    b = Mid$(a, 1, 11)
    
    MsgBox b
    Ciao !

  5. #5
    Utente di HTML.it L'avatar di sebamix
    Registrato dal
    Aug 2000
    Messaggi
    1,028
    codice:
    ' GetFileName;
    ' Return the name of a file;
    ' I : strFile = full path of a file;
    ' O : string = file name only;
    ' Examples : GetFileName("c:\path\path2\file.ext") return "file.ext";
    '            GetFileName("c:\path\path2") return "path2";
    '            GetFileName("c:\path\path2\") return "";
    '            GetFileName("file.ext") return "file.ext";
    Public Function GetFileName(strFile As String) As String
        ' Temp splitting vector;
        Dim strTemp() As String
        ' Split the filename into a vector, using as delimiter "\";
        strTemp = Split(strFile, "\")
        ' If the resulting vector have more than 1 element;
        If LBound(strTemp) <> UBound(strTemp) Then
            ' Return the last element (the file name);
            GetFileName = strTemp(UBound(strTemp))
        Else
            ' Return the string passed as file name (we suppose that someone pass us the filename without path);
            GetFileName = strFile
        End If
        ' Kill the vector;
        Erase strTemp
    End Function
    
    ' GetFileExt;
    ' Return the extension of a file;
    ' I : strFile = full path of name-only of the file;
    ' O : string = extension or "" if file name have no extension;
    ' Examples : GetFileExt("c:\path\path2\file.ext") return "ext";
    '            GetFileExt("file.ext") return "ext";
    '            GetFileExt("c:\path\path2\file") return "";
    '            GetFileExt(".ext") return "ext";
    Public Function GetFileExt(strFile As String) As String
        ' Temp splitting vector;
        Dim strTemp() As String
        ' Split the filename into a vector, using as delimiter "\";
        strTemp = Split(strFile, ".")
        ' If the resulting vector have more than 1 element;
        If LBound(strTemp) <> UBound(strTemp) Then
            ' Return the last element (the extension);
            GetFileExt = strTemp(UBound(strTemp))
        Else
            ' No extension : return an empty string;
            GetFileExt = ""
        End If
        ' Kill the vector;
        Erase strTemp
    End Function
    
    ' GetFilePath;
    ' Return the path of a specified file;
    ' I : strFile = string : filename with full path;
    '     blnIncludeEndingSlash = boolean : if true, the returned path have an ending "\";
    '                                       default is false;
    ' O : string : full path of the file;
    ' Examples : GetFilePath("c:\path\path2\file.ext") return "c:\path\path2";
    '            GetFilePath("c:\path\path2\file.ext", True) return "c:\path\path2\";
    ' Tip : you can get the previous directory with this function;
    '       GetFilePath("c:\path1\path2") return "c:\path1";
    '       Note that GetFilePath("c:\path1\path2\") will return
    '       "c:\path1\path2"!;
    Public Function GetFilePath(strFile As String, Optional blnIncludeEndingSlash As Boolean = False) As String
        ' Temp splitting vector;
        Dim strTemp() As String
        ' This string will be append at the begin of the path at the end of the function;
        Dim strNetPath As String
        ' This is a simple counter;
        Dim i As Integer
        ' If the first 2 char of the given filename are "\\", then we
        ' got a net path;
        If Left(strFile, 2) = "\\" Then
            ' "\\" will be append at the begin of the path;
            strNetPath = "\\"
        Else
            ' Append nothing at the begin of the path;
            strNetPath = ""
        End If
        ' Split the filename using "\" as delimiter;
        strTemp = Split(strFile, "\")
        ' Reset the return path;
        GetFilePath = ""
        ' Pass all the elements of the vector (but not the last);
        For i = LBound(strTemp) To UBound(strTemp) - 1
            ' If the resulting path is still empty do not append "\";
            If GetFilePath <> "" Then GetFilePath = GetFilePath & "\"
            ' Append the current element of the splitting vector on the resulting path;
            GetFilePath = GetFilePath & strTemp(i)
        Next i
        ' Must we append the final "\"?
        If blnIncludeEndingSlash = True Then
            ' yes!
            GetFilePath = GetFilePath & "\"
        End If
        ' Add the eventual "\\" at the begin of the path;
        GetFilePath = strNetPath & GetFilePath
        ' Erase the splitting vector from the memory;
        Erase strTemp
    End Function

  6. #6
    Utente di HTML.it L'avatar di sebamix
    Registrato dal
    Aug 2000
    Messaggi
    1,028
    Originariamente inviato da Mabi
    Ma perchè non usare un semplice Mid ?
    codice:
    Dim a As String
    Dim b As String
    
    a = "c:\fff\ggg\pippo.exe"
    b = Mid$(a, 1, 11)
    
    MsgBox b
    Ciao !
    Non va bene perchè funziona solo con "c:\fff\ggg\pippo.exe" e non con un nome file generico.
    Credo che cristian voglia qualcosa di generico, appunto

  7. #7
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Hai ragione sebamix, avevo capito che voleva qualcosa solo per il suo esempio.
    Allora questo ti accontenta in qualsiasi caso:
    codice:
    Dim Pos As Integer
    Dim Cont As Integer
    Dim a As String
    
    a = "C:\l'esempio che vuoi\file.est"
    Pos = 1
    Do
      Cont = Pos
      Pos = InStr(Pos, a, "\", vbTextCompare)
      Pos = Pos + 1
    Loop Until Pos = 1
    
    MsgBox Mid$(a, Cont, Len(a))
    OK ?

  8. #8
    madonna che giri... vi siete dimenticati che meno si scrive e meglio e'?

    basta una istruzione una

    codice:
    Dim a as string, b as string
    
    a = "C:\l'esempio che vuoi\file.est"
    b = Left(a, InStrRev(a, "\"))

  9. #9
    grazie mille a tutti
    comuncque quella di mabi mi va benissimo


    x sebamix mi sembra un po esagerata la tua funzione

  10. #10
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    basta una istruzione una
    Si, ma per avere il nome del file.
    Se invece ti serve
    mettere in una variabile b il valore c:\fff\ggg\
    allora non va bene.

    Comunque ho sbagliato anch'io nella visualizzazione del risultato:
    codice:
    MsgBox Mid$(a, 1, Cont - 1)
    Ciao !

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.