¿cómo puedo comprobar si existe un archivo?


Quiero comprobar si existe un archivo y si lo hace, quiero abrirlo y leer la 1ª línea,

Si el archivo no existe o si el archivo no tiene contenido, entonces quiero fallar silenciosamente sin que nadie sepa que se ha producido un error.

Author: John Saunders, 2011-08-23

3 answers

Comience con esto:

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(path)) Then
   msg = path & " exists."
Else
   msg = path & " doesn't exist."
End If

Tomado de la documentación .

 60
Author: Helge Klein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-23 13:47:36

Para cualquiera que esté buscando una manera de ver que un archivo específico exista en VBS:

Function bIsFileDownloaded(strPath, timeout)
  Dim FSO, fileIsDownloaded
  set FSO = CreateObject("Scripting.FileSystemObject")
  fileIsDownloaded = false
  limit = DateAdd("s", timeout, Now)
  Do While Now < limit
    If FSO.FileExists(strPath) Then : fileIsDownloaded = True : Exit Do : End If
    WScript.Sleep 1000      
  Loop
  Set FSO = Nothing
  bIsFileDownloaded = fileIsDownloaded
End Function

Uso:

FileName = "C:\test.txt"
fileIsDownloaded = bIsFileDownloaded(FileName, 5) ' keep watching for 5 seconds

If fileIsDownloaded Then
  WScript.Echo Now & " File is Downloaded: " & FileName
Else
  WScript.Echo Now & " Timeout, file not found: " & FileName 
End If
 0
Author: Đức Thanh Nguyễn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-14 14:02:33

Una carpeta existente FALLARÁ con FileExists

Function FileExists(strFileName)
' Check if a file exists - returns True or False

use en su lugar o además:

Function FolderExists(strFolderPath)
' Check if a path exists
 -3
Author: allanw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-05 20:48:44