?
Solved

Simple copy in vbscript userprofile

Posted on 2013-12-18
7
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 56

Accepted Solution

by:
Bill Prew earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

752 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