Solved

How to copy files from the server

Posted on 2011-02-13
7
420 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

896 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

12 Experts available now in Live!

Get 1:1 Help Now