What woudl i modify in the following script to copy the current directory?
I am thinking that I would change the c:\\scipts to some sort of dot slash
but im not sure of the syntax.
Thanks!
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery( _
"Select * from Win32_Directory where Name = 'c:\\Scripts'")
For Each objFolder in colFolders
errResults = objFolder.Copy("D:\Archive")
Wscript.Echo errResults
Next
--
Ron G. Williams
MCSA, MCSE+Security, MCDBA
Have you considered using a humble line of batch code?
@echo off
xcopy /c /d /y *.* d:\Archive\
There appears to be a contradiction between your aim and
what your VB Script does. Your stated aim is to copy files
yet your script copies folders.
Ron, if you have the 'Scripting.FileSystemObject' object instantiated, you
can use the 'GetParentFolderName' method:
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sCurDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
oFSO.CopyFile sCurDir & "\*.*", "C:\New Folder", True
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can optionally combine this with the 'CurrentDirectory' method from
'WScript.Shell' to set your base directory to the directory your script is
located in:
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
oFSO.CopyFile "*.*", "C:\New Folder", True
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What if i wanted to copy specific files from the directory where the running
script resides?
For example, within the same folder as install.vbs, i want to copy
file1.txt, file2.txt etc etc
how do i reference that in the script? would it look like this:
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
oFSO.CopyFile "file1.txt", "C:\New Folder", True
oFSO.CopyFile "file2.txt", "C:\New Folder", True
oFSO.CopyFile "file3.txt", "C:\New Folder", True
--
--
Ron G. Williams
MCSA, MCSE+Security, MCDBA
oFSO.CopyFile CurrentDirectory & "\file1.txt", "C:\New Folder\File1.txt",
True
oFSO.CopyFile CurrentDirectory & "\file2.txt", "C:\New Folder\File2.txt",
True
oFSO.CopyFile CurrentDirectory & "\file3.txt", "C:\New Folder\File3.txt",
True
"RonWilliams" <RonWi...@discussions.microsoft.com> wrote in message
news:E5CBDDA2-0A2B-4B92...@microsoft.com...
That should work perfectly with one minor change. Put a backslash at the
end of your folder designation to specify it is a folder and not a file you
are copying to. Without this small distinction, the end result is that you
would be either a 'Permission denied' error message if the folder exists or
a file named 'New Folder' with the contents of the last file copied (since
the overwrite switch is True) if it does not.
oFSO.CopyFile "file1.txt", "C:\New Folder\", True
If you have more than a few files, you might want to consider using a
wildcard (ex: oFSO.CopyFile "file*.txt", "C:\New Folder\", True) if they
have similar names, using a delimited string or building an array. The
delimited string could look something like the code below. I used a colon as
the delimiter.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
sFileList = "file1.txt:file2.txt:file3.txt:file4.txt"
For Each sFile in Split(sFileList, ":")
oFSO.CopyFile sFile, "r:\New Folder\", True
Next
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"James Whitlow" <jwhitlow...@bloglines.com> wrote in message
news:OKVyr50t...@TK2MSFTNGP06.phx.gbl...
> "RonWilliams" <RonWi...@discussions.microsoft.com> wrote in message
> news:E5CBDDA2-0A2B-4B92...@microsoft.com...
>> Thanks James, that was a LOT of help. THANKS!
>>
>> What if i wanted to copy specific files from the directory where the
>> running
>> script resides?
>> For example, within the same folder as install.vbs, i want to copy
>> file1.txt, file2.txt etc etc
>>
>> how do i reference that in the script? would it look like this:
>>
>> Set oWSH = CreateObject("WScript.Shell")
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>>
>> oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
>>
>> oFSO.CopyFile "file1.txt", "C:\New Folder", True
>> oFSO.CopyFile "file2.txt", "C:\New Folder", True
>> oFSO.CopyFile "file3.txt", "C:\New Folder", True
>
> That should work perfectly with one minor change. Put a backslash at
> the end of your folder designation to specify it is a folder and not a
> file you are copying to.
*** I think this is a feature of xcopy.exe. When I tested your
*** command
*** oFSO.CopyFile "file1.txt", "C:\New Folder\", True
*** it failed consistently.
***
*** Furthermore the code picks up "file1.txt" located in the
*** current directory, which is not necessarily the same as
*** the directory where the script resides. The OP's first post
*** is somewhat ambiguous on this point.
> Without this small distinction, the end result is that you would be either
> a 'Permission denied' error message if the folder exists or a file named
> 'New Folder' with the contents of the last file copied (since the
> overwrite switch is True) if it does not.
>
> oFSO.CopyFile "file1.txt", "C:\New Folder\", True
>
> If you have more than a few files, you might want to consider using a
> wildcard (ex: oFSO.CopyFile "file*.txt", "C:\New Folder\", True) if they
> have similar names, using a delimited string or building an array. The
> delimited string could look something like the code below. I used a colon
> as the delimiter.
>
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Set oWSH = CreateObject("WScript.Shell")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
> sFileList = "file1.txt:file2.txt:file3.txt:file4.txt"
> For Each sFile in Split(sFileList, ":")
> oFSO.CopyFile sFile, "r:\New Folder\", True
> Next
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** Your code includes a hard-coded collection of file names
*** but I'm unable to detect a wildcard. Where is it?
The example using the wildcard is in parenthesis in the first paragraph.
The lower block of code is an example using a delimited list in a string. It
does not contain a wildcard, but there is no reason it couldn't. Something
like: sFileList = "*.vbs:file2.txt:*.log:file4.txt"
There is actually a reason that the code cannot contain wildcards:
AFAIK, the FSO object does not understand them. "*" and "?"
wildcards are understood by the Command Processor only.
I tested the code sample that I posted on my Windows XP Pro with SP2 and
it worked with wildcards. I have not tested it on any other version of
Windows, so I cannot speak beyond XP SP2.
Can someone in the group that is using Vista or W2K please try using
FSO's 'CopyFile' method with wildcards post the results?
I found these two links on the subject:
http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_ytql.mspx
http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject_copyfile.html
I noted text in the second link that stated, "Note that if the source
does contain wildcards, it is automatically assumed that the destination is
an existing folder and any matching files are copied to it." I tested the
statement by specifying a wildcard in my source and removing the trailing
backslash from my destination and the copy was successful.
Thanks for the links - they clarify the issue.