none
Why am I not getting Desktop

    Question

  • I'm trying to learn scripting and wrote a script to back my files up to a USB drive.  The following gets everything except the Desktop.  The computer is a Windows 7 machine.  Suggestions would be appreciated.

    ~~~~~~~~~

    Set objShellVars=CreateObject("WScript.Shell")
    Set objShell = CreateObject("Shell.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    strUserProfile = objShellVars.ExpandEnvironmentStrings("%userprofile%")
    strUserName = objShellVars.ExpandEnvironmentStrings("%username%")
    strAppData = objShellVars.ExpandEnvironmentStrings("%appData%")

    strDriveLetter = inputbox("Drive letter to copy to (USB key or mapped network drive)", "Copy to drive")

    strCopyHere = strDriveLetter & ":\Backups"  

    if not objFSO.FolderExists(strCopyHere) then
       objFSO.CreateFolder(strCopyHere)
    end if

    strCopyHere = strCopyHere  & "\" & strUserName
    objFSO.CreateFolder(strCopyHere)

    Set objFolder = objShell.NameSpace(strCopyHere)
    objFolder.CopyHere strUserProfile & "\Documents", &H10&
    objFolder.CopyHere strUserProfile & "\Desktop", &H10&      'this line does not work
    objFolder.CopyHere strUserProfile & "\Favorites", &H10&
    objFolder.CopyHere strUserProfile & "\Music", &H10&
    objFolder.CopyHere strUserProfile & "\Pictures", &H10&
    objFolder.CopyHere strUserProfile & "\Videos", &H10&
    objFolder.CopyHere strAppData & "\Microsoft\Signatures", &H10&


     

      

    Friday, March 02, 2012 10:07 PM

Answers

  • If you are on Vista or WIn7 that won't work.

    You need to use shell folders to copy the corect items especially if yur network has relocated your desktop to then network.

    Set objShell = CreateObject("WScript.Shell")
    sDesktop = objShell.SpecialFolders("Desktop")

    Here is how to list the available folders on any system.

    Set objShell = CreateObject("WScript.Shell")
    for each f in objShell.SpecialFolders
        WScript.Echo f
    Next


    ¯\_(ツ)_/¯

    Friday, March 02, 2012 10:41 PM
  • Yeah, there's gotta be a better way, but I'm doing what I can to help the users get the files backed up.  Maybe we can have something in place at a corporate level someday.  'preciate your help and comments.


    Roam the profiles an back them up centrally.  I have done this for years and it saves a lot of headaches.

    ¯\_(ツ)_/¯

    Monday, March 05, 2012 8:47 PM

All replies

  • If you are on Vista or WIn7 that won't work.

    You need to use shell folders to copy the corect items especially if yur network has relocated your desktop to then network.

    Set objShell = CreateObject("WScript.Shell")
    sDesktop = objShell.SpecialFolders("Desktop")

    Here is how to list the available folders on any system.

    Set objShell = CreateObject("WScript.Shell")
    for each f in objShell.SpecialFolders
        WScript.Echo f
    Next


    ¯\_(ツ)_/¯

    Friday, March 02, 2012 10:41 PM
  • Thanks for the response.  I'm wanting a progress bar so the uses will know something is happening.  And yes, I know the desktop shouldn't have much to worry about, but you wouldn't believe what some of my users will store on the desktop. 
    Monday, March 05, 2012 1:45 PM
  • For basic backup purposes I prefer to use robocopy. It pretty much does what you want and can be called from batch, VBscript and PowerShell. If you split the job up per folder you can output the progress to the console, here is an example in batch:

    @echo off
    echo Copying Desktop
    robocopy %userprofile%\Desktop H:\Backup\Desktop /copy:dat /r:1 /w:1 /zb /e /np /log:H:\Backup\Desktop.log
    echo Copying Pictures
    robocopy %userprofile%\Pictures H:\Backup\Pictures  /copy:dat /r:1 /w:1 /zb /e /np /log:H:\Backup\Pictures.log
    echo Copying Documents
    ...

    Monday, March 05, 2012 2:08 PM
  • Robocopy was mentioned when I asked about progress bars, but I didn't realize what it was run from a command line.  I"ve done backups in the past with xcopy using the /A and /M parameter.  Robocopy is interesting.  thx 

    Monday, March 05, 2012 2:38 PM
  • You can also retrive the path to desktop from the registry.

    Set objShellVars=CreateObject("WScript.Shell")
    strDesktop = objShellVars.RegRead _
        ("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
    strDesktop = objShellVars.ExpandEnvironmentStrings(strDesktop)
    Wscript.Echo strDesktop 

    Monday, March 05, 2012 4:32 PM
  • You can also retrive the path to desktop from the registry.

    Set objShellVars=CreateObject("WScript.Shell")
    strDesktop = objShellVars.RegRead _
        ("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
    strDesktop = objShellVars.ExpandEnvironmentStrings(strDesktop)
    Wscript.Echo strDesktop 

    This is easier:

    Set objShell = CreateObject("WScript.Shell")
    strDeskTop = objShell.SpecialFolders("desktop")



    ¯\_(ツ)_/¯

    Monday, March 05, 2012 4:50 PM
  • You can also retrive the path to desktop from the registry.

    Set objShellVars=CreateObject("WScript.Shell")
    strDesktop = objShellVars.RegRead _
        ("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
    strDesktop = objShellVars.ExpandEnvironmentStrings(strDesktop)
    Wscript.Echo strDesktop 


    On my computer, this returns "%USERPROFILE%\Desktop", which is what I was trying to use.
    Monday, March 05, 2012 4:54 PM
  • You can also retrive the path to desktop from the registry.

    Set objShellVars=CreateObject("WScript.Shell")
    strDesktop = objShellVars.RegRead _
        ("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
    strDesktop = objShellVars.ExpandEnvironmentStrings(strDesktop)
    Wscript.Echo strDesktop 


    On my computer, this returns "%USERPROFILE%\Desktop", which is what I was trying to use.

    If it does return that then your system is broken.

    This: objShellVars.ExpandEnvironmentStrings(strDesktop)

    Will expand the strings and replce the values.  If it doesn't then you may have accidently deleted some of you environment variables.

    This:

    Set objShell = CreateObject("WScript.Shell")
    strDeskTop = objShell.SpecialFolders("desktop")

    Should always return the desktop on any OS.


    ¯\_(ツ)_/¯

    Monday, March 05, 2012 5:05 PM
  • Again, thanks.  It does return c:\users\<myname>\desktop on the computer I'm on.  Unfortunately, I don't have another Win7 machine to try it on here.  I'll check my two Win7 computers at home and see what they return. 

    If my system's broken, it doesn't seem to know it.  It's truckin' right along ;o)

    Monday, March 05, 2012 5:14 PM
  • Again, thanks.  It does return c:\users\<myname>\desktop on the computer I'm on.  Unfortunately, I don't have another Win7 machine to try it on here.  I'll check my two Win7 computers at home and see what they return. 

    If my system's broken, it doesn't seem to know it.  It's truckin' right along ;o)

    You are being completely unclear.  WHat code retrurns thay result.

    You just posted that it returns -> "%USERPROFILE%\Desktop.

    The code you refrenced cannot return that if your environment is intact.

    XP/Win7 will return completel different locations for the profile.  The deskto pcan be redirected in a  netword and the registry will not return the correct value in many cases.  The shel specialfolders is guaranteed by Microsoft to always return the correct value in all situations.  That is why it is included in teh shell API.


    ¯\_(ツ)_/¯

    Monday, March 05, 2012 5:21 PM
  • Sorry.  When I check that field with RegEdit, I see "%USERPROFILE%\Desktop."  I didn't mean that it literally returns the environment variable.

    The code you posted returns "C:\Users\<myname>\Desktop."  <myname>, is of course, my user name.

    Monday, March 05, 2012 5:34 PM
  • Sorry.  When I check that field with RegEdit, I see "%USERPROFILE%\Desktop."  I didn't mean that it literally returns the environment variable.

    The code you posted returns "C:\Users\<myname>\Desktop."  <myname>, is of course, my user name.

    See.  YOu need to be clear.  YOu said teh code returns that strin when you meant that you looked at teh registry with regisit and saw that string.

    ExpandEnvironmentStrings will always replace the %...% stuff with the value.

    Using shell folders is much easier and more reliable overall.  That is why it is provided.


    ¯\_(ツ)_/¯

    Monday, March 05, 2012 5:37 PM
  • I changed the code to use your suggestion for the desktop location, and I'm still not getting the desktop.  I can use "%USERPROFILE%\Desktop" with robocopy, and it copies it fine.  Xcopy understands it fine.  I can Start, Run, "%USERPROFILE%\Desktop", and it jumps right to it.  The echo immediately above the line verifies I'm getting the proper path.  But the script refuses to copy it; it's as if it just ignores that line.

    I may just go with Robocopy for now, but I'd like to let the users choose their backup drive letters, decide whether to copy over their old backups, select a folder for the backups, etc.

    very similar script works flawlessly with all my XP users.  Maybe that's not so odd.  I sure appreciate your time and suggestions, though.

    ~~~~~~~~~~~~~~~~~~

    Set objShell = CreateObject("WScript.Shell")
    Set objShellApp = CreateObject("Shell.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    strUserProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    strUserName = objShell.ExpandEnvironmentStrings("%username%")
    strAppData = objShell.ExpandEnvironmentStrings("%appData%")

    strDesktop = objShell.SpecialFolders("desktop")

    strDriveLetter = inputbox("Drive letter to copy to (USB key or mapped network drive)", "Copy to drive")

    strCopyHere = strDriveLetter & ":\Backups"  

    if not objFSO.FolderExists(strCopyHere) then
       objFSO.CreateFolder(strCopyHere)
    end if

    strCopyHere = strCopyHere  & "\" & strUserName

    objFSO.CreateFolder(strCopyHere)

    Set objFolder = objShellApp.NameSpace(strCopyHere)

    objFolder.CopyHere strUserProfile & "\Documents", &H10&

    wscript.echo strDesktop

    objFolder.CopyHere strDesktop, &H10&

    objFolder.CopyHere strUserProfile & "\Favorites", &H10&
    objFolder.CopyHere strUserProfile & "\Music", &H10&
    objFolder.CopyHere strUserProfile & "\Pictures", &H10&
    objFolder.CopyHere strUserProfile & "\Videos", &H10&
    objFolder.CopyHere strAppData & "\Microsoft\Signatures", &H10&

    Monday, March 05, 2012 6:39 PM
  • objFolder.CopyHere strDesktop, &H10&

    What is that line of code trying to do?

    Why are you using the shell when the FSO works better?

    objFSO.CreateFolder  strCopyHere & "\Desktop"
    objFso.CopyFolder strDesktop& "\*.*", strCopyHere & "\Desktop"

    The problem with the shell copy  methods is that they are asynch and return before the copy is cpomplete.  If the copy is not finished it will be terminated when teh script completes even if the copy is not finished.

    You will never get errors on shell copy.  Vscript has no mechanism to interecept the errors.  That is why we mostly use the FSO.

    Write a simple scrit that does one thing only and that is to copy the desktop to another location using the FSo and prove to yourself that it works as a simple copy.


    ¯\_(ツ)_/¯

    Monday, March 05, 2012 6:54 PM
  • That line of code is trying to copy the desktop.  I works on everything before and after, but not the desktop.

    I used FSO on previous scripts that I used on my XP machines.  But some of my users store Gigs of info on their hard drives and the scripts may run for several minutes trying to copy it all.  It drives 'em nuts to see nothing happening.   I added an "I'm finished" message at the end of the script, but they'd still convince themselves that nothing was going on after a couple of minutes. 

    Monday, March 05, 2012 7:15 PM
  • That line of code is trying to copy the desktop.  I works on everything before and after, but not the desktop.

    I used FSO on previous scripts that I used on my XP machines.  But some of my users store Gigs of info on their hard drives and the scripts may run for several minutes trying to copy it all.  It drives 'em nuts to see nothing happening.   I added an "I'm finished" message at the end of the script, but they'd still convince themselves that nothing was going on after a couple of minutes. 

    I don't know about your user problems.  I do know that you will have issues with shell. 

    I cannot believe the users have gigs on their desktop.  If you really want to do this completely perfectly you will need to do it with a compiled program.

    Of course you can just put a big long WScript.Sleep 120000 at the end of the file to keep the file from closing of you can sit in a loop and poll the shell object to see when the count of files reaches the number that are to be copied.


    ¯\_(ツ)_/¯

    Monday, March 05, 2012 8:34 PM
  • Yeah, there's gotta be a better way, but I'm doing what I can to help the users get the files backed up.  Maybe we can have something in place at a corporate level someday.  'preciate your help and comments.

    Monday, March 05, 2012 8:41 PM
  • Yeah, there's gotta be a better way, but I'm doing what I can to help the users get the files backed up.  Maybe we can have something in place at a corporate level someday.  'preciate your help and comments.


    Roam the profiles an back them up centrally.  I have done this for years and it saves a lot of headaches.

    ¯\_(ツ)_/¯

    Monday, March 05, 2012 8:47 PM