Script - Abbrev Folder Path for My Documents

Posted on 2009-02-23
Last Modified: 2012-05-06
I'm trying write a script that will copy files into users' My Documents folders, but the only path I know to get to My Documents requires the user name: C:\Documents and Settings\orenb000\My Documents\.  Is there a path I can use that could get me to My Documents without knowing the user name?
Question by:humbleamateur
    LVL 16

    Expert Comment

    How's about "%userprofile%\My Documents" ?
    LVL 2

    Expert Comment

    %username%\my documents
    LVL 16

    Expert Comment

    %username%  only gets you (eg)  "cantoris" whereas %userprofile% gets you "c:\documents and settings\cantoris"

    Author Comment

    Hmmm....I don't know what I'm doing wrong.  I tried both of these out in the attached code and got "Path Not Found" errors both times.  Is there something wrong with my code?  Thanks for all your help!

    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile "C:\test.txt", "%userprofile%\My Documents\test.txt"
    fs.CopyFile "C:\test.txt", "C:\Documents and Settings\%username%\My Documents\test.txt"

    Open in new window

    LVL 16

    Accepted Solution

    You can't use environment variables directly in VBScript like that - you can in a batch file though.

    You will need to use the WScript Shell object to expand them into their actual strings:

    Set objShell = CreateObject( "WScript.Shell" )

    Author Closing Comment

    It worked perfectly!  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
    This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now