Using VBScript and FileSystemObject, can I use relative paths to copy files from one folder to another?

Posted on 2011-03-23
Medium Priority
Last Modified: 2012-05-11
Hello, I have the following directory structure:

    - TestSource
    - TestDestination
I call the TestCopyFile.vbs with cscript from a DOS prompt and the .vbs contains the following:

dim objFSOPatch, testPath

tDest = ".\TestDestination\"

set objFSOPatch = CreateObject("Scripting.FileSystemObject")
objFSOPatch.CopyFile ".\TestSource\FileToBeCopied.xxx", tDest, True

set fso=Nothing

However, this does not appear to work.  Can I not use relative pathing for this?  Can someone make the change that will fix this for me, please?

Thank you!
Question by:RichardRiga
  • 3
  • 2

Accepted Solution

HyperBPP earned 2000 total points
ID: 35199821
Here is an example of what you are trying to do:

' CopyFile.vbs
' copy a file
' Can accept long filenames
' calling convention
'    copyfile.vbs "SourceFile" "TargetFile" Overwrite
'  sourcefile is fully qualified (can be unc, can be relative path, can use wildecards) file to copy from
'  targetfile is fully qualified (can be unc, can be relative path, can use wildcards) file to copy to
' Returns status via Oi_ComDlg
'   0 on success
'   err.number:@fm:err.description on error 
' 03-06-2001  rjc     Created
' 08-27-2001  rjc     Configure for 3.75  Expects to be hosted in vb script control, called with RUN srcipt,main
function main(argstring)
	dim ofs ' as Scripting.FileSystemObject
	Dim oFile 'as Fileobject
	dim strSource ' as string
	Dim strTarget ' as string
	Dim strTargetFolder ' as string
	dim bOverWrite  ' as boolean
	dim strAns ' as string
	dim iArgCount
	dim params
	'On Error Resume Next	
	strAns = ""
	params = split(argstring,chr(253))
	iArgCount = Ubound(params)
		if iArgCount > 0 then
			strSource = params(0)   
			strSource = ""
			Err.raise 50001,"Copyfile.vbs","Missing Source File"
		end if	
		if iArgCount > 1 then
			strTarget =  params(1)
			strTarget = "" 
			Err.raise 50002,"Copyfile.vbs","Missing Target File"
		end if
		if iArgCount > 2 then
			bOverwrite = ( params(2) = "1" )
		    bOverwrite = false
		end if
	set oFs = CreateObject("Scripting.FileSystemObject") 
	if Err.Number = 0 then
		if  oFs.FileExists(strSource) = false then
	  		Err.raise 50003,"Copyfile.vbs","Invalid Source File"
	  	end if	
	end if  
	' Simple check.  can be fooled
	if Err.Number = 0 then 
		'Target is a folder? make sure has trailing '\'	
		if ofs.FolderExists(strTarget) then
			if right(strTarget,1) <> "\" then
				strTarget = strTarget & "\"
			end if
			if InStrRev(strTarget, "\") then
	    		strTargetFolder = Left(strTarget,InStrRev(strTarget, "\")-1)
	    		if ofs.FolderExists(strTargetFolder) = False then
	    			Err.raise 50004,"Copyfile.vbs","Invalid Target Folder"
	    		end if
	    	end if
	    end if	
	end if
	'Perform the copy. Use file object, allows copy to directory without specifying filename
	if Err.Number =  0 then
	   set oFile = ofs.GetFile(	strSource )
	   ofile.Copy strTarget, bOverwrite
	   set oFile = Nothing
	end if
	' Clean Up 
	set ofs = Nothing 
	if err.Number <>  0 then
		strAns = Err.Number & chr(254) & Err.Description	
		strAns = 0   
	end if
	' Return value to hosting object	
	main =  strAns
end function

Open in new window


Author Comment

ID: 35199830
Hyper, that first link i'd already looked at and tried to emulate but haven't gotten to work.
The second one only mentions that it is possible to use a relative path but shows no example for it specifically.  What am I doing wrong in MY example?

Author Comment

ID: 35199834
lol ok i posted that last while you posted that second. i'll check it out now thanks

Author Closing Comment

ID: 35200057
Thanks so much, that helped a ton.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

607 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