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.
This topic is 10 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
totalyscrewedup
Posts: 6
Last visit: Wed Aug 21, 2013 6:58 am

search and copy a folder using vbscript

Post by totalyscrewedup »

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)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: search and copy a folder using vbscript

Post by jvierra »

Start here: http://msdn.microsoft.com/en-us/library ... s.85).aspx
Study the return codes.
User avatar
totalyscrewedup
Posts: 6
Last visit: Wed Aug 21, 2013 6:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: search and copy a folder using vbscript

Post by jvierra »

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
Last visit: Wed Aug 21, 2013 6:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup »

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\"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: search and copy a folder using vbscript

Post by jvierra »

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
Last visit: Wed Aug 21, 2013 6:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: search and copy a folder using vbscript

Post by jvierra »

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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: search and copy a folder using vbscript

Post by Alexander Riedel »

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
Last visit: Wed Aug 21, 2013 6:58 am

Re: search and copy a folder using vbscript

Post by totalyscrewedup »

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
This topic is 10 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked