Copying a file through an unknown directory to known directory

• Question

• Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\temp\abc\123.xml")
objFile.Copy "c:\programdata\ABC\DEF\XXXXXX\JKL\"

I am using the above in a *.VBS to copy 123.xml to the \JKL\ directory - however xxxxxx is an unknown variable: this directory could be called anything.

How can I also get 123.xml into the right directory everytime?

Wednesday, September 10, 2014 6:35 AM

• 1. Your script returns the folder name the required folder.

2. c:\programdata\ABC\DEF\XXXXXX\JKL

objFile.Copy "c:\programdata\ABC\DEF\XXXXXX\JKL\"

Your question was: How to work out the value of XXXXX? My sample script gave you that value. You said so yourself. You now need to plug it into your script line. If this sounds puzzling then here it is explicitly:

Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oFolder = ofso.GetFolder("c:\programdata\ABC\DEF")
For Each oSubfolder In ofolder.SubFolders
sDest = oSubfolder.Path
Next
objFile.Copy sDest

• Marked as answer by Thursday, September 11, 2014 6:02 AM
Wednesday, September 10, 2014 7:30 PM

All replies

• Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\temp\abc\123.xml")
objFile.Copy "c:\programdata\ABC\DEF\XXXXXX\JKL\"

I am using the above in a *.VBS to copy 123.xml to the \JKL\ directory - however xxxxxx is an unknown variable: this directory could be called anything.

How can I also get 123.xml into the right directory everytime?

How does the script know which one is the correct directory? Could it be that xxxx is the only directory that contains a JKL subdirectory? If so then you need to examine every subdirectory of DEF until you find one that contains JKL.

Wednesday, September 10, 2014 7:01 AM
• Sorry maybe I was not clear.

The path is always c:\programdata\ABC\DEF\NAME OF DATASET\JKL so iI know every directory except the italics which could change.

However there will always only be one Name of dataset folder - I just don't know what it would be called.

Hope this clears it up...

Wednesday, September 10, 2014 7:04 AM
• It's still the same: You must enumerate the subfolder(s) of DEF, e.g. like so:

Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oFolder = ofso.GetFolder("c:\programdata\ABC\DEF")
For Each oSubfolder In ofolder.SubFolders
WScript.Echo oSubfolder.Path
Next

Wednesday, September 10, 2014 7:35 AM
• Many, many thanks for that but I am still unsure.

Now the folder is enumerated do I just use the

Set objFile = objFSO.GetFile("c:\temp\abc\123.xml")

objFile.Copy "how do I tell it to use correct sub folderr"

Wednesday, September 10, 2014 9:35 AM
• You have to enumerate two levels and match on the target folder name.  When you have that then yo have the correct folder object.

¯\_(ツ)_/¯

Wednesday, September 10, 2014 10:39 AM
• For fun consider the following:

"c:\programdata\ABC\DATASET1\JKL\"
"c:\programdata\ABC\DATASET2\JKL\"
"c:\programdata\ABC\DATASET3\JKL\"
"c:\programdata\ABC\DATASET4\JKL\"

In this case you will need a psychic to know the folder you require.

¯\_(ツ)_/¯

Wednesday, September 10, 2014 11:35 AM
• Many, many thanks for that but I am still unsure.

Now the folder is enumerated do I just use the

Set objFile = objFSO.GetFile("c:\temp\abc\123.xml")

objFile.Copy "how do I tell it to use correct sub folderr"

I gave you this script:

Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oFolder = ofso.GetFolder("c:\programdata\ABC\DEF")
For Each oSubfolder In ofolder.SubFolders
WScript.Echo oSubfolder.Path
Next

Questions:

1. What exactly does the script return?
2. What is the target folder for your copy action?
3. What exactly is the item "c:\temp\abc\123.xml" that you quote in your reply?
4. What is the purpose of the getfile method you use in your script?

Wednesday, September 10, 2014 4:09 PM
• Frederik - I do not think the OP knows anything about scripting or computer technology. My post is a probe like yours to get more infor.

This , also, can solve some of the ambiguity.

Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oFolder = ofso.GetFolder("c:\programdata\ABC\DEF")
For Each folder In ofolder.SubFolders
For Each f In folder.SubFolders
If "XYZ" = UCase(f.Name) Then
WScript.Echo "Folder found:" & f.Path
End If
Next
Next

I suspect that there will be more than one folder found.

¯\_(ツ)_/¯

Wednesday, September 10, 2014 4:23 PM
• Frederik - I do not think the OP knows anything about scripting or computer technology. My post is a probe like yours to get more infor.

I see. This might explain the puzzling items in his reply.
Wednesday, September 10, 2014 5:27 PM
• 1. Your script returns the folder name the required folder.

2. c:\programdata\ABC\DEF\XXXXXX\JKL

3. Not being facetious but it is an *.XML called 123

4. I (probably mistakenly) was trying to use the getfile to specify the file I wanted to copy from the temp location.

I am sorry for attempting to resolve an issue myself and learn by trial and error - my script does exactly what I need so far and, although it may not be the prettiest and most exact, at least I have given it a go and learnt a lot in the process.

This part - of placing the *.xml in a semi 'wildcarded' location is proving beyond me however.

Your script, which you kindly gave me does the exact first part I require and defines the 'unknown folder' - I just can't seem to piece copy the *.xml to that location - I will keep trying however!

• Edited by Wednesday, September 10, 2014 7:17 PM Typo
Wednesday, September 10, 2014 7:12 PM
• Frederik - I do not think the OP knows anything about scripting or computer technology. My post is a probe like yours to get more infor.

Little harsh? I'm learning.

However - I really appreciate your reply/script and, just like Frederik, it has certainly given me something to get stuck into and attempt to resolve my problem.

Thank you very much for your time.

I could post my whole script if you really wanted something to laugh at - personally I am quite proud of what I have done so far...

Wednesday, September 10, 2014 7:15 PM
• Hi,

Since you're just learning, I highly recommend skipping VBScript and learning PowerShell instead. VBScript still certainly has its uses, but PowerShell is the future (and the present, really).

http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

Wednesday, September 10, 2014 7:20 PM
• 1. Your script returns the folder name the required folder.

2. c:\programdata\ABC\DEF\XXXXXX\JKL

objFile.Copy "c:\programdata\ABC\DEF\XXXXXX\JKL\"

Your question was: How to work out the value of XXXXX? My sample script gave you that value. You said so yourself. You now need to plug it into your script line. If this sounds puzzling then here it is explicitly:

Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oFolder = ofso.GetFolder("c:\programdata\ABC\DEF")
For Each oSubfolder In ofolder.SubFolders
sDest = oSubfolder.Path
Next
objFile.Copy sDest

• Marked as answer by Thursday, September 11, 2014 6:02 AM
Wednesday, September 10, 2014 7:30 PM
• Frederik - I do not think the OP knows anything about scripting or computer technology. My post is a probe like yours to get more infor.

Little harsh? I'm learning.

However - I really appreciate your reply/script and, just like Frederik, it has certainly given me something to get stuck into and attempt to resolve my problem.

Thank you very much for your time.

I could post my whole script if you really wanted something to laugh at - personally I am quite proud of what I have done so far...

Not being harsh.  You don't seem to know anything about scripting or computer file systems so I pointed it out to note that you would not likely understand techy answers.  I posted the script to force you to look a method to see if we could decode what you were asking.

You cannot use wildcards in VBScript.

If you do not know this you should not be using VBScript as it is well on to being obsolete.  Learn PowerShell it is easier and can use wild cards.

Get-ChildItem _path c:\abc\def\xyz -include *.xml

¯\_(ツ)_/¯

Wednesday, September 10, 2014 7:47 PM
• I could post my whole script if you really wanted something to laugh at - personally I am quite proud of what I have done so far...

Not an issue.  Early scripts are usually quite ugly.  I only recommend that you should be  spending your time on PowerShell and not old deprecated script languages.

http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

¯\_(ツ)_/¯

Wednesday, September 10, 2014 7:49 PM
• Many thanks again Frederik
Thursday, September 11, 2014 6:02 AM