?
Solved

How to copy files from the server

Posted on 2011-02-13
7
Medium Priority
?
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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