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.

--
Tony LaPaso

Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Michael

Michael
Tue May 03 08:33:47 CDT 2005

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
----------------------------------------------------------------------

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Joe

Joe
Tue May 03 09:18:58 CDT 2005


"Tony LaPaso" <tlapaso@comcast.net> wrote in message
news:%231iTLt8TFHA.2388@TK2MSFTNGP10.phx.gbl...
> 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.
>
> --
> Tony LaPaso
>

In addition to rolling your own, there are the Shell Folder CopyHere and
MoveHere methods that are the standard Windows Explorer copying functions.
They do exactly what you want and display the Windows progress indicator for
long copies. They may not be suitable for admin scripts, because they popup
user and error dialogs and return no indications of user selection or
dismissal, or errors. Also, they are somewhat emasculated in Windows XP --
wildcards are no longer accepted; only specific files and all contents of
the folder may be copied. I have no experience with them on 2003. I have
seen posts indicating that they may shut down before extremely large files
are fully copied, but I've never experienced that, myself.

If you're still interested, the following is a standard schpeal on the
methods.

Joe Earnest

-----
[vbs]

set oFolder= ShellApp.nameSpace( _
target folder pathname string or
Shell system folder integer code )

oFolder.copyHere _
source file(s) or subfolder(s) _
[ , integer code ]
oFolder.moveHere _
source file(s) or subfolder(s) _
[ , integer code ]

The ShellApp Folder subobject's CopyHere and MoveHere method dialogs are the
standard Windows copy and move operations and dialogs. The dialogs are
modal for the duration of the process. In addition to pathname strings, the
methods will also accept a ShellApp Folder subobject's "Items" collection,
that operates on all of the files and subfolders in a given folder. On
Windows XP, at least, the dialogs no longer accept wildcards, and so operate
only for single file or folder specifications, or folder contents. Several
of the flags also fail to operate properly with Windows XP. The methods are
quite fast, overwrite files with hidden, system and read-only attributes,
and set the archive attribute on the target files copied. (Like Windows
Explorer, if hidden files are hidden, they are not copied and an explanatory
box pops up.) Because these methods return no operation flags, prompt the
user for overwriting and simply popup error conditions, the results cannot
always be ascertained without independent verification.

The following are the documented bitwise flags for dialog operation.

&H0004 4 Does not display a progress dialog. This option appears to
operate normally with Windows XP.

&H0008 8 Automatically renames the file(s) being copied, if a file of
the same name already exists in the target folder, using the "Copy (n) of "
prefix. This option appears to operate normally with Windows XP.

&H0010 16 Automatically responds with "Yes to All" to any confirmation
box. This option appears to operate normally with Windows XP.

&H0040 64 Preserves undo information if possible. This option does not
appear viable for script operation.

&H0080 128 Performs the operation on files only, if a filename mask is
specified. Because the methods do not appear to respond to wildcard mask
specifications in Windows XP, this option is not viable in Windows XP.

&H0100 256 Displays a progress dialog but does not display file names.
This option appears to operate normally with Windows XP.

&H0200 512 Automatically creates a new subfolder, if one is required,
without displaying a confirmation dialog. This option appears to be
implemented by default with Windows XP.

&H0400 1024 Does not display an informational dialog, if an error occurs.
This option appears to operate normally with Windows XP.

&H0800 2048 Does not copy file security attributes. Applies only to
version 4.71 or greater of the shell.

&H1000 4096 Suppresses any recursive subdirectory operation. The
operation is limited to the local source folder. This option effects
immediate single folder copying, if the folder has any subfolder content.
Because the methods do not appear to respond to wildcard mask specifications
in Windows XP, this option is not viable in Windows XP.

&H2000 8192 Only specified files are copied. Connected files are not
copied as a group. Applies only to version 5 of the shell.




Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Georges

Georges
Tue May 03 10:07:02 CDT 2005

Hello,
To summarize, you could use :

set oSh = CREATEOBJECT("Shell.Application")
oSh.NameSpace("C:\test").CopyHere "c:\temp\*.txt", 24

c:\test is the destination folder
c:\temp\*.txt is the files you want to copy

24 stand for : FOF_NOCONFIRMATION (16) + FOF_RENAMEONCOLLISION (8)

Regards
Georges MAUREL


"Joe Earnest" <jearnest3-SPAM@earthlink.net> a écrit dans le message de
news:ebBjrs%23TFHA.2820@tk2msftngp13.phx.gbl...
>
> "Tony LaPaso" <tlapaso@comcast.net> wrote in message
> news:%231iTLt8TFHA.2388@TK2MSFTNGP10.phx.gbl...
> > 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.
> >
> > --
> > Tony LaPaso
> >
>
> In addition to rolling your own, there are the Shell Folder CopyHere and
> MoveHere methods that are the standard Windows Explorer copying functions.
> They do exactly what you want and display the Windows progress indicator
for
> long copies. They may not be suitable for admin scripts, because they
popup
> user and error dialogs and return no indications of user selection or
> dismissal, or errors. Also, they are somewhat emasculated in Windows
XP --
> wildcards are no longer accepted; only specific files and all contents of
> the folder may be copied. I have no experience with them on 2003. I have
> seen posts indicating that they may shut down before extremely large files
> are fully copied, but I've never experienced that, myself.
>
> If you're still interested, the following is a standard schpeal on the
> methods.
>
> Joe Earnest
>
> -----
> [vbs]
>
> set oFolder= ShellApp.nameSpace( _
> target folder pathname string or
> Shell system folder integer code )
>
> oFolder.copyHere _
> source file(s) or subfolder(s) _
> [ , integer code ]
> oFolder.moveHere _
> source file(s) or subfolder(s) _
> [ , integer code ]
>
> The ShellApp Folder subobject's CopyHere and MoveHere method dialogs are
the
> standard Windows copy and move operations and dialogs. The dialogs are
> modal for the duration of the process. In addition to pathname strings,
the
> methods will also accept a ShellApp Folder subobject's "Items" collection,
> that operates on all of the files and subfolders in a given folder. On
> Windows XP, at least, the dialogs no longer accept wildcards, and so
operate
> only for single file or folder specifications, or folder contents.
Several
> of the flags also fail to operate properly with Windows XP. The methods
are
> quite fast, overwrite files with hidden, system and read-only attributes,
> and set the archive attribute on the target files copied. (Like Windows
> Explorer, if hidden files are hidden, they are not copied and an
explanatory
> box pops up.) Because these methods return no operation flags, prompt the
> user for overwriting and simply popup error conditions, the results cannot
> always be ascertained without independent verification.
>
> The following are the documented bitwise flags for dialog operation.
>
> &H0004 4 Does not display a progress dialog. This option appears to
> operate normally with Windows XP.
>
> &H0008 8 Automatically renames the file(s) being copied, if a file of
> the same name already exists in the target folder, using the "Copy (n) of
"
> prefix. This option appears to operate normally with Windows XP.
>
> &H0010 16 Automatically responds with "Yes to All" to any confirmation
> box. This option appears to operate normally with Windows XP.
>
> &H0040 64 Preserves undo information if possible. This option does
not
> appear viable for script operation.
>
> &H0080 128 Performs the operation on files only, if a filename mask is
> specified. Because the methods do not appear to respond to wildcard mask
> specifications in Windows XP, this option is not viable in Windows XP.
>
> &H0100 256 Displays a progress dialog but does not display file names.
> This option appears to operate normally with Windows XP.
>
> &H0200 512 Automatically creates a new subfolder, if one is required,
> without displaying a confirmation dialog. This option appears to be
> implemented by default with Windows XP.
>
> &H0400 1024 Does not display an informational dialog, if an error
occurs.
> This option appears to operate normally with Windows XP.
>
> &H0800 2048 Does not copy file security attributes. Applies only to
> version 4.71 or greater of the shell.
>
> &H1000 4096 Suppresses any recursive subdirectory operation. The
> operation is limited to the local source folder. This option effects
> immediate single folder copying, if the folder has any subfolder content.
> Because the methods do not appear to respond to wildcard mask
specifications
> in Windows XP, this option is not viable in Windows XP.
>
> &H2000 8192 Only specified files are copied. Connected files are not
> copied as a group. Applies only to version 5 of the shell.
>
>
>



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Joe

Joe
Tue May 03 10:29:13 CDT 2005

Hi,

[snipped]

"Georges MAUREL" <georges.maurel_at_free.fr> wrote in message
news:exgkHH$TFHA.3436@TK2MSFTNGP09.phx.gbl...
> Hello,
> To summarize, you could use :
>
> set oSh = CREATEOBJECT("Shell.Application")
> oSh.NameSpace("C:\test").CopyHere "c:\temp\*.txt", 24
>
> c:\test is the destination folder
> c:\temp\*.txt is the files you want to copy
>
> 24 stand for : FOF_NOCONFIRMATION (16) + FOF_RENAMEONCOLLISION (8)
>
> Regards
> Georges MAUREL

Thanks for catching the fact that I forgot to create the Shell.Application
object. Pulling from an old post ...

Unfortunately, the Op didn't state his OS (but he did say "a file" --
hopefully singular). On Windows XP (and later?) the asterisk wildcard in
your example will not be accepted. It's either all or singular. (You can
feed an otherwise generated array of singular file names in one at a time,
but that's it.) That was part of the reason for the long post.

Joe Earnest



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Georges

Georges
Tue May 03 10:41:45 CDT 2005

Hello Joe,
I just wanted to show a sample code running on windows 2000

Your post was very instructive and I didn't want to minimize your
contribution
I hope I didn't hurt you. If so, please forgive me

Regards
Georges MAUREL


"Joe Earnest" <jearnest3-SPAM@earthlink.net> a écrit dans le message de
news:uSk3DU$TFHA.228@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> [snipped]
>
> "Georges MAUREL" <georges.maurel_at_free.fr> wrote in message
> news:exgkHH$TFHA.3436@TK2MSFTNGP09.phx.gbl...
> > Hello,
> > To summarize, you could use :
> >
> > set oSh = CREATEOBJECT("Shell.Application")
> > oSh.NameSpace("C:\test").CopyHere "c:\temp\*.txt", 24
> >
> > c:\test is the destination folder
> > c:\temp\*.txt is the files you want to copy
> >
> > 24 stand for : FOF_NOCONFIRMATION (16) + FOF_RENAMEONCOLLISION (8)
> >
> > Regards
> > Georges MAUREL
>
> Thanks for catching the fact that I forgot to create the Shell.Application
> object. Pulling from an old post ...
>
> Unfortunately, the Op didn't state his OS (but he did say "a file" --
> hopefully singular). On Windows XP (and later?) the asterisk wildcard in
> your example will not be accepted. It's either all or singular. (You can
> feed an otherwise generated array of singular file names in one at a time,
> but that's it.) That was part of the reason for the long post.
>
> Joe Earnest
>
>



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Joe

Joe
Tue May 03 10:55:56 CDT 2005

Hi Georges,

"Georges MAUREL" <georges.maurel_at_free.fr> wrote in message
news:Onn$hb$TFHA.628@tk2msftngp13.phx.gbl...
> Hello Joe,
> I just wanted to show a sample code running on windows 2000
>
> Your post was very instructive and I didn't want to minimize your
> contribution
> I hope I didn't hurt you. If so, please forgive me
>
> Regards
> Georges MAUREL
>
>
> "Joe Earnest" <jearnest3-SPAM@earthlink.net> a écrit dans le message de
> news:uSk3DU$TFHA.228@TK2MSFTNGP12.phx.gbl...
>> Hi,
>>
>> [snipped]
>>
>> "Georges MAUREL" <georges.maurel_at_free.fr> wrote in message
>> news:exgkHH$TFHA.3436@TK2MSFTNGP09.phx.gbl...
>> > Hello,
>> > To summarize, you could use :
>> >
>> > set oSh = CREATEOBJECT("Shell.Application")
>> > oSh.NameSpace("C:\test").CopyHere "c:\temp\*.txt", 24
>> >
>> > c:\test is the destination folder
>> > c:\temp\*.txt is the files you want to copy
>> >
>> > 24 stand for : FOF_NOCONFIRMATION (16) + FOF_RENAMEONCOLLISION (8)
>> >
>> > Regards
>> > Georges MAUREL
>>
>> Thanks for catching the fact that I forgot to create the
>> Shell.Application
>> object. Pulling from an old post ...
>>
>> Unfortunately, the Op didn't state his OS (but he did say "a file" --
>> hopefully singular). On Windows XP (and later?) the asterisk wildcard in
>> your example will not be accepted. It's either all or singular. (You
>> can
>> feed an otherwise generated array of singular file names in one at a
>> time,
>> but that's it.) That was part of the reason for the long post.
>>
>> Joe Earnest

*Very* thoughtful reply, but not to worry -- I'm far more thick-skinned than
that. Your example was succinct, useful and to the point, and quite
appropriate, since I didn't give a succint example. Without knowing the
experience level of the Op, I was just concerned that some confusion might
result from the asterisked example. (Not everyone takes the time to read
the longer textual posts. :)

Regards,
Joe Earnest



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Tony

Tony
Tue May 03 14:33:21 CDT 2005

Thank you all for responding. Your responses are exactly what I was
looking for. Thanks very, very much. You've really helped.

Tony



"Tony LaPaso" <tlapaso@comcast.net> wrote in message
news:%231iTLt8TFHA.2388@TK2MSFTNGP10.phx.gbl...
> 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.
>
> --
> Tony LaPaso
>



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Al

Al
Tue May 03 22:53:56 CDT 2005

OK, hands up all those who wish that the default windows duplicate renaming
scheme started with "Copy (1) of" instead of "Copy of"? Or, better yet (and
more sortable), suffixed a sequence number field. Copying XXX to where there
already is an XXX would then give you "XXX_000001" and etc.


/Al

"Tony LaPaso" <tlapaso@comcast.net> wrote in message
news:OO$HzbBUFHA.752@TK2MSFTNGP10.phx.gbl...
> Thank you all for responding. Your responses are exactly what I was
> looking for. Thanks very, very much. You've really helped.
>
> Tony
>
>
>
> "Tony LaPaso" <tlapaso@comcast.net> wrote in message
> news:%231iTLt8TFHA.2388@TK2MSFTNGP10.phx.gbl...
> > 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.
> >
> > --
> > Tony LaPaso
> >
>
>



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Trevor

Trevor
Tue May 03 22:59:56 CDT 2005

Yep,

I agree with the last suggestion. There is one piece of software that does
that (XXX_000001), but I can't remember what it is

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au

Al Dunbar [MS-MVP] wrote:
> OK, hands up all those who wish that the default windows duplicate
> renaming scheme started with "Copy (1) of" instead of "Copy of"? Or,
> better yet (and more sortable), suffixed a sequence number field.
> Copying XXX to where there already is an XXX would then give you
> "XXX_000001" and etc.
>
>
> /Al
>
> "Tony LaPaso" <tlapaso@comcast.net> wrote in message
> news:OO$HzbBUFHA.752@TK2MSFTNGP10.phx.gbl...
>> Thank you all for responding. Your responses are exactly what I was
>> looking for. Thanks very, very much. You've really helped.
>>
>> Tony
>>
>>
>>
>> "Tony LaPaso" <tlapaso@comcast.net> wrote in message
>> news:%231iTLt8TFHA.2388@TK2MSFTNGP10.phx.gbl...
>>> 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.
>>>
>>> --
>>> Tony LaPaso


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html



Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by jp

jp
Wed May 04 18:25:25 CDT 2005

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
>----------------------------------------------------------------------


Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Joe

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




Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Michael

Michael
Wed May 04 21:08:04 CDT 2005

On Wed, 4 May 2005 17:57:13 -0600, "Joe Earnest"
<jearnest3-SPAM@earthlink.net> wrote in
microsoft.public.scripting.vbscript:

[snip]
>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.
[snip]

Very perceptive analysis, Joe. Thank you.

I was not aware of the OP's level at VBS coding and assumed that it
would not be too difficult to convert VBA code to VBS - I think one just
has to remove all the types from the Dim statements. Then again, seeing
the types as coded in VBA provides some clues.

I think I passed the FileSystem as an argument because I needed it
already in the calling routine, so passing it to the subroutine seemed
more efficient than creating a new instance. As a generic black-box type
of subroutine, this is probably not a good idea.

Looking back, I think the shell methods you suggested
(CopyHere/MoveHere) are much more elegant.

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Re: Copying Files to get "Copy of...", "Copy (2) of...", etc. by Joe

Joe
Thu May 05 08:13:21 CDT 2005

Hi,

"Michael Bednarek" <ROT13:zo@gtz.pbz.nh> wrote in message
news:l7vi71dab0s8mi5ab63742ps9p5rvaujmo@4ax.com...
> On Wed, 4 May 2005 17:57:13 -0600, "Joe Earnest"
> <jearnest3-SPAM@earthlink.net> wrote in
> microsoft.public.scripting.vbscript:
>
> [snip]
>>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.
> [snip]
>
> Very perceptive analysis, Joe. Thank you.
>
> I was not aware of the OP's level at VBS coding and assumed that it
> would not be too difficult to convert VBA code to VBS - I think one just
> has to remove all the types from the Dim statements. Then again, seeing
> the types as coded in VBA provides some clues.
>
> I think I passed the FileSystem as an argument because I needed it
> already in the calling routine, so passing it to the subroutine seemed
> more efficient than creating a new instance. As a generic black-box type
> of subroutine, this is probably not a good idea.
>
> Looking back, I think the shell methods you suggested
> (CopyHere/MoveHere) are much more elegant.
>
> --
> Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Sorry for the "OP" confusion. My thread on OE is apparently either
partially deleted or incomplete. I had a bad starting post and got turned
around on who asked the first question.

Regards,
Joe Earnest