Solved

How to copy files from the server

Posted on 2011-02-13
7
419 Views
Last Modified: 2012-05-11
Hi,

help me with a VBScript to copy files? I need a script that copied all the files from "\\S1\Test\Temp" to "%USERPROFILE%\Desktop"

Finally, I want a MsgBox, where the user is presented with information, files were copied. However, the MsgBox to display only the file name without end and without a path, where it is located.
Example:

 The server copies the file to the desktop user "\\S1\Test\TEMP\Test20.docx"
 The user will see a MsgBox: On the desktop you have the file: Test20

 Thanks for your help.
0
Comment
Question by:Thomas_Meyer
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:karthika_cts
ID: 34885984
There are various methods, and here goes one by one


Method 1:
-------------
Dim FSO

Set FSO = CreateObject("Scripting.FileSystemObject")

FSO.CopyFile "\\file to be copied path", "\\destination directory"

Msgbox "File copied"

Method 2:
---------------

Const OVERWRITE_EXISTING = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
' global variables
source = "\\servername\imaging\*.*"
dest = "c:\windows\system\"
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
If Err = 0 Then    
Wscript.Echo "files copied successfully"
Else    
Wscript.Echo "No files copied"
End If    


Method 3:
------------------

Option Explicit
dim path
path=CreateObject("WScript.Shell").ExpandEnvironmentStrings("%UserProfile%")
& ("\Application Data\Microsoft\Templates")
dim objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(path & ("\envelope.dotx")) = False Then
objFSO.CopyFile "\\servername\stdapps\custemp\envelope.dotx", path & "\"
End If
If objFSO.FileExists(path & ("\AgencyFaxCover.dotx"))= False Then
objFSO.CopyFile "\\servername\stdapps\custemp\AgencyFaxCover.dotx", path & "\"
End If
If objFSO.FileExists(path & ("\AgencyLetterhead_BW.dotx"))= False Then
objFSO.CopyFile "\\servername\stdapps\custemp\AgencyLetterhead_BW.dotx",
path & "\"
End If


References:
http://www.eggheadcafe.com/software/aspnet/32567129/copy-file-from-server-share-to-user-profile.aspx
http://www.computing.net/answers/programming/vb-script-needed-to-copy-files/14942.html

0
 

Author Comment

by:Thomas_Meyer
ID: 34886112
I need to display a MsgBox, which will be given the copied file.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34892217
Hi, here is another way.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSource = "\\S1\Test\Temp"
strDestination = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
strMessage = "You have the following files on the desktop:"
For Each objFile In objFSO.GetFolder(strSource).Files
	objFSO.CopyFile objFile.Path, strDestination, True
	strMessage = strMessage & VbCrLf & Left(objFile.Name, InStrRev(objFile.Name, ".") - 1)
Next
MsgBox strMessage

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Thomas_Meyer
ID: 34894424
Thank you for your work is great, but the problem occurred to me that my script copies the file Thumbs.db, it would not please him somehow removed from the filter?
Thank you very much.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34895592
You can of course check the file name, and if it is thumbs.db, then do not copy it.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strSource = "\\S1\Test\Temp"
strDestination = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
strMessage = "You have the following files on the desktop:"
For Each objFile In objFSO.GetFolder(strSource).Files
	If LCase(objFile.Name) <> "thumbs.db" Then
		objFSO.CopyFile objFile.Path, strDestination, True
		strMessage = strMessage & VbCrLf & Left(objFile.Name, InStrRev(objFile.Name, ".") - 1)
	End If
Next
MsgBox strMessage

Open in new window

0
 

Author Closing Comment

by:Thomas_Meyer
ID: 34896150
Amazing work, thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34900590
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now