Joe
Wed May 04 18:57:13 CDT 2005
Hi,
"jp" <paulm###kcbbs.gen.nz> wrote in message
news:g8mi71t2hr4u1r22m5j4pv9k4uelf3t307@4ax.com...
> Being a novice can you explain or outline for me the Fs Fn parameters
> (fs As Object, fn As String)
>
> and where you get the values for
>
> myPath = fs.GetParentFolderName(fn)
> myFile = fs.GetBasename(fn)
> myExt = fs.GetExtensionName(fn)
>
> as I would like to test this but am unable to get my head around how
> to make it work.
>
> TIA.
>
>
> jpm
>
>
>
> On Tue, 03 May 2005 23:33:47 +1000, Michael Bednarek
> <ROT13-zo@zorqanerx.pbz> wrote:
>
>>On Tue, 3 May 2005 05:31:43 -0500, "Tony LaPaso" <tlapaso@comcast.net>
>>wrote in microsoft.public.scripting.vbscript:
>>
>>>Hi All,
>>>
>>>I have a need to copy a file w/WSH. If the destination file of the copy
>>>operation already exists I do *not* want to overwrite it but I don't
>>>want the copy operation to fail either. Instead, I want the behavior
>>>we've all seen in Windows. I want the destination file to be named,
>>>"Copy of xxx", where "xxx" is the name of the existing file. If "Copy
>>>of xxx" already exists then I want the destination named, "Copy (2) of
>>>xxx", etc.
>>>
>>>I could write code myself to check if the destination file already
>>>exists and then adjust my filename appropriately. But I wonder if
>>>there's an object available to WSH that will automatically name the
>>>files using the "Copy of..." pattern, or will have to code this logic
>>>myself Is there an existing mechanism?
>>>
>>>Thanks very much.
>>
>>Here's a piece of VBA code I wrote some time ago; I haven't clapped eyes
>>on it in 3 years, but it should give you at least a starting point. It
>>does not do "Copy (2) of" but merely appends " (2)" to the filename.
>>
>>----------------------------------------------------------------------
>>Function IncrementFilename(fs As Object, fn As String) As String
>>
>>' Increment (or add) the "(nn") part at the end of a filename.
>>
>>Dim nLeft As Long, n As Long, bTwoB As Boolean, strNum As String
>>Dim myPath As String, myFile As String, myExt As String, myfn As String
>>
>>myPath = fs.GetParentFolderName(fn)
>>myFile = fs.GetBasename(fn)
>>myExt = fs.GetExtensionName(fn)
>>
>>bTwoB = False
>>nLeft = Len(myFile) ' In case there is no "(nn)"
>>strNum = " (1"
>>If Right(myFile, 1) = ")" Then ' Is there a ")" at the end?
>> n = Len(myFile) - 1
>> For nLeft = n To 1 Step -1
>> If Mid(myFile, nLeft, 1) = "(" Then ' Search for "("
>> bTwoB = True
>> Exit For
>> End If
>> Next nLeft
>> If bTwoB Then ' Found a "(" ?
>> strNum = Mid(myFile, nLeft + 1, n - nLeft)
>> If IsNumeric(strNum) Then
>> strNum = Format(Val(strNum) + 1)
>> End If
>> End If
>>End If
>>myfn = myPath & "\" & Left(myFile, nLeft) & strNum & ")." & myExt
>>Do While fs.FileExists(myfn)
>> myfn = IncrementFilename(fs, myfn)
>>Loop
>>IncrementFilename = myfn
>>End Function
>>----------------------------------------------------------------------
I can see where this would be confusing, if you're new to VBS, since the
function that the OP was referencing was an old VB (VisualBasic) function --
not a VBScript (VBS) function, even though he was looking for a scripting
solution.
VBS has all variant variables - no hard-typed variables. The statements
like
>>Function IncrementFilename(fs As Object, fn As String) As String
and
>>Dim nLeft As Long, n As Long, bTwoB As Boolean, strNum As String
>>Dim myPath As String, myFile As String, myExt As String, myfn As String
are hard-typed variable types for VB. You can do this in VB, but not in
VBS. In VBS, you simply declare the variable name, so the above lines would
be:
>>Function IncrementFilename(fs, fn)
and
>>Dim nLeft, n, bTwoB, strNum
>>Dim myPath, myFile, myExt, myfn
Though the Op did not comment on the arguments for his function,
fs is obviously the FileSystemObject. He is creating the object in global
script, as in
set fs= createObject("Scripting.FileSystemObject")
and then passing it into the function through an argument. The same with
fn, which is apparently the file pathname string for the file that he wants
to increment.
The following lines use methods of the fs object instance
> myPath = fs.GetParentFolderName(fn)
> myFile = fs.GetBasename(fn)
> myExt = fs.GetExtensionName(fn)
If you're just starting at VBS, get the MS CHM file documentation, and take
a look at the MS Script Center for a huge number of admin and tutorial
scripts, and maybe a site like Joe'Software. (these will line wrap)
MS WSH CHM File Script Documentation
http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en
MS Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.mspx
Joe'Software
http://www.jsware.net/jsware/boss.html
Joe Earnest