我一直在研究一个batch file脚本几个星期,现在应该自动化我在工作中需要执行的某些例行任务。 然而,我偶然发现了一个我还没有弄清楚的问题。
我正在使用带有脚本和一些其他软件(.exe)的USB棒。 该脚本要求您以pipe理员模式运行它,以便它可以访问某些服务,如Windows时间服务或Windows更新服务…
现在我注意到,当你以pipe理员模式运行脚本时,它的起始目录是C:\ Windows \ System32
在这个脚本期间,我希望运行一个程序,也是在这个棒子上。 但是它没有find这个程序。 我可以把path写在棒子上的确切位置。 但是这样做是行不通的,因为在不同的计算机上使用了这个棒,而且盘符并不总是一致的。
长话短说,我正在寻找一种方法来运行pipe理员模式下的batch file(位于USB设备上),并让该脚本自动运行程序(也在USB设备上)。
提前致谢! 登普西
PS:如果可能的话,有人能够解释我如何使脚本将所有东西都logging到文本文件中? 请记住,脚本中有很多命令。 将所有输出logging到一个文本文件并保存在同一个USB设备上将会很好。
将这些行添加到脚本的顶部:
@setlocal enableextensions @cd /d "%~dp0"
第一行启用环境variables,第二行是一个特殊的variables,指向正在启动的脚本的当前目录。
这个答案是用户wilx的一个很好的分解:
cd -- This is change directory command. /d -- This switch makes cd change both drive and directory at once. Without it you would have to do cd %~d0 & cd %~p0. %~dp0 -- This can be dissected further into three parts: %0 -- This represents zeroth parameter of your batch script. It expands into the name of the batch file itself. %~0 -- The ~ there strips double quotes (") around the expanded argument. %dp0 -- The d and p there are modifiers of the expansion. The d forces addition of a drive letter and the p adds full path.