none
Refresh network folder with vbscript RRS feed

  • Question

  • Hi all,

    This sub works well most of the time.

    Public Sub REGISTARO
     Dim FILOLOG, oFILO, objFilo
     Set objFilo = CreateObject("Scripting.FileSystemObject")
     FILOLOG = "\\FileServer\Share\mYfOLDER\Myfile.txt"
     If objFilo.FileExists(FILOLOG) Then objFilo.DeleteFile(FILOLOG)
     Set oFILO = objFilo.OpenTextFile(FILOLOG, ForWriting, True) 
     oFILO.writeline(Now & "-" & "TALIGADO")
     oFILO.Close
     Set oFILO = Nothing
     Set objFilo = Nothing
    End Sub

    However when is used for the first time in the day, it gives an error in the line:

    If objFilo.FileExists(FILOLOG) Then objFilo.DeleteFile(FILOLOG)

    And the error is:

    2017-09-14 07:00:10-MlogORCA.vbs(1185, 32) Microsoft VBScript runtime error: File not found

    If before I run the script I use the Windows Explorer  to refresh the folder the error never happends

    My problem is that I run the script from a Schedule Task and I'm unable to do it manually thee refresh...

    Is there a solution to this ?

    Thanks


    Joao Simplicio Rodrigues



    Thursday, September 14, 2017 7:11 PM

Answers

  • So what I'm looking for is a way to "refresh" the "folder"... Like making F5 in the Windows Explorer

    All F5 does is refresh the GUI window's display with what exists in the underlying file system. It does absolutely nothing else. That has nothing whatsoever to do with whatever problem you are experiencing.

    You can skip the error condition in the script by using On Error Resume Next, as already noted. With that line of code, VBScript will proceed to the next line of code without halting the script. If an error occurred, the Number property of the built-in Err object (i.e., Err.Number) will be non-zero.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 14, 2017 8:50 PM
    Moderator

All replies

  • Your code is full of bad, unprintable characters.

    Sub REGISTARO
    	Dim FILOLOG,oFILO,objFilo
    	Set objFilo = CreateObject("Scripting.FileSystemObject")
    	FILOLOG="\\FileServer\Share\mYfOLDER\Myfile.txt"
    	If objFilo.FileExists(FILOLOG) Then
    		objFilo.DeleteFile(FILOLOG)
    	End If
    	Set oFILO = objFilo.OpenTextFile(FILOLOG,2, True) 
    	objFilo.writeline(Now & "-" & "TALIGADO")
    	objFilo.Close
    End Sub
    
    You cannot easily copy VB or VBA code to VBScript without fixing up the differences.


    \_(ツ)_/

    Thursday, September 14, 2017 7:27 PM
  • Ok

    I pasted directly from VbsEdit.

    Now from Notepad.

    Public Sub REGISTARO
     Dim FILOLOG, oFILO, objFilo
     Set objFilo = CreateObject("Scripting.FileSystemObject")
     FILOLOG = "\\FileServer\Share\mYfOLDER\Myfile.txt"
     If objFilo.FileExists(FILOLOG) Then objFilo.DeleteFile(FILOLOG)
     Set oFILO = objFilo.OpenTextFile(FILOLOG, ForWriting, True) 
     oFILO.writeline(Now & "-" & "TALIGADO")
     oFILO.Close
     Set oFILO = Nothing
     Set objFilo = Nothing
    End Sub

    Hope it is better... My question remains.

    Thank you


    Joao Simplicio Rodrigues

    Thursday, September 14, 2017 7:36 PM
  • I fixed your code.  I don't need another copy.VBSEdit inserts abd characters when pasting.

    This is the bad line:

    Set oFILO = objFilo.OpenTextFile(FILOLOG, 2, True)

    You need to use "2" as the open type.


    \_(ツ)_/

    Thursday, September 14, 2017 7:45 PM
  • You can use On Error Resume Next in your subroutine to trap the error and react accordingly.

    We can't reproduce the conditions whereby you experience your error condition.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 14, 2017 7:48 PM
    Moderator
  • Not defining the open mode will prevent the file from updating.  The file will never exist on first execution because it is not being created.

    The folder needing to be refreshed is also odd if this is not Windows XP or earlier.  XP without certain patches can find a drive unavailable on first access.

    "FileExists" will never throw an error so On Error will not have an issue and the "DeleteFile" will never be executed.

    If on XP then search for a copy of the patch that is missing ot map a drive to the share.


    \_(ツ)_/

    Thursday, September 14, 2017 8:00 PM
  • Im sorry my bad.

    Forget to mention that in the begin of the script I have: 

    Option Explicit
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8

    Also have additional difficulties in explain my self because English is not my native language.

    My the problem is that the file exist and then cannot be found to delete. In this line:

     If objFilo.FileExists(FILOLOG) Then objFilo.DeleteFile(FILOLOG)

    The error happends only just early in the morning, first time it runs... Then runs ok.

    And I know that is a network issue.

    So what I'm looking for is a way to "refresh" the "folder"... Like making F5 in the Windows Explorer 

    If I may say like this...

    Thanks again.


    Joao Simplicio Rodrigues


    Thursday, September 14, 2017 8:09 PM
  • Further information

    This happends in a large network file system.

    My workstation run Win 8.1 and the server Windows Server 2008 R2.

    I going to try run a cmd with a simple Dir command to the folder...


    Joao Simplicio Rodrigues

    Thursday, September 14, 2017 8:17 PM
  • So what I'm looking for is a way to "refresh" the "folder"... Like making F5 in the Windows Explorer

    All F5 does is refresh the GUI window's display with what exists in the underlying file system. It does absolutely nothing else. That has nothing whatsoever to do with whatever problem you are experiencing.

    You can skip the error condition in the script by using On Error Resume Next, as already noted. With that line of code, VBScript will proceed to the next line of code without halting the script. If an error occurred, the Number property of the built-in Err object (i.e., Err.Number) will be non-zero.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 14, 2017 8:50 PM
    Moderator
  • Just for reference.

    To solve my issue the Dir command in a cmd works.

    Thank you for your attention.


    Joao Simplicio Rodrigues

    Friday, September 15, 2017 9:28 AM
  • I refer the honorable gentlemen to the answers already provided.

    -- Bill Stewart [Bill_Stewart]

    Friday, September 15, 2017 4:08 PM
    Moderator