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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question?

Status
Not open for further replies.

ekbranson

Programmer
Jan 12, 2004
52
US
Hello, I was putting together something I was going to use for a type of backup routine when I came across something I think is odd. I was curious if any one might be able to explain vbscript scope to me. If you look at the attached code I have remarked my question. Basically when I call this function the function modifies my tempSource with a trailing \.

Code:
option explicit

dim tempSource
dim tempDest
dim tempPath 
dim tempFolder
dim e, t, s

tempSource = "C:\Test"
tempDest = "Y:\Transfer"
tempFolder = "TempFolder"
t = tempSource & "\" & tempFolder
s = tempSource & "\" & tempFolder

msgbox t

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'some how this line modifies t and returns the modifiction.  In other words if 
't is set above but is returned with a trailing \.  

e = Copy_AcftRecords (tempSource, t , "*.doc") 
msgbox s
msgbox t





function Copy_AcftRecords(source, destination, fileExt)
	dim objFSO
	dim overwrite 

	overwrite = true

	set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FolderExists( source ) and objFSO.FolderExists( destination ) then

		if right(destination, 1) <> "\" then
			destination = destination & "\"
		end if

		if right(source, 1) <> "\" then
			source = source & "\"
		end if

		source = source & fileExt

		objFSO.CopyFile source, destination, overwrite
		Copy_AcftRecords = true
	end if
	Copy_AcftRecords = False
end function
 
When I run your code, none of the paths have a trailing \ in the MsgBoxs.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The third message box should contain a \. I just cut and pasted the cut back into a test .vbs file and it still gives me the trailing \.

I get "C:\Test\TestFolder" in the first two and then "C:\Test\TestFolder\"

 
I don't know what to say, I copy and pasted the code you posted and it didn't have a slash. I get:
C:\Test\TempFolder
C:\Test\TempFolder
C:\Test\TempFolder

Notice that I am getting C:\Test\TempFolder where you say you get C:\Test\TestFolder. Are you running the same code as what you posted?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Yeah, I am cutting and pasting the code I placed on the web site. I put it in a completly new script file. That has me completly puzzled. If im not mistaken there is no odd scope in each function. In other words, each function should contain its on variables and not share them. Write? I will try one more thing, I want to see what happens if I create another script in a seperate folder. I will post my results.
 
Ok it still gives me a trailing \. Any ideas?
 
I am completely baffled by this one. If you can copy and paste the code from the box above and get a completely different result than I do, then I have no idea. Let me try one more thing though.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Ok, I ran it using both CScript.exe and WScript.exe. I didn't think there would be a difference and there wasn't. I still don't get a trailing slash. What version of WSH are you using?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I have moved the script to another computer and you are right no trailing \. The exact same script. I have no idea why it would do this.
 
Ok, I moved the exact same script to another computer and you are right. No trailing \. I have no clue why one computer would do this and another one would not. Both computer have WinXp, AMD processors. The only difference is this one is a laptop and the other one is a pc.
 
Did you determine you WSH version yet?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
That was my last idea. No chance that someone changed one or the other while you weren't lookig? :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I don't know what the problem is but is baffeling. Just some food for thought, I compiled in Visual Studio and I still get the same thing. Thanks for your help.
 
Maybe one of the real experts will wander along and point out something stupifyingly simple that we overlooked.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
e = Copy_AcftRecords (tempSource, t , "*.doc")
function Copy_AcftRecords(source, destination, fileExt)
destination = destination & "\"

If destination is a ByRef parameter then t is normally modified.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You know I have never had a problem without declaring the ByVal or ByRef in vbscript. I would have never thought of that. Thanks alot.

Here is how I modified the above code.
Code:
option explicit

dim tempSource
dim tempDest
dim tempPath 
dim tempFolder
dim e, t, s

tempSource = "C:\Test"
tempDest = "Y:\Transfer"
tempFolder = "TempFolder"
t = tempSource & "\" & tempFolder
s = tempSource & "\" & tempFolder
e = Create_ArchiveFolder(tempSource,tempFolder)
msgbox t

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'some how this line modifies t and returns the modifiction.  In other words if 
't is set above but is returned with a trailing \.  

e = Copy_AcftRecords (tempSource, t , "*.doc") 
msgbox s
msgbox t
e = Move_ArchiveFolder( s, tempDest )



function Create_ArchiveFolder(path,folder)

	dim objFSO 
	dim strFullPath

	strFullPath = path & "\" & folder
	set objFSO = CreateObject("Scripting.FileSystemObject")

	if objFSO.FolderExists( strFullPath ) then
		Create_ArchiveFolder = false
		exit function
	else
		objFSO.CreateFolder( strFullPath )
		Create_ArchiveFolder = true
	end if

end function

function Copy_AcftRecords(source, ByVal destination , fileExt)
	dim objFSO
	dim overwrite 

	overwrite = true

	set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FolderExists( source ) and objFSO.FolderExists( destination ) then

		if right(destination, 1) <> "\" then
			destination = destination & "\"
		end if

		if right(source, 1) <> "\" then
			source = source & "\"
		end if

		source = source & fileExt

		objFSO.CopyFile source, destination, overwrite
		Copy_AcftRecords = true
	end if
	Copy_AcftRecords = False
end function

function Move_ArchiveFolder(source, destination)
	dim objFSO
	dim overwrite 

	overwrite = true
	
	set objFSO = CreateObject("Scripting.FileSystemObject")
	'objFSO.CopyFolder source, destination, overwrite
	'objFSO.DeleteFolder( source )

	Move_ArchiveFolder = true
end function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top