使目录不在当前目录中 [英] Making a directory not in the current directory

查看:62
本文介绍了使目录不在当前目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am making a batch file that I will put in the startup folder. I need it to make a directory, but I need it to be made on the desktop. I only know how to make a new directory in the current directory.

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

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

推荐答案

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

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

因此可以仅使用:

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

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

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.

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

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

  • 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
该注册表项包含与上面的注册表项几乎相同的字符串值,但是这些字符串值通常为REG_SZ类型.此注册表项用于向下兼容不支持带有环境变量引用的字符串值的其他注册表项的应用程序.

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.

可能仅在两个注册表项之一中定义了shell文件夹.例如,在Windows XP上,字符串值Administrative ToolsCD BurningFontsRecent仅在注册表项Shell Folders下存在,而在项User Shell Folders下不存在.

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.

复合添加的信息:

  1. Windows本身默认情况下使用键User Shell Folders下定义的字符串值,并且仅在键User Shell Folders下不存在的情况下使用键Shell Folders下定义的字符串值.

  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.

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

关于Windows在键User Shell FoldersShell Folders下处理字符串值Desktop的一些其他事实,如在Windows XP SP3 x86中观察到的,并在更改当前用户的注册表配置单元后始终重新启动Windows:

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

  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.

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

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.

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

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

如果键Shell Folders 下的类型REG_SZ的字符串值DesktopUser Shell Folders下的类型REG_EXPAND_SZ的字符串值DesktopShell Folders下创建类型为REG_SZ的字符串值Desktop,扩展路径为%USERPROFILE%\Desktop,并且还创建目录(如果没有)现存的. Windows不重新创建键USer Shell Folders下类型为REG_EXPAND_SZ的字符串值Desktop 不是.

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.

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

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天全站免登陆