1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch file: how to insert desktop location into copy command

Discussion in 'Windows XP' started by Diabo, Apr 28, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Diabo

    Diabo Thread Starter

    Joined:
    May 8, 2007
    Messages:
    9
    I'm trying to write a .bat that's supposed to handle the very simple task of copying a file to the desktop.

    Something like:

    copy x:\folder\file.ext [desktop]\file_backup.ext

    The problem: [desktop].

    It would be really easy if I could simply insert %desktop% the same way as %windir% etc. Unfortunately, %desktop% doesn't exist.

    Using the variable for userprofile doesn't work either, because the user may have moved the desktop folder out of the docs & sets folder to any other location, and not necessarily on the same partition or drive.

    It would be easy in a .vbs, but that won't work if the windows script host is locked down or disabled.

    So it has to be a batch file. Does anyone know how to explain a .bat how to find the desktop?
     
  2. gurutech

    gurutech

    Joined:
    Apr 23, 2004
    Messages:
    2,959
    "Desktop" is usually C:\Documents and Settings\username\desktop
     
  3. Diabo

    Diabo Thread Starter

    Joined:
    May 8, 2007
    Messages:
    9
    Thanks for the quick response, but I'm not looking for the "usual" location. I'm after the real location instead.

    If you value your data, your desktop folder will be a safe distance away from your system partition. If windows is on partition C, the desktop folder of my target audience is almost certainly not on C. It's gonna be on another drive, or at least on another partition. It may even be on a network drive in another room or building.

    So what I'm looking for is a way to feed the location of the current user's desktop folder into the batch file, no matter where that desktop folder has been moved to.

    REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Desktop
    returns the current desktop folder location. Is there a way to insert the output into either the COPY or SET command of a batch file?
     
  4. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    Using Environment Variable

    Echo %UserProfile%\Desktop
     
  5. Diabo

    Diabo Thread Starter

    Joined:
    May 8, 2007
    Messages:
    9
    %userprofile%\desktop is just another way to address the default desktop folder in the current user's docs & sets folder, so that won't do it either. The desktop folder may be located anywhere, and it may have any name.
     
  6. managed

    managed Allan

    Joined:
    May 24, 2003
    Messages:
    5,359
    If you don't know a Folder's name there's no way you can refer to it, in a Batch file or anywhere else.
     
  7. Mosaic1

    Mosaic1

    Joined:
    Aug 17, 2001
    Messages:
    7,486
    Is this going to be used only for English Windows versions? Or for others. This will work for XP and 2k. you'llk have to test on Vista yourself if using that version.

    You can get the desktop location from the registry for the Current User here:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

    Export the key to a text file using regedit /a and then parse the file to get the path to the desktop. Do you know how to do that?

    Like this example: (create a file C:\whatever it is.txt)
    Code:
    Regedit /e /a dd.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    
    find /i  dd.txt  "Desktop" >d.txt
    
    For /F  "tokens=2 delims==" %%a in (d.txt) do copy "C:\whatever it is.txt" %%a 
    We could use reg.exe if you had XP. But regedit exists on XP and 2k natively.

    Let me know how you do with this.
     
  8. Mosaic1

    Mosaic1

    Joined:
    Aug 17, 2001
    Messages:
    7,486
    And you'll add the cleanup for the files created here. Note there are no quotation marks around %%a in the copy command.

    That's because the data you have retreived looks like this example:
    "D:\\Documents and Settings\\Katie\\Desktop"
     
  9. Mosaic1

    Mosaic1

    Joined:
    Aug 17, 2001
    Messages:
    7,486
    If you want to use the path more than once, try this instead to create %mydesktop% for the command session. This used the echo and pause rather than the copy command for this example.
    Code:
    Regedit /e /a dd.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    find /i  dd.txt  "Desktop" >d.txt
     For /F  "tokens=2 delims==" %%a in (d.txt) do set mydesktop=%%a
    Echo %mydesktop% 
    Pause
    
     
  10. Diabo

    Diabo Thread Starter

    Joined:
    May 8, 2007
    Messages:
    9
    Thanx Mosaic1! Your code works.

    Regedit /e /a %temp%\ddesktopreg.txt
    "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    find /i %temp%\ddesktopreg.txt "Desktop" >%temp%\desktopreg.txt
    For /F "tokens=2 delims==" %%a in (%temp%\desktopreg.txt) do set desktopfolder=%%a
    DEL %temp%\ddesktopreg.txt
    DEL %temp%\desktopreg.txt
    COPY x:\folder\subfolder\file.ext %desktopfolder%\copy_of_file.ext


    Are the string names in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders always in english or are the string names localised?
     
  11. Mosaic1

    Mosaic1

    Joined:
    Aug 17, 2001
    Messages:
    7,486
    Diabo,

    You're welcome. Sorry it took ame a couple days to get back. I've been offline.


    Yes. The value name should be Desktop, and the data will be the actual name and path, which is localized. This technique is used by many of the antispyware utilities to get the location of the various shell folders.
     
  12. managed

    managed Allan

    Joined:
    May 24, 2003
    Messages:
    5,359
    I was wrong, very nice solution.
     
  13. Soundy

    Soundy

    Joined:
    Feb 17, 2006
    Messages:
    1,834
    I've never seen a user's Desktop folder that wasn't within that user's profile. "My Documents" is readily pointed somewhere else, but short of a registry change, how would your average user locate his desktop somewhere else??

    Of course, you can always just go to "%systemdrive%\Documents and Settings\All Users\Desktop" if it's something you want all users to see/access.
     
  14. Diabo

    Diabo Thread Starter

    Joined:
    May 8, 2007
    Messages:
    9
    Thanx again, Mosaic1. "Desktop" not being localised makes things a lot easier.

    There's no need to edit the registry by hand. TweakUI, X-Setup, or any other registry tweaker will do.
     
  15. Soundy

    Soundy

    Joined:
    Feb 17, 2006
    Messages:
    1,834
    Still not something your average user is going to do.
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/708164