C:\Documents and Settings\Administrator\Desktop
I don't want to type the above each time to refer to a file on the desktop
C:\Documents and Settings\Administrator\Desktop
I don't want to type the above each time to refer to a file on the desktop
You can use "%USERPROFILE%\Desktop"
but I don't know from which version of Windows it is built in.
If your want the real folder where Desktop is located then use this code in the bach
for /F "skip=2 tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do set DesktopFolder="%%a"
This requires the reg.exe to be available (again, I don't know from which version of Window it is there) and it will set the DesktopFolder variable to the path of the Desktop.
%USERPROFILE%
is available already on XP (and probably earlier, too). –
Illegitimacy C:\Documents
–
Kimberliekimberlin Desktop
folder has another name. –
Acuminate %USERPROFILE%\Desktop
, because the path contains spaces. –
Acuminate File or directory "C:\Documents and Settings\Administrator\Desktop\logo1w.png" is not found
–
Kimberliekimberlin ls "%USERPROFILE%\Desktop"\logo1w.png
and ls "%USERPROFILE%\Desktop\logo1w.png",the file exists on desktop. –
Kimberliekimberlin The hybrid of Anders can be a bit more simple and readable, with the method described here hybrid scripting by Tom Lavedas.
@if (@X)==(@Y) @goto :Dummy @end/* Batch part
@echo off
SETLOCAL ENABLEEXTENSIONS
for /f "delims=" %%x in ('cscript //E:JScript //nologo "%~f0"') do set desk=%%x
echo desktop path is %desk%
goto :EOF
***** Now JScript begins *****/
WScript.Echo(WScript.CreateObject("Shell.Application").Namespace(16).Self.Path);
WScript.Echo(WScript.CreateObject("WScript.Shell").SpecialFolders("Desktop"));
is shorter, and looks more readable to me –
Beaverbrook If you absolutely need to have a batch file, but want to use the power of windows scripting host, you might want to try a WSH/batch hybrid
Batch/WSH hybrid:
@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
for /f "delims=" %%x in ('cscript //E:JScript //nologo "%~f0"') do set desk=%%x
echo desktop path is %desk%
@goto :EOF
@end @ELSE
WScript.Echo(WScript.CreateObject("Shell.Application").Namespace(16).Self.Path);
@end
See ShellSpecialFolderConstants if you need to get the path of some other shell folder
set UserDesktop=%UserProfile%\Desktop
if exist %Public% (
set SharedDesktop=%Public%\Desktop
) else (
set SharedDesktop=%AllUsersProfile%\Desktop
)
So now you can use the Local Variables
%UserDesktop%
and %SharedDesktop%
SharedDesktop first case is for Vista and above the else is for XP
ps: before using these variables you should quote then "%UserDesktop%" because Username must have spaces, like ...\Bill Gates\... or \Documents and settings\...
© 2022 - 2024 — McMap. All rights reserved.