如何在用户的桌面目录中创建目录? [英] How to create a directory in the user's desktop directory?

查看:78
本文介绍了如何在用户的桌面目录中创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在制作一个批处理文件,该文件将放入启动文件夹中.我需要在当前用户的桌面目录中创建一个目录.我只知道如何在当前目录中建立一个新目录.

我知道如何创建目录的唯一方法是使用命令 mkdir .

解决方案

使用%USERPROFILE%\ Desktop 定义Windows桌面目录的默认. USERPROFILE 是预定义的 Windows环境变量之一../p>

因此可以仅使用:

  md%UserProfile%\ Desktop \ NewDirectory"2> nul 

这将在用户的桌面上创建一个名称为 NewDirectory 的目录,只要用户未更改桌面目录的默认设置即可.命令 md 可以与标准目录路径或相对目录路径一起使用.在命令提示符下运行的帮助输出 md/?解释说,如果默认情况下启用了命令扩展名,则 md 将整个目录树创建到一个不存在的目录.另请参阅Microsoft文档,以命名文件,路径和命名空间.

但是最好从Windows注册表中获取桌面目录路径,而不是仅使用默认值.有两个注册表项包含名称为 Desktop 的字符串值,并带有用户桌面目录的路径:

  • HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ User Shell文件夹
    该注册表项包含几个通常为 REG_EXPAND_SZ 类型的字符串值,这些字符串值定义了为当前用户帐户定义的各种shell文件夹的路径.shell文件夹通常包含一个环境变量引用,这是 REG_EXPAND_SZ 类型的原因,这意味着必须另外扩展字符串值才能获取shell文件夹的绝对路径.下面的批处理文件通过使用命令 CALL 来强制Windows指令处理器再进行一次命令行解析,从而扩展了环境变量.

  • HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell文件夹
    该注册表项包含与上面的注册表项几乎相同的字符串值,但是这些字符串值通常为 REG_SZ 类型.此注册表项用于向下兼容不支持带有环境变量引用的字符串值的其他注册表项的应用程序.

可能仅在两个注册表项之一中定义了shell文件夹.例如,在Windows XP上,字符串值管理工具 CD刻录字体 Recent 仅在注册表项下存在 Shell文件夹,并且在键 User Shell Folders 下不存在.

复合添加的信息:

  1. Windows本身默认情况下使用键 User Shell Folders 下定义的字符串值,并且仅在键 Shell Folders 下定义的字符串值使用用户外壳文件夹.

  2. Windows不会将对 User Shell Folders 下的字符串值的修改传播到对 Shell Folders 下的同名字符串值的修改.程序直接在注册表中修改键 User Shell Folders 下的字符串值,而无需对键 Shell Folders 下具有相同名称的键进行适当的更改.
    因此,如果 User Shell Folders 中的 Desktop 包含与 Shell Folders 中的 Desktop 不同的目录路径,则Windows将使用用 User Shell Folders 中的 Desktop 定义的路径.

用户可以自由地将任何文件夹更改为用户想要的任何文件夹.但是用户必须注意两次更改两个注册表项中的字符串值.通过Windows或Windows应用程序的图形用户界面上的选项(例如 Downloads shell文件夹),可以轻松地修改某些shell文件夹.

另请参阅Microsoft文档,以获取已知文件夹 KNOWNFOLDERID 以及这些页面上引用的其他文档页面以及有关应用程序注册的文档.

这是一个批处理文件,可从Windows注册表中尽可能安全地获取用户的桌面目录.

  @echo关闭setlocal EnableExtensions DisableDelayedExpansion设置"DesktopFolder ="/F的跳过= 1个令牌= 1,2 *"如果(I)%% I"==台式机"如果不是,则为"%%〜K";=="如果"%% J"=="REG_SZ";(设置"DesktopFolder = %%〜K");否则,如果"%% J"=="REG_EXPAND_SZ";呼叫集"DesktopFolder = %%〜K"如果没有定义,则为/F"skip = 1 tokens = 1,2 *"的DesktopFolder如果/I%"('%SystemRoot%\ System32 \ reg.exe QUERY"HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders/v Desktop 2 ^&nul')中的%% I做%I"==台式机"如果不是,则为"%%〜K";=="如果"%% J"=="REG_SZ";(设置"DesktopFolder = %%〜K");否则,如果"%% J"=="REG_EXPAND_SZ";呼叫集"DesktopFolder = %%〜K"如果未定义,DesktopFolder设置为"DesktopFolder = \".如果%DesktopFolder:〜-1%"=="\"设置"DesktopFolder =%DesktopFolder:〜0,-1%"如果未定义,DesktopFolder设置为"DesktopFolder =%UserProfile%\ Desktop";md"%DesktopFolder%\ NewDirectory"2> nul本地 

此批处理文件甚至在Windows XP上也可以使用,在Windows XP上 reg.exe 输出的查询结果与Windows Vista和更高版本的Windows的 reg.exe 不同.

请参阅有关使用命令重定向运算符解释 2> nul ,该命令将命令 MD 输出的错误消息重定向到已经存在的可处理目录中 STDERR 到设备 NUL 以禁止显示此错误消息.

但是,用户的桌面目录应仅包含快捷方式文件( *.lnk 文件)以及用户在桌面上创建的文件和目录.除了快捷方式文件或用户桌面目录中的目录外,任何程序都不能创建其他文件.Microsoft为应用程序定义了其他几个Shell文件夹,例如 APPDATA (应用程序数据)或 LOCALAPPDATA (本地应用程序数据)

.

与Windows XP SP3相比,Windows在处理 User Shell Folders Shell Folders 键下的字符串值 Desktop 时,还有一些其他事实更改当前用户的注册表配置单元后,总是重新启动Windows的x86:

  1. 更改键 User Shell Folders 下的字符串值 Desktop 的路径字符串,例如从%USERPROFILE%\ Desktop 更改为%USERPROFILE%\ MyDesktop ,当然,创建目录%USERPROFILE%\ MyDesktop 会将Windows桌面目录更改为自定义%USERPROFILE%\ MyDesktop <下次登录时使用/code>,并且在下次重新启动时,Windows会修改键 Shell Folders 下的 Desktop 的字符串值.我没有测试是否在键 Shell Folders 下的 Desktop 是否仅在注销并登录后也可以使用.绝对最好同时更改两个 Desktop 字符串值,以将桌面目录永久更改为与默认%USERPROFILE%\ Desktop 不同的目录.

  2. Windows永远不会重新创建键 User Shell Folders 下的已删除或重命名的字符串值 Desktop .因此,如果一旦删除或重命名了 User Shell Folders 键下的 Desktop 后,该字符串值可能不存在,则一旦删除或重命名,或者结果导致注册表文件部分损坏该字符串值不存在.如下面的进一步测试所示,用户不会注意到该问题.

  3. Shell Folders 下的类型为 REG_SZ 的字符串值 Desktop 始终设置为%USERPROFILE的扩展路径如果根本不存在键 User Shell Folders 下类型为 REG_EXPAND_SZ 的字符串值 Desktop ,则为%\ Desktop .如果在这种错误处理情况下不存在,Windows也会自动创建目录%USERPROFILE%\ Desktop

  4. 如果键 Shell Folders 下的字符串值 REG_SZ 的字符串值 Desktop Windows创建的用户或程序将两者删除或重命名的键 User Shell Folders 下的 REG_EXPAND_SZ 类型的 Desktop 在下一个启动键 Shell Folders 下的类型为 REG_SZ 的字符串值 Desktop ,扩展路径为%USERPROFILE%\ Desktop ,并创建目录(如果不存在).Windows不会重新创建键 USer Shell Folders 下类型为 REG_EXPAND_SZ 的字符串值 Desktop .

  5. 如果键 Shell Folders 下的类型为 REG_SZ 的字符串值 Desktop 存在与%不同的扩展路径USERPROFILE%\ Desktop ,如%USERPROFILE%\ MyDesktop 的扩展路径,以及键<下的类型为 REG_EXPAND_SZ 的字符串值 Desktop code> User Shell Folders 根本不会退出,Windows将忽略键 Shell Folders REG_SZ Desktop 的自定义路径.>,然后将值设置为%USERPROFILE%\ Desktop 的扩展路径,并另外创建目录%USERPROFILE%\ Desktop (如果尚不存在).因此,如果没有在键 User Shell Folders <下设置字符串类型 REG_EXPAND_SZ 的字符串值 Desktop 的情况下,也无法使用自定义的桌面目录./code>.

我没有使用Windows较新版本对在 Shell Folders User Shell Folders 键下对 Desktop 的处理进行测试.或两个字符串值都不存在和/或具有相同或不同的目录路径和/或具有不同于默认值的目录路径.

So I am making a batch file that I will put in the startup folder. I need to make a directory in the desktop directory of current user. I only know how to make a new directory in the current directory.

The only way I know how to make a directory is with the command mkdir.

解决方案

The default for the Windows desktop directory is defined with %USERPROFILE%\Desktop. USERPROFILE is one of the predefined Windows environment variables.

So it would be possible to use just:

md "%UserProfile%\Desktop\NewDirectory" 2>nul

That would create a directory with name NewDirectory on user's desktop as long as the user has not changed the default for the desktop directory. The command md can be use with a full qualified directory path or a relative directory path. The help output on running in a command prompt md /? explains that md creates the entire directory tree to a directory not existing if command extensions are enabled as by default. See also the Microsoft documentation for naming files, paths, and namespaces.

But it would be better to get the desktop directory path from Windows registry instead of using simply the default. There are two registry keys containing a string value with name Desktop with the path to user's desktop directory:

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
    This registry key contains several string values usually of type REG_EXPAND_SZ which define the paths to the various shell folders defined for the current user account. The shell folders contain usually an environment variable reference which is the reason for the type REG_EXPAND_SZ which means the string value must be additionally expanded to get absolute path to the shell folder. The batch file below expands the environment variables by using command CALL to force one more command line parsing by Windows command processor.

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    This registry key contains nearly the same string values as the registry key above, but the string values are usually of type REG_SZ. This registry key is for downwards compatibility for applications not supporting the other registry key with the string values with environment variable references.

It is possible that a shell folder is defined only in one of the two registry keys. For example on Windows XP the string values Administrative Tools, CD Burning, Fonts and Recent exist only under registry key Shell Folders and do not exist under key User Shell Folders.

Information added by Compo:

  1. Windows itself uses by default the string values defined under key User Shell Folders and uses a string value defined under key Shell Folders only if not existing under key User Shell Folders.

  2. Windows does not propagate a modification on a string value under key User Shell Folders to the string value with same name under key Shell Folders if a user or a program modifies directly in registry a string value under key User Shell Folders without making appropriate change to key with same name under key Shell Folders.
    So in case of Desktop in User Shell Folders contains a different directory path than Desktop in Shell Folders, Windows uses the path defined with Desktop in User Shell Folders.

A user has the freedom to change any folder to whatever the user wants. But the user must take care to change a string value in both registry keys on existing twice. Some of the shell folders can be easily modified via an option on graphical user interface of Windows or a Windows application like the Downloads shell folder.

See also the Microsoft documentations for Known Folders and KNOWNFOLDERID and the other documentation pages referenced on these pages as well as the documentation about Application Registration.

Here is a batch file which gets the user's desktop directory from Windows registry as much safe as possible.

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "DesktopFolder="
for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder set "DesktopFolder=\"
if "%DesktopFolder:~-1%" == "\" set "DesktopFolder=%DesktopFolder:~0,-1%"
if not defined DesktopFolder set "DesktopFolder=%UserProfile%\Desktop"

md "%DesktopFolder%\NewDirectory" 2>nul

endlocal

This batch file works even on Windows XP on which reg.exe outputs the results of the query different to reg.exe of Windows Vista and newer Windows versions.

See Microsoft article about Using command redirection operators for an explanation of 2>nul which redirects the error message output by command MD on directory already existing to handle STDERR to the device NUL to suppress this error message.

However, the user's desktop directory should contain only shortcut files (*.lnk files) and the files and directories created by the user on the desktop. No program should every create other files than shortcut files or directories in the user's desktop directory. Microsoft defined several other shell folders for applications like APPDATA (application data) or LOCALAPPDATA (local application data) for applications.


Some additional facts about handling of string value Desktop under the keys User Shell Folders and Shell Folders by Windows as observed with Windows XP SP3 x86 with always restarting Windows after making a change in registry hive of current user:

  1. A change of the path string of the string value Desktop under the key User Shell Folders for example from %USERPROFILE%\Desktop to %USERPROFILE%\MyDesktop and of course creation of the directory %USERPROFILE%\MyDesktop changes the Windows desktop directory to custom %USERPROFILE%\MyDesktop on next log on and the string value of Desktop under key Shell Folders is adapted by Windows on next restart. It was not tested by me if Desktop under the key Shell Folders is adapted also on just doing a log off and log on. It is definitely better to change both Desktop string values at the same time to change the desktop directory permanently to a directory different from default %USERPROFILE%\Desktop.

  2. A removed or renamed string value Desktop under the key User Shell Folders is never recreated by Windows. So it is possible that this string value does not exist if Desktop under the key User Shell Folders was by mistake once deleted or renamed or the registry file is partly damaged with the result that this string value does not exist. A user would not notice that issue as the further tests below showed.

  3. The string value Desktop of type REG_SZ under key Shell Folders is always set to expanded path of %USERPROFILE%\Desktop if string value Desktop of type REG_EXPAND_SZ under key User Shell Folders does not exist at all. Windows creates also the directory %USERPROFILE%\Desktop automatically if not existing in this error handling case

  4. If the string value Desktop of type REG_SZ under key Shell Folders and the string value Desktop of type REG_EXPAND_SZ under key User Shell Folders are both deleted or renamed by a user or program, Windows creates on next start the string value Desktop of type REG_SZ under key Shell Folders with expanded path of %USERPROFILE%\Desktop and creates also the directory if not existing. The string value Desktop of type REG_EXPAND_SZ under key USer Shell Folders is not recreated by Windows.

  5. If the string value Desktop of type REG_SZ under key Shell Folders exists with a different expanded path than %USERPROFILE%\Desktop like expanded path of %USERPROFILE%\MyDesktop and the string value Desktop of type REG_EXPAND_SZ under the key User Shell Folders does not exit at all, Windows ignores the customized path of Desktop of type REG_SZ under the key Shell Folders and sets the value to expanded path of %USERPROFILE%\Desktop and creates additionally the directory %USERPROFILE%\Desktop if not already existing. So it is not possible to use a customized desktop directory without having the customized desktop directory defined also with string value Desktop of type REG_EXPAND_SZ under the key User Shell Folders.

I did not make tests with newer versions on Windows regarding to handling of Desktop under the keys Shell Folders and User Shell Folders if one or both string values do not exist and/or have same or different directory paths and/or have a directory path different to default.

这篇关于如何在用户的桌面目录中创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆