Solved

How to copy files from the server

Posted on 2011-02-13
7
424 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

724 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