Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Windows XP Windows XP
Search Search
Search for:
Tech Support Guy > > >

Batch file: how to insert desktop location into copy command


(!)

Diabo's Avatar
Diabo Diabo is offline
Junior Member with 9 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Enough to be dangerous
28-Apr-2008, 07:07 PM #1
Batch file: how to insert desktop location into copy command
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?
gurutech's Avatar
Computer Specs
Member with 2,910 posts.
 
Join Date: Apr 2004
Location: Central NJ
Experience: Seasoned Professional
28-Apr-2008, 07:49 PM #2
"Desktop" is usually C:\Documents and Settings\username\desktop
Diabo's Avatar
Diabo Diabo is offline
Junior Member with 9 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Enough to be dangerous
28-Apr-2008, 08:34 PM #3
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?
devil_himself's Avatar
Member with 4,910 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
28-Apr-2008, 08:37 PM #4
Using Environment Variable

Echo %UserProfile%\Desktop
Diabo's Avatar
Diabo Diabo is offline
Junior Member with 9 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Enough to be dangerous
28-Apr-2008, 08:50 PM #5
%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.
managed's Avatar
managed   (Allan) managed is offline
Computer Specs
Member with 4,102 posts.
 
Join Date: May 2003
Location: Liverpool, UK
Experience: Advanced
28-Apr-2008, 09:02 PM #6
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.
Mosaic1's Avatar
Member with 7,486 posts.
 
Join Date: Aug 2001
28-Apr-2008, 09:05 PM #7
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.
Mosaic1's Avatar
Member with 7,486 posts.
 
Join Date: Aug 2001
28-Apr-2008, 09:10 PM #8
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"
Mosaic1's Avatar
Member with 7,486 posts.
 
Join Date: Aug 2001
28-Apr-2008, 09:17 PM #9
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
Diabo's Avatar
Diabo Diabo is offline
Junior Member with 9 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Enough to be dangerous
28-Apr-2008, 10:17 PM #10
Thanx Mosaic1! Your code works.

Regedit /e /a %temp%\ddesktopreg.txt
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\S hell 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?
Mosaic1's Avatar
Member with 7,486 posts.
 
Join Date: Aug 2001
01-May-2008, 05:37 PM #11
Diabo,

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

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

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.
managed's Avatar
managed   (Allan) managed is offline
Computer Specs
Member with 4,102 posts.
 
Join Date: May 2003
Location: Liverpool, UK
Experience: Advanced
01-May-2008, 05:54 PM #12
I was wrong, very nice solution.
Soundy's Avatar
Computer Specs
Member with 1,825 posts.
 
Join Date: Feb 2006
Location: The Pitt, BC
Experience: Omnigeek
01-May-2008, 06:16 PM #13
Quote:
Originally Posted by Diabo View Post
%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.
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.
Diabo's Avatar
Diabo Diabo is offline
Junior Member with 9 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Enough to be dangerous
01-May-2008, 10:11 PM #14
Thanx again, Mosaic1. "Desktop" not being localised makes things a lot easier.

Quote:
Originally Posted by Soundy View Post
short of a registry change, how would your average user locate his desktop somewhere else??
There's no need to edit the registry by hand. TweakUI, X-Setup, or any other registry tweaker will do.
Soundy's Avatar
Computer Specs
Member with 1,825 posts.
 
Join Date: Feb 2006
Location: The Pitt, BC
Experience: Omnigeek
01-May-2008, 11:20 PM #15
Still not something your average user is going to do.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑