Solved

Simple copy in vbscript userprofile

Posted on 2013-12-18
7
305 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 54

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to count occurrences of each item in an array.
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.

733 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