search and copy a folder using vbscript

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
totalyscrewedup
Posts: 6
Joined: Tue Aug 13, 2013 11:58 am

search and copy a folder using vbscript

Post by totalyscrewedup » Tue Aug 13, 2013 12:06 pm

I'm a newbie to scripting in general and after 3 weeks learning this is what I came up with to do this:

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory Where FileName = 'MaxMc' and drive = 'c:'")

For Each objFolder in colFolders
errResult = objFolder.CopyEx("C:\Program Files",,,True)
wscript.echo errResult
Next

'after I run this script in Primal, I get following result:'
'10'
'exit code: 0 , 000h'
'and of course, the files have not been copied nor is the folder created'
What am I doing wrong? (By the way, I created the folder on local disc just for testing with that name)

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: search and copy a folder using vbscript

Post by jvierra » Tue Aug 13, 2013 12:15 pm

Start here: http://msdn.microsoft.com/en-us/library ... s.85).aspx
Study the return codes.

User avatar
totalyscrewedup
Posts: 6
Joined: Tue Aug 13, 2013 11:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup » Tue Aug 13, 2013 1:01 pm

Based on that, I'm not able to copy using VMI services since I'm looking through the subfolders of the same path as I'm copying too (in reality, I'm actually copying from 5 subfolders deep and copying 2 subfolders deep).
I verified that the folder with that name doesn't exist in c:\program files since it actually resides under c:\program files\folderx\subfoldery\subfolderz\subfolderxyz.
Is my conclusion correct because the article provided in that link states the error code 10 stands that object already exists?

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: search and copy a folder using vbscript

Post by jvierra » Tue Aug 13, 2013 1:08 pm

Your use of the command is a bit odd.

What is in the path name of the folder?

You also will need to be running elevated to copy to that location.

User avatar
totalyscrewedup
Posts: 6
Joined: Tue Aug 13, 2013 11:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup » Tue Aug 13, 2013 1:20 pm

the whole setup is odd since we have have multiple hurdles to overcome. The first one is that the application is not an executable but rather combination of different sets of dlls that need to be copied into c:\windows, c:\windows\system32 and few more locations. That application is tied to the different shares and servers and all predispositions are set in stone so to say.
Having said that, our platform used for software delivery has designated location to which it downloads the package to locally but the last sub folder before reaching the package changes constantly. Hence the "hunt" for it using wmi and trying to copy it to correct location.
Long story short, i have to copy a folder having specific name from a path which has a sub folder that is constantly changed (if I push it to 2 different computers, that sub folder will have a different name which has no pattern to go by) to the "c:\program files\"

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: search and copy a folder using vbscript

Post by jvierra » Tue Aug 13, 2013 1:25 pm

The CopyEx requires a target folder that does not exist.

In your case it requires the following:

errResult = objFolder.CopyEx("C:\\Program Files\\MaxMc",,,True)
Notice the double slashes.
Last edited by jvierra on Tue Aug 13, 2013 1:31 pm, edited 1 time in total.

User avatar
totalyscrewedup
Posts: 6
Joined: Tue Aug 13, 2013 11:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup » Tue Aug 13, 2013 1:31 pm

Thank you so much! I did try double slashes in the past but failed to end the final path sub folder same as the folder being copied.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: search and copy a folder using vbscript

Post by jvierra » Tue Aug 13, 2013 1:33 pm

totalyscrewedup wrote:Thank you so much! I did try double slashes in the past but failed to end the final path sub folder same as the folder being copied.
You need to be sure that there is only one folder being returned.

The following works without error except if there is more than one object in the collection.
VBScript Code
Double-click the code block to select all.
For Each objFolder in colFolders
    
    WScript.Echo objFolder.Path
    
    errResult = objFolder.CopyEx("C:\\scripts\\test44",,,True)
	wscript.echo errResult
	
Next

User avatar
Alexander Riedel
Posts: 6973
Joined: Tue May 29, 2007 4:43 pm

Re: search and copy a folder using vbscript

Post by Alexander Riedel » Tue Aug 13, 2013 1:50 pm

The prescribed way to copy files to "Program Files" and/or system 32 is to create an installer. From what you describe this this is some kind of software roll out.
If I where you, I would look at our MSIWizard here http://www.sapien.com/blog/2013/06/05/i ... si-wizard/ and create an installer for whatever you have to roll out.

If you need more control over the installer, look at http://www.advancedinstaller.com/

Unless there is a really good reason to use VBScript for this it seems that you are just using the wrong tool for the job :)
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
totalyscrewedup
Posts: 6
Joined: Tue Aug 13, 2013 11:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup » Thu Aug 15, 2013 8:08 am

Jvierra, that is where I actually stared my script from to make it work.
We have this folder only when it's created this way. Thanks for the warning.

Alex,

we are poor and can't afford software. Besides, how will I learn if I don't do things the hard way?

On another note, one of the parts of the script I can't get to work on Windows 7 devices is shortcut creation.
I'm getting error 2 (Access denied)
This is what I have so far and it works on xp machines as well as running it under my credentials but when pushed through the platform, I get the access denied error.
Here is the script part:
'create shortcut for Windows 7 & XP'

Set WshShell = WScript.CreateObject("wscript.shell")
strDesktop = WshShell.SpecialFolders("AllUsersDesktop")
Set oShellLink = WshShell.CreateShortcut (strDesktop & "\MaxMc.lnk")
oShellLink.TargetPath = "C:\program files\MaxMc_10\MaxMC_1001\maxmc.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "C:\program files\MaxMc_10\MaxMC_1001\maxmc.exe"
oShellLink.Description = "Launch Max Mc"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save

Locked