Solved

Simple copy in vbscript userprofile

Posted on 2013-12-18
7
289 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
Comment Utility
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
Comment Utility
Thanks but I get an error "Expected ')', code 800A03EE ....
0
 
LVL 5

Expert Comment

by:usslindstrom
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Doesn't work for me, error on line 5,
0
 
LVL 5

Expert Comment

by:usslindstrom
Comment Utility
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
Comment Utility
Excellent, works perfectly!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

12 Experts available now in Live!

Get 1:1 Help Now