Discussion:
Trying to copy files using variables - need help!
(too old to reply)
Charlie Kaiser
2004-02-17 05:51:19 UTC
Permalink
I'm relatively new to scripting, but I've been hitting the books pretty
hard. :-)
I'm working on an enterprise script, and have a problem that I can't seem to
find a solution for. Here's what I want the script to do...
1. Connect to AD and enumerate all computers, creating a textfile with all
the computer names.
2. Create a folder structure, with a separate folder named for each
computer. (root folder\serverA, rootfolder\serverB, etc...)
3. Read the textfile created in step 1, then connect to each computer in the
file, and search for all files on the computer with a particular extension.
4. Copy all the files found in step 3 to the appropriately named folder in
the folder structure created in step 2.
5. Move on to the next computer and repeat step 4.
6. Log the activity.

So far, I've been able to accomplish step 1 and 2 easily enough, and step 3
kinda works too. Step 4 is where I'm stuck. I'm trying to use the fileCopy
method, but I'm having trouble passing a variable based on the readline from
the textfile as a source location, as well as passing a variable based on
the computername as part of the destination path.
All the documentation I've been reading uses filenames and paths for the
fileCopy method. Am I looking at the wrong method to do this or do I just
not know how to get the variables right? I get a variety of errors,
depending on how I set up the script line that copies the files, but it's
always in the same spot. I'm NOT getting permission errors; It's syntax or
file not found errors.
I've attached the working code below; this will enumerate all the computers
and create the folder structure:

'Code follows:
Const ADS_SCOPE_SUBTREE = 2
Dim objOU
Dim objFileSystem
Dim objComputernames

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objComputernames = objFileSystem.createTextFile("C:\computernames.txt",
TRUE)


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name FROM 'LDAP://DC=eccad,DC=com' " &
"WHERE objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
objComputernames.WriteLine objRecordset.Fields(0).Value
objRecordSet.MoveNext
Loop
Wscript.Echo "Computers Enumerated"
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\computernames.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objFolder = objFSO2.CreateFolder( "C:\eccad computers\" &
StrComputer)
Next
Wscript.Echo "Folders Created"

'End of code


I've also been able to use the following code to successfully search a
computer for files with a particular extension, but so far have only been
able to make it wscript.echo the filename. I cannot figure out how to take
that information and pass it to the fileCopy method.

'code follows:
sComputer = "modoc.eccad.com"
wscript.echo "Checking: " & sComputer
set oFSO=createobject("scripting.filesystemobject")
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory",,48)
For Each objItem in colItems
'objItem will be c:\folder\folder\file
'needs converting to \\scomputer\c$\folder etc
sDrive=left(objItem.name,1)
sPath=mid(objItem.name,3) ' start after C:
sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
set oFolder=oFso.GetFolder(sFolder)
for each oFile in oFolder.files
sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
If instr("|.mdb|",sExt) and sExt<>"." Then
wscript.echo sFolder & oFile.name
'previous line gets me the file path and name, but I can't
'figure out how to write the next line to perform the copy action.
end if
next
Next
'End of code

Can anyone help guide me to a way to copy the files? I'll worry about
running this against more than one computer once I get past the file copy
hurdle. :-)
Thanks!
Charlie Kaiser
***@spamtrapessexcreditspamtrap.com
(remove spamtrap for valid email address)
Roland Hall
2004-02-17 10:47:02 UTC
Permalink
"Charlie Kaiser" wrote:
: for each oFile in oFolder.files
: sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
: If instr("|.mdb|",sExt) and sExt<>"." Then
: wscript.echo sFolder & oFile.name
: 'previous line gets me the file path and name, but I can't
: 'figure out how to write the next line to perform the copy action.
: end if
: next
: Next
: 'End of code
:
: Can anyone help guide me to a way to copy the files? I'll worry about
: running this against more than one computer once I get past the file copy
: hurdle. :-)

object.CopyFile(source, destination[, overwrite])

oFso.CopyFile "c:\source\folder\*.mdb", "c:\target\folder"

oFso.CopyFile "" & sFolder & oFile.Name & "", "" & tFolder & ""

Where is your target folder?
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Charlie Kaiser
2004-02-17 14:13:37 UTC
Permalink
The problem with this is that I can't hard code "c:\source\folder\*.mdb",
because the source and folder names (as well as the target folder) will
change with each file found in the previous section. What I need is for a
variable representing the results found by the second code snippet to be
passed to the copyFile (or some other) method as the source argument, and
for a variable representing the location of the folder created in the first
code snippet to be passed to copyFile (or some other) method as the target
argument.
Does that help?
Thanks!
Charlie
Post by Roland Hall
: Can anyone help guide me to a way to copy the files? I'll worry about
: running this against more than one computer once I get past the file copy
: hurdle. :-)
object.CopyFile(source, destination[, overwrite])
oFso.CopyFile "c:\source\folder\*.mdb", "c:\target\folder"
oFso.CopyFile "" & sFolder & oFile.Name & "", "" & tFolder & ""
Where is your target folder?
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
Post by Roland Hall
MSDN Library - http://msdn.microsoft.com/library/default.asp
Roland Hall
2004-02-17 22:30:41 UTC
Permalink
"Charlie Kaiser" wrote:
: The problem with this is that I can't hard code "c:\source\folder\*.mdb",
: because the source and folder names (as well as the target folder) will
: change with each file found in the previous section. What I need is for a
: variable representing the results found by the second code snippet to be
: passed to the copyFile (or some other) method as the source argument, and
: for a variable representing the location of the folder created in the
first
: code snippet to be passed to copyFile (or some other) method as the target
: argument.
: Does that help?
: Thanks!
: Charlie

Syntax:
object.CopyFile(source, destination[, overwrite])

Values:
oFso.CopyFile "c:\source\folder\*.mdb", "c:\target\folder"

Variables:
oFso.CopyFile "" & sFolder & oFile.Name & "", "" & tFolder & ""

The Syntax and Values are only a visual. You would use the Variables. I
asked where the target folder was because you do not appear to have a
variable for that. I used oFso as your ActiveX object variable.

BTW... The 2nd link in my tag would be helpful to have. You can download
the WSH 5.6 Documentation. It will give you jscript/vbscript examples for
almost everything. Some are language specific. This is where I got the
syntax information from.

HTH...
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Charlie Kaiser
2004-02-18 18:30:24 UTC
Permalink
OK, I see what you're saying. I'm still having trouble making it work,
though. I've pasted the latest version below. When I run the script, it
echos the correct path and filename twice (I changed the script to create a
variable for the source file based on different parts of the name), but
still says "file not found".
I've tried playing with the syntax of the copy line but always get the same
problem. Changed the quotes, parentheses, etc., but no joy. Even if I don't
use the variable objFoundFile, and just use the "objItem.name & ofile.name"
variable, it still gives me the file not found.
If I hard-code the source and target filenames, it copies fine. I've
hard-coded the target to eliminate that as an error source. Any ideas why I
get the file not found error?
Thanks!
Charlie

'Code Follows:
sComputer = "modoc.eccad.com"
wscript.echo "Checking: " & sComputer
Set oFSO=createobject("scripting.filesystemobject")
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory",,48)
For Each objItem in colItems
'objItem will be c:\folder\folder\file
'needs converting to \\scomputer\c$\folder etc
sDrive=left(objItem.name,1)
sPath=mid(objItem.name,3) ' start after C:
sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
set oFolder=oFso.GetFolder(sFolder)
for each oFile in oFolder.files
sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
if instr("|.mdb|",sExt) and sExt<>"." Then
wscript.echo objItem.name & ofile.name
'Let's try something different. Create a variable to represent the source
filename:
objFoundFile=objItem.name & ofile.name
'then let's try and use that variable in the copyFile method,
'checking for a proper echo first:
wscript.echo objFoundFile
oFso.CopyFile objFoundFile, "c:\eccad computers\modoc\"
end if
next
Next
wscript.Echo "Done"

'End Code
Post by Roland Hall
object.CopyFile(source, destination[, overwrite])
oFso.CopyFile "c:\source\folder\*.mdb", "c:\target\folder"
oFso.CopyFile "" & sFolder & oFile.Name & "", "" & tFolder & ""
The Syntax and Values are only a visual. You would use the Variables. I
asked where the target folder was because you do not appear to have a
variable for that. I used oFso as your ActiveX object variable.
Roland Hall
2004-02-20 03:50:13 UTC
Permalink
"Charlie Kaiser" wrote:
: OK, I see what you're saying. I'm still having trouble making it work,
: though. I've pasted the latest version below. When I run the script, it
: echos the correct path and filename twice (I changed the script to create
a
: variable for the source file based on different parts of the name), but
: still says "file not found".
: I've tried playing with the syntax of the copy line but always get the
same
: problem. Changed the quotes, parentheses, etc., but no joy. Even if I
don't
: use the variable objFoundFile, and just use the "objItem.name &
ofile.name"
: variable, it still gives me the file not found.
: If I hard-code the source and target filenames, it copies fine. I've
: hard-coded the target to eliminate that as an error source. Any ideas why
I
: get the file not found error?
: Thanks!
: Charlie
:
: 'Code Follows:
: sComputer = "modoc.eccad.com"
: wscript.echo "Checking: " & sComputer
: Set oFSO=createobject("scripting.filesystemobject")
: Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
: Set colItems = objWMIService.ExecQuery("Select * from
Win32_Directory",,48)
: For Each objItem in colItems
: 'objItem will be c:\folder\folder\file
: 'needs converting to \\scomputer\c$\folder etc
: sDrive=left(objItem.name,1)
: sPath=mid(objItem.name,3) ' start after C:
: sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
: set oFolder=oFso.GetFolder(sFolder)
: for each oFile in oFolder.files
: sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
: if instr("|.mdb|",sExt) and sExt<>"." Then
: wscript.echo objItem.name & ofile.name
: 'Let's try something different. Create a variable to represent the
source
: filename:
: objFoundFile=objItem.name & ofile.name
: 'then let's try and use that variable in the copyFile method,
: 'checking for a proper echo first:
: wscript.echo objFoundFile
: oFso.CopyFile objFoundFile, "c:\eccad computers\modoc\"
: end if
: next
: Next
: wscript.Echo "Done"
:
: 'End Code

I think you get it because when you get to a folder past the root, you need
to append \ to the end of that folder name.
If you have: c:\folder
...and the file is a.mdb
Currently you get: c:\foldera.mdb
That is why you get file not found.

And, if the folder has a space in it, you will get that error.

Try this:

'Code Follows:
dim sComputer, sDrive, sPath, sFolder, tFolder, oFSO, objWMIService,
colItems, objItem, oFile, sExt
sComputer = "modoc.eccad.com"
tFolder = "c:\folder\folder\file\"
wscript.echo "Checking: " & sComputer
Set oFSO=createobject("scripting.filesystemobject")
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory",,48)

For Each objItem in colItems
sDrive=left(objItem.name,1)
sPath=mid(objItem.name,3) ' start after C:
sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
set oFolder=oFSO.GetFolder(sFolder)

for each oFile in oFolder.files
sExt=lcase("." & oFSO.GetExtensionName(sFolder & oFile.name))
if instr(".mdb",sExt) and sExt<>"." Then
if right(objItem.name, 1) <> "\" Then
objFoundFile=objItem.name & "\" & oFile.name
else
objFoundFile=objItem.name & oFile.name
end if
wscript.echo "Copying " & objFoundFile & " to " & tFolder
oFSO.CopyFile "" & objFoundFile & "", tFolder, true
end if
next
Next
wscript.Echo "Done"
set oFSO = nothing
set objWMIService = nothing
set colItems = nothing
set oFolder = nothing

'End Code
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Torgeir Bakken (MVP)
2004-02-20 03:27:54 UTC
Permalink
Post by Roland Hall
oFSO.CopyFile "" & objFoundFile & "", tFolder, true
The "Scripting.FileSystemObject" do not have an issue with paths with
spaces, it handles that internally.

So this will work fine even if there is spaces in the path:

oFSO.CopyFile objFoundFile, tFolder, true


Anyway, to get one quote inside a quoted string, you need to use two quotes
to get one effective, so if you had needed it, this would be correct:

oFSO.CopyFile """" & objFoundFile & """", tFolder, true

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter
Roland Hall
2004-02-21 09:08:29 UTC
Permalink
"Torgeir Bakken (MVP)" wrote:
: Roland Hall wrote:
:
: > oFSO.CopyFile "" & objFoundFile & "", tFolder, true
:
: The "Scripting.FileSystemObject" do not have an issue with paths with
: spaces, it handles that internally.
:
: So this will work fine even if there is spaces in the path:
:
: oFSO.CopyFile objFoundFile, tFolder, true
:
:
: Anyway, to get one quote inside a quoted string, you need to use two
quotes
: to get one effective, so if you had needed it, this would be correct:
:
: oFSO.CopyFile """" & objFoundFile & """", tFolder, true

That would explain why I never saw it. Thanks for pointing that out and I
was not aware of the space issue. The main issue was the \ at the end but I
think I made two changes at once.
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Continue reading on narkive:
Loading...