Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript required to copy files

Status
Not open for further replies.

sorrell

IS-IT--Management
Mar 7, 2005
9
GB
Please can any of you vb scripting wizards help me out?I require a script to copy files from \\server1\share1 to \\server2\share2\folder. So on server 1 I have a list of files. On server 2 I have a list of folders with the same name. I want the script to copy file1 and place in folder1, file2 into folder2, file3 into folder3, etc.My files and folders have the following naming convention - surnamefirstname. The name is 8 characters long, upto 5 for the surname and 3 for the firstname, e.g smithian. This could be shorter dependent on the users name, e.g coxdeb I am not bothered about changing permissions on the folders or anything fancy like that. Can anyone help me out??Thanks in advance
 
When you say you have a 'list of files' on server1, does this mean all the files in a specific folder, a text file or spreadsheet listing the files, or something else?

So if you have \\server1\share1\smithian.dat you want to copy it to \\server2\share2\smithian\smithian.dat?

Are the target folders already created or will the script have to create them?
 
Hi Jges, thanks for taking time to reply, it is much appreciated.

You have got it right in the second half of your reply. I have on server 1 a list of files (approx 600)in the same share:-

\\server1\share1\smithian.dat
\\server1\share1\jonesdav.dat
\\server1\share1\johnsbri.dat

I would like the script to copy these files to another server and place each file in an existing folder that I have created:-

\\server2\share2\smithian\smithian.dat
\\server2\share2\jonesdav\jonesdav.dat
\\server2\share2\johnsbri\johnsbri.dat

This script will be used once to copy the files to the new location. Thanks in advance

 
jgres said:
When you say you have a 'list of files' on server1, does this mean all the files in a specific folder, a text file or spreadsheet listing the files, or something else?

sorrell said:
I have on server 1 a list of files (approx 600)in the same share:

So, to be clear, by "list of files" you mean a "share that contains approx. 600 .dat files"

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
yes that is correct, 600 files of the same filetype
 
Here is some code, it doesn't do much in the way of error checking - run a few small tests before unleashing it.

You'll need to edit the source and target folder constants to meet your needs.
Code:
Option Explicit
dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

'edit the following 2 lines as needed
const sourceFolder = "\\server1\share1"
const targetFolder = "\\server2\share2"

'make sure target folder exists
If not objFSO.FolderExists(sourceFolder) Then
	wscript.echo("invalid folder")
	wscript.quit
end if
	
	dim objStartFolder
	Set objStartFolder = objFSO.GetFolder(sourceFolder)
	dim colFiles
	dim objFile
	dim newFolder
	dim newFile
	
	set colFiles = objStartFolder.Files
	for each objFile in colFiles
		'wscript.echo("file: " & objFile)
		newFolder = objFSO.BuildPath(targetFolder, objFSO.GetBaseName(objFile.Name))
		'wscript.echo("newFolder: " & newFolder)
		newFile = objFSO.BuildPath(newFolder, objFile.Name)
		'wscript.echo("newFile: " & newFile)
		if objFSO.FolderExists(newFolder) then
			if not objFSO.FileExists(newFile) then
				objFSO.CopyFile objFile, newFile
			end if
		end if
	next
	
	wscript.echo("Done")

Save the above code in a text file, give it a name that makes sense to you and change the extension from ".txt" to ".vbs". Double click on the file or type in the path to the file on the command line to launch it.
 
jges, you are the man! this working like a treat. Thanks so much for your help, much appreciated
 
I retested and didn't get the error.

What exactly is line 27 in your file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top