Solved

How to copy files from the server

Posted on 2011-02-13
7
421 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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