Visual Basic - nombre de archivo aleatorio

Life is soft - evento anual de software empresarial
 
Vista:

nombre de archivo aleatorio

Publicado por Alfredo (67 intervenciones) el 05/09/2004 19:22:31
hola, como puedo hacer para que el sistema me asigne un nombre de archivo aleatorio y unico?????
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:nombre de archivo aleatorio

Publicado por Cecilia Colalongo (3116 intervenciones) el 05/09/2004 22:11:50
Fijate con esto:

Public Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long

Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Function GetTemporaryFileName(Optional Prefix As String=vbNullString) As String
Dim strBuffer As String

strBuffer = String(MAX_PATH, " ")

GetTempPath MAX_PATH, strBuffer

GetTempFileName strBuffer, Prefix, 0, strBuffer

GetTemporaryFileName = Trim(Replace(strBuffer, Chr(0), vbNullString))
End Function

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:nombre de archivo aleatorio

Publicado por Ruri (583 intervenciones) el 05/09/2004 22:30:21
Alfredo: Lo que pedía es un temporal. Para obtenerlo podés utilizar el Api de esta manera:

Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Function CrearTemporalEnUnPath(Optional ByVal strPref As String = "tmp", Optional ByRef strPath As String = "") As String
On Error Resume Next
Dim sBuffer As String, lng As Long
If Len(strPref) > 3 Then strPref = Left$(strPref, 3)

If strPath = "" Then strPath = ObtenerDirectorioTemporal

If strPath = "" Then GoTo NoTemporal 'NO DEBERÍA OCURRIR
sBuffer = String$(260, 0)
lng = GetTempFileName(strPath, strPref, 0, sBuffer)
CrearTemporalEnUnPath = IIf(lng <> 0, Left$(sBuffer, InStr(1, sBuffer, vbNullChar, vbTextCompare) - 1), "")
Exit Function
NoTemporal:
CrearTemporalEnUnPath = ""
End Function

Private Function ObtenerDirectorioTemporal() As String
Dim sBuffer As String, lng As Long
sBuffer = String$(260, 0)
lng = GetTempPath(260, sBuffer)
ObtenerDirectorioTemporal = IIf(lng >= 3, Left$(sBuffer, lng), "")
End Function

Private Sub Command1_Click()
MsgBox CrearTemporalEnUnPath
End Sub

Saludos Ruri
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar