Solved

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

Posted on 2011-03-23
5
2,510 Views
Last Modified: 2012-05-11
Hello, I have the following directory structure:

C:\
    - TestSource
           FileToBeCopied.xxx
    - TestDestination
    TestCopyFile.vbs
   
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!
0
Comment
Question by:RichardRiga
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:HyperBPP
ID: 35199785
0
 
LVL 6

Accepted Solution

by:
HyperBPP earned 500 total points
ID: 35199821
Here is an example of what you are trying to do:
http://www.sissongroup.com/Upload/Kidz-EEMC%20CMS800/Kidz-EEMC%20CMS800/COPYFILE.VBS


' 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 = ""
	Err.Clear
	
	params = split(argstring,chr(253))
	iArgCount = Ubound(params)
	
		if iArgCount > 0 then
			strSource = params(0)   
		else
			strSource = ""
			Err.raise 50001,"Copyfile.vbs","Missing Source File"
		end if	
		
		if iArgCount > 1 then
			strTarget =  params(1)
		else
			strTarget = "" 
			Err.raise 50002,"Copyfile.vbs","Missing Target File"
		end if
		
		if iArgCount > 2 then
			bOverwrite = ( params(2) = "1" )
		else
		    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
		else
			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	
	else
		strAns = 0   
	end if
	
	' Return value to hosting object	
	main =  strAns
	
		
end function

Open in new window

0
 
LVL 1

Author Comment

by:RichardRiga
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?
0
 
LVL 1

Author Comment

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

Author Closing Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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