如何找到批处理文件

How do I locate a batch file

所以我正在写一个批处理的小程序,我希望它能运行在别人的电脑上。我的 cdC:\Users\%username%\Desktop\,\thing,但那不是其他人可能存储它的地方。我如何找到它的位置并对它执行 cd?谢谢!

程序和文档可以添加到注册表中,因此在“开始”- 运行 对话框或快捷方式中键入不带路径的名称可以使 Windows 找到它们。

这是一个通用的 reg 文件。将下面的行复制到一个新的文本文档并将其另存为 anyname.reg。使用您的程序或文档对其进行编辑。

在路径中使用 \ 来分隔关键路径中的文件夹名称,因为 regedit 使用单个 \ 来分隔其关键名称。所有 reg 文件都以 REGEDIT4 开头。分号将一行变成注释。 @ 符号表示将值分配给键而不是命名值。

该文件不必存在。这个可以用来设置Word.exe打开Winword.exe.

此示例将 IE.Txt(来自 IE5)添加到注册表,因此键入 IE.Txt 将打开它。我认为该文件在 IE4 中称为 IE4.txt。

REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension> 

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\IE.txt]

;The @ means the path to the file is assigned to the default value for the key.
;The whole path in enclosed in a quotation mark ".

@="\"C:\Program Files\Internet Explorer\IE.txt\""

;Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry

;Informs the shell that the program accepts URLs.

;"useURL"="1"

;Sets the path that a program will use as its' default directory. This is commented out.

;"Path"="C:\Program Files\Microsoft Office\Office\"

reg命令可以批量读取或写入注册表。将其注册为 exe 文件而不是批处理文件。

%cd% 可用于批处理文件或在命令提示符下,并扩展为当前目录的驱动器盘符和路径(可以更改,例如使用 CD 命令)

%~dp0 仅在批处理文件中可用,并扩展为该批处理文件所在的驱动器号和路径(无法更改)。它是从 %0 获得的,这是批处理文件的名称。

@echo off
echo %appdata%
CD %appdata%
pause
echo %CD%
pause
Dir 
pause
echo "%~dp0%"
pause
CD "%~dp0%"
Dir 
cls
echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0
pause