1

I have a folder that does exist on the network. When I run the following code before browsing to that directory (from Windows 7 Enterprise x64), it returns False. When I run the code after browsing to the directory, it returns True.

Const SHAREPOINT_FOLDER = "\\MyServer\sites\IT\MySite\Shared%20Documents\"

MsgBox sharePointExists()

Function sharePointExists()
  Set fso = CreateObject("Scripting.FileSystemObject")

  sharePointExists = fso.FolderExists(SHAREPOINT_FOLDER)            

  Set fso = Nothing
End Function

I have not tested this on any other OS. This is a SharePoint 2010 website directory. Again, the folder does exist and is readable and writable by me. If I browse to it manually in Windows Explorer, this script returns True until I restart my computer. Once I restart, the script returns False.

Any suggestions on making this work? It is a critical part of my program, and I need to have it work without manually browsing to the directory.

Note: I have replaced the FolderExists with FileExists and give it a valid filename. The same problem exists for FileExists.

If I substitute a Wscript.Shell object and execute a shell.Run, it opens the folder without trouble. This process takes several seconds (just like when I browse to it manually), but once I do this, I get True every time.

Be aware: If this function is run outside of my network, I want it to return False. I cannot assume it is True (otherwise I simply would not check for existence). Also, even on the network, this folder may not be accessible 100% of the time - another justification for checking the existence.

RalphyZ
  • 843
  • 8
  • 9

3 Answers3

0

There are two directions you can chose in this I would say: It seems the problem itself is that the network drive is not fully connected after a reboot. Without details it is hard to say why but if this is for example a drive mapped via a drive letter it might not be available at the time of login. If you access such a drive with the windows explorer a function in the windows api is called to automatically do the reconnect, but this is not triggered with programatic accesses.

Direction 1 would therefore be to make sure the drive is always correctly accessable. Changing GPOs to include "always wait for the network at computer startup and logon" might help or if you map this by script it might be possible to change the mapping script itself (add checks, persistent yes vs no, etc). I don't know enough about your actual setting to give solid advice there.

Direction 2 would be mitigating the problem in the script. If it is indeed a mapped drive you can just make sure in the script it is always mapped by remapping in the script itself. There is also a way to trigger the api call windows does programatically but not in vbscript so I don't know if that is an option for you.

I assume that not only the check fails but the real operation you want to execute on that share would fail as well? If it is only the FolderExists check that fails you could also just do an

On Error Resume Next

before the real access and analyze the results.

Syberdoor
  • 2,521
  • 1
  • 11
  • 14
0

Since I needed to know if the share truly exists (not just ignore the fact that my script was not being truthful), I decided to map a drive letter to the SharePoint directory first - and if that fails, I know that the folder does not really exist. Doing that, I can automate the check, and it doesn't take much extra time. Also, with that mapped drive, I can do all my tasks (copying files) using the drive letter, and not the full SharePoint path.

objNetwork.MapNetworkDrive driveLetter & ":", drivePath, false, strUser, strPassword  

I use the username/password because for the first connection to SharePoint (in VBScript, not in Windows), I was getting an authentication error.

RalphyZ
  • 843
  • 8
  • 9
-1

Use

sharePointExists = fso.FolderExists( Unescape( SHAREPOINT_FOLDER))            

The Unescape(charString) function returns a string (in Unicode format) that contains the contents of charString. ASCII character set equivalents replace all characters encoded with the %xx hexadecimal form. Characters encoded in %uxxxx format (Unicode characters) are replaced with the Unicode character with hexadecimal encoding xxxx.

Resource (reverse to the Escape function)

Edit: maybe you should reconnect to an idle connection to the server. Something similar as mentioned in Tao's (self)answer here. Map the connection by mapping network drive, perform your task, drop the connection.

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thank you for the tip, however this did not fix my problem :( I still get False unless I manually browse to this folder first. – RalphyZ Feb 23 '15 at 14:45