Solved

Simple copy in vbscript userprofile

Posted on 2013-12-18
7
299 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 53

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

778 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