Solved

Simple copy in vbscript userprofile

Posted on 2013-12-18
7
293 Views
Last Modified: 2013-12-19
Hi,

Cannot seem to make a copy of some folder with files & robocopy with vbscript:


This is the script:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("robocopy ""E:\Program Files\Myprogram"" "("%USERPROFILE%")\AppData\Roaming\Microsoft\Word" *.* /E /IS")

Not sure howto treat the %USERPROFILE%. Please advise.
J.
0
Comment
Question by:janhoedt
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:usslindstrom
ID: 39726507
Are you expanding the environment variable first?

Set objShell = CreateObject( "WScript.Shell" )
strUserProfileDir = objShell.ExpandEnvironmentStrings( "%USERPROFILE%" )
objShell.Run("robocopy ""E:\Program Files\Myprogram"" "(" & """" & strUserProfileDir & """" &")\AppData\Roaming\Microsoft\Word" *.* /E /IS")
Set objShell = Nothing


*Untested, but should point you in the right direction for using environment variables.

http://www.robvanderwoude.com/vbstech_data_environment.php
0
 

Author Comment

by:janhoedt
ID: 39726559
Thanks but I get an error "Expected ')', code 800A03EE ....
0
 
LVL 5

Expert Comment

by:usslindstrom
ID: 39726583
Try this:

Set objShell = CreateObject( "WScript.Shell" )
strUserProfileDir = objShell.ExpandEnvironmentStrings( "%USERPROFILE%" )
strCopyCmd = "robocopy ""E:\Program Files\myprogram"" (""" & strUserProfileDir & """\AppData\Roaming\Microsoft\Word"" *.* /E /IS)"
objShell.Run(strCopyCmd)
Set objShell = Nothing
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39727048
Here is how I usually approach these.  I use a small function to add double quotes around a string when needed to keep things cleaner. I also don't think you want the parens in the robocopy command line.  Try this:

Set objShell = CreateObject("WScript.Shell")
strUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
Wscript.Echo "[" & strUserProfile & "]"
strCopyCmd = "robocopy " & Quote("E:\Program Files\myprogram") & " " & Quote(strUserProfile & "\AppData\Roaming\Microsoft\Word") & " *.* /E /IS"
objShell.Run(strCopyCmd)
Set objShell = Nothing 

' Small routine to add double quotes around a string
Function Quote(strText)
   Quote = Chr(34) & strText & Chr(34)
End Function

Open in new window

~bp
0
 

Author Comment

by:janhoedt
ID: 39727130
Doesn't work for me, error on line 5,
0
 
LVL 5

Expert Comment

by:usslindstrom
ID: 39727596
BP - long time no see.  Excellent idea on the "Quote" function.  Hadn't thought of that.

janhoedt - Using BP's code, it tested fine for me here...  It launched a second command shell window and processed the robocopy command perfectly (pic attached).

Line 5 is running the robocopy command.  Add "WScript.Echo strCopyCmd" right before line 5 and return the results.  Your %userprofile% environment string might contain some bad chars or something.
EE-RoboCopy-VBS.png
0
 

Author Closing Comment

by:janhoedt
ID: 39728596
Excellent, works perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now