Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1 file, 85 users....

Status
Not open for further replies.

mojo1979

Technical User
Nov 17, 2003
138
US
Hi,

I am trying to automate a process, whereby each time a user logs on a certain file (saved on a shared drive) is copied to their desktop. If that file already exists on the desktop (i.e. an old version), then it is deleted and replaced with the current one.

After some help from a fellow Tek-tips expert, I have managed to get this far....

On error goto 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Create a Path to a log file
Const fileName = "c:\vbslog.txt"

Dim WshShell,WshEnv
Dim WshNetwork:Set WshNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
set WshEnv = WshShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
SysDrive = WshEnv("SYSTEMDRIVE")

Dim objDomain:Set objDomain = getObject("LDAP://rootDse")
Dim DomainString:DomainString = objDomain.Get("dnsHostName")

Dim UserString:UserString = WshNetwork.UserName
Dim objADSysInfo:Set objADSysInfo = CreateObject("ADSystemInfo")
strComputer = WshNetwork.ComputerName

UserDesktop = WshShell.SpecialFolders("Desktop")

'Your file name
fname = "test.xls"
'Source file location
SFile = SysDrive & "\\dxbserver1\public\" & fname
'Destination Location
Dfile = "\\dxbserver1\users\" & UserString & "\Desktop"

'Delete The file
path = dfile & "\" & fname
wscript.echo path

Set file = objFSO.GetFile(path)
If objFSO.FileExists(path) Then
objFSO.DeleteFile file.Path
End if
'Wait 5 seconds
wscript.sleep 5000

'Log Success and Failure
Set fso = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists(Data_Path & filename) Then
Set f = fso_OpenTextFile(Data_Path & fileName,2, True)
Else
Set f = fso_OpenTextFile(Data_Path & fileName,8)
End If

If strComputer = "" Then
f.WriteLine VbCrLf & strComputer &" did not respond."
Else
f.WriteLine VbCrLf & strcomputer & " responded."
objfso.CopyFile SFile, DFile, overwrite = TRUE

End If

But when running this I get a error:

Line: 35
Char: 1
Error: file not found
Code: 800A0035

Please note that the user's desktop redirects to a share on my file server, i.e. \\dxbserver1\users\%username%\desktop

Any help would be greatly appreciated!

Steve
 
Get rid of this line:
Set file = objFSO.GetFile(path)

Are you sure of the following ?
SFile = SysDrive & "\\dxbserver1\public\" & fname

Why having many FileSystemObject ?

I suspect an error here:
objfso.CopyFile SFile, DFile, overwrite = TRUE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
After deleting the line suggested, i get the error:

Line: 53
Char: 6
Error: path not found
Code: 800A004c

Which as you suggest, points to a problems with the line objfso.copyfile.....
 
I tried and tested this so it should work

Code:
On error goto 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Create a Path to a log file
Const fileName = "c:\vbslog.txt"
 
Dim WshShell,WshEnv
Dim WshNetwork:Set WshNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
set WshEnv = WshShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
SysDrive = WshEnv("SYSTEMDRIVE")

Dim objDomain:Set objDomain = getObject("LDAP://rootDse")
Dim DomainString:DomainString = objDomain.Get("dnsHostName")

Dim UserString:UserString = WshNetwork.UserName
Dim objADSysInfo:Set objADSysInfo = CreateObject("ADSystemInfo")
strComputer = WshNetwork.ComputerName

UserDesktop = WshShell.SpecialFolders("Desktop")

'Your file name
fname = "test.xls"
'Source file location
SFile = "\\dxbserver1\public\" & fname
wscript.echo sfile
'Destination Location
Dfile =  UserDesktop  & "\"
wscript.echo dfile
'"\\dxbserver1\users\" & UserString & "\Desktop"

'Delete The file
path = UserDesktop & "\" & fname
wscript.echo path

Set file = objFSO.GetFile(path)
If objFSO.FileExists(path) Then
    objFSO.DeleteFile file.Path
End if
'Wait 5 seconds
wscript.sleep 5000

'Log Success and Failure
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not FSO.FileExists(Data_Path & filename) Then
     Set f = fso.OpenTextFile(Data_Path & fileName,2, True)
    Else
     Set f = fso.OpenTextFile(Data_Path & fileName,8)
    End If

    If strComputer = "" Then
     f.WriteLine VbCrLf & strComputer &" did not respond."
    Else
     f.WriteLine VbCrLf & strcomputer & " responded."   
     objfso.CopyFile SFile, DFile, overwrite = TRUE

    End If
 
Genius!

It works....

To remove the prompts, I just remove all the wscript lines correct?

Thanks alot!
 
Ah, slight problem.

The first time the script is run it will copy the file to the desktop. However, if the script is then run again (i.e the user logs on again), then an error appears:

Line: 36
Char: 5
Error: Object required: 'file'
Code: 800A01A8
 
possibly the file does not exist.
Put On error resume next at the top of the script
 
Ok, if I add that then no error message comes up, but the file isn't replaced by the newer version...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top