¿Hay alguna función que devuelva automáticamente el nombre de un fichero
temporal que no exista ya?
Y, por úlitmo, ¿cómo se puede leer una variable de entorno?
==============
HILO COMPLETO=
==============
Buho:
-----
La variable de entorno puedes leerla con la funcion Environ.
En cuanto al fichero temporal, yo emplearía la tecnica inversa. Es decir,
crear una funcion que mediante la genreacion de un numero aleatorio de 8
cifras (Randomize y Rnd) , por ejemplo, generara un nombre: 98756432.tmp, si
se supone que el nombre del fichero temporal no es importante. Miraría si
existe (Dir) dicho nombre de fichero. SI existe, reiteraría de nuevo la
generacion de un nuevo nombre de fichero hasta probar su NO existencia. Y es
el que utilizaría.
SoftJaen
========
Sí, mediante la función API «GetTempFileName», la cual te devuelve el nombre
de un archivo temporal.
Adjunto te remito unas funciones de mi biblioteca de utilidades que te serán
muy prácticas para tu propósito, ya que las mismas te permiten crear un
archivo temporal o sólo obtener el nombre de dicho archivo, así como el
prefijo que quieres utilizar, que por defecto es ~.
Inserta el formulario que adjunto en el algún proyecto.
Y, por úlitmo, ¿cómo se puede leer una variable de entorno?
Como te lo ha dicho Buho, con la función «Environ»:
Ahí va la copia de funciones:
Option Explicit
Private Const MAX_PATH = 260
Private Declare Function apiDeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Declare Function apiGetTempFileName 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 apiGetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
'*************************************************************
' Nombre: DeleteFile
' por Enrique Martínez «Softjaen» - 18/08/96
'
' Finalidad:
' Borra el archivo especificado.
'
' Resultado:
' Boolean: True, si la función termina correctamente.
'
'*************************************************************
Public Function DeleteFile(ByVal sFileName As String) As Boolean
DeleteFile = CBool(apiDeleteFile(sFileName))
End Function
'*************************************************************
' Nombre: GetTempPath
' por Enrique Martínez «Softjaen» - 01/07/96
'
' Finalidad:
' Recupera la trayectoria que se ha designado para
' el uso de los archivos temporales
'
' Resultado:
' String: La trayectoria temporal es la especificada por la
' variable de entorno TMP. En caso de no existir esta
' variable, el valor lo da la variable de entorno TEMP
' y, en último caso, si ésta tampoco existiese, el
' directorio vigente.
' En caso de error devuelve 'vbNullString'.
'*************************************************************
Public Function GetTempPath() As String
Dim sBuffer As String, lSize As Long
sBuffer = String(260, vbNullChar)
lSize = apiGetTempPath(Len(sBuffer) - 1, sBuffer)
If lSize Then
GetTempPath = Left(sBuffer, lSize)
Else
GetTempPath = vbNullString
End If
End Function
'*******************************************************************************
' Nombre: GetTempFileName
' por Enrique Martínez «Softjaen» - 13/08/96
'
' Finalidad:
' Obtiene el nombre de un archivo temporal para
' que lo utilice una aplicación
'
' Entradas:
' sPrefijo: Prefijo que se utilizará en el nombre del archivo
'
' bCreateFile: True = Se crea el archivo; False = Sólo devuelve el nombre
'
' Resultado:
' String: En caso de error devuelve 'vbNullString'.
'
'*******************************************************************************
Public Function GetTempFileName(Optional ByVal sPrefijo As String = "~", _
Optional ByVal bCreateFile As Boolean) As String
Dim sTemp As String, sFileName As String, lUnique As Long
sFileName = String(MAX_PATH, vbNullChar)
' Obtenemos el directorio TEMP
sTemp = GetTempPath()
' Obtenemos el nombre del archivo temporal
lUnique = apiGetTempFileName(sTemp, sPrefijo, 0&, sFileName)
If lUnique > 0 Then
GetTempFileName = Left(sFileName, InStr(sFileName, vbNullChar) - 1)
' Eliminamos el archivo si se ha elegido la opción de no crearlo.
If Not bCreateFile Then DeleteFile GetTempFileName
End If
End Function
Private Sub Command1_Click()
' Obtengo el nombre de un archivo temporal, el
' cual empieza con el prefijo "&"
MsgBox GetTempFileName("&", True)
End Sub
Christian Valeria Celedón
-------------------------
Crea un nuevo proyecto y agrégale el siguiente código al formulario:
Option Explicit
'Declaración de funciones API de Windows
'-----------------------------------------------------------------------------------
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
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
'Esta función crea un archivo temporal en el disco duro y devuelve
'el path asociado.
Public Function GetTempFileNameString() As String
Dim mstrTempPath As String
Dim mstrTempFile As String
Dim mstrPrefix As String
' Obtenemos el path de la carpeta temporal. Si se desea crear el archivo temporal
' en otra ruta, se deben cambiar las 3 líneas siguientes por algó así:
' mstrTempPath = "C:\Mis documentos"
mstrTempPath = String(260, Chr(0))
GetTempPath 260, mstrTempPath
mstrTempPath = Left(mstrTempPath, InStr(mstrTempPath, Chr(0)) - 1)
' Establecemos un prefijo para el nombre del archivo.
mstrPrefix = "CVC" 'Mis iniciales.
' Creamos el archivo temporal y devolvemos la ruta.
mstrTempFile = String(260, 0)
GetTempFileName mstrTempPath, mstrPrefix, 0, mstrTempFile
mstrTempFile = UCase(Left(mstrTempFile, InStr(1, mstrTempFile, Chr(0)) - 1))
GetTempFileNameString = mstrTempFile
End Function
Private Sub GetEnviron()
Dim mstrEnviron As String
Dim mstrTempPath As String
Dim mintIndex As Integer
Dim mintFreeFile As Integer
' Obtenemos la ruta de un nuevo archivo temporal.
mstrTempPath = GetTempFileNameString
' Obtenemos un handle para abrir el archivo.
mintFreeFile = FreeFile
' Abrimos el archivo temporal.
Open mstrTempPath For Output As #mintFreeFile
' Inicializamos el índice a 1.
mintIndex = 1
Do
' Obtiene la variable de entorno.
mstrEnviron = Environ(mintIndex)
' Escribimos la variable de entorno en el archivo.
Print #mintFreeFile, mstrEnviron
mintIndex = mintIndex + 1
Loop Until mstrEnviron = ""
' Cerramos el archivo temporal.
Close #mintFreeFile
' Abrimos el archivo temporal con el Block de Notas.
Shell "NOTEPAD.EXE " & mstrTempPath, vbNormalFocus
End Sub
Private Sub Form_Load()
GetEnviron
End Sub
               (
geocities.com/es/ensolva/Descargas)                   (
geocities.com/es/ensolva)                   (
geocities.com/es)