2

I am writing a batch file on my Windows 8.1 machine. In one section of my batch file I need to start a command prompt in the "current working directory".

So far, this is what my batch file looks like:

@echo OFF
set WORKING=%cwd%
start cmd.exe /K pushd %WORKING%
exit

Let's say the batch file is located in the folder C:\Temp\Utilities. If I open an explorer window and double click the batch file to run it everything works great. A new command prompt is created in the directory C:\Temp\Utilities. However, if I right-click the batch file and select Run as administrator the working directory is no longer the location of the batch file, it's C:\Windows\System32.

Similarly, if I create a shortcut to the batch file in a different folder (for example. C:\Temp) and repeat the two steps above the results are the same. If I double click the shortcut and run it as a normal user the working directory is what I would expect. (Note, the working directory for the shortcut it's whatever is set for "Start in" of the shortcut properties, not the location of the batch file.) If I right click the shortcut and run it as administrator I again get a command prompt opened to the folder C:\Windows\System32.

I assume this is a "bug" or "feature" (if you want to call it that) in Windows 8.1 and it probably happens because execution environments for programs run as administrator are forced to run in the System32 folder? (I remember with Windows 7 this did not happen so it must be a new feature to Windows 8.)

I found one way to fix the issue and stop the command prompt from starting in C:\Windows\System32. I did this by modifying the following line in the batch file:

set WORKING=%~dp0

Doing it this way sets the working directory to the location of the batch file. With this change, no matter how I run the batch file or the shortcut (administrator or normal) the working directory ends up being the same, C:\Temp\Utilities.

The problem with this solution is I don't want the working directory to always be the location of the batch file. If the batch file is run directly then it's okay but if I run it from a shortcut I need the working directory to be whatever is set in the "Start in" property of that shortcut. For example, if the batch file is located in the folder D:\Temp\Utilities this is what I need to happen regardless of whether I run as administrator or not:

Shortcut Location    Start In Property    Command Prompt Working Directory
--------------------    -------------------     ------------------------------------------
C:\Temp                  <undefined>           D:\Temp\Utilities
C:\Data\bin             C:\Data\bin             C:\Data\bin
C:\Data\bin             D:\Temp\Utilities     D:\Temp\Utilities

What this means is I can't always use %~dp0 to set the working directory in my batch file. What I need is some way for the batch file to know if it was run either directly or by a shortcut. If the batch file is run directly then the working directory is easy to get, it's just the value of %cwd%. If the batch file is run by using a shortcut, I don't know how to get the "Start in" property inside the batch file.

Does anyone know how I can do these two things inside my batch file:

1. Check whether it was run directly or by a shortcut.

2. If run by a shortcut, get the "Start in" property of the shortcut that started it.

Thank you,

Orangu

UPDATE

I found sort-of a "hackish" way to fix the issue. For the shortcut I edited the "Target" field and changed it to the following:

cmd.exe /k pushd "C:\Temp" && "D:\Temp\Utilities\batchfile.bat"

Now the working directory can be obtained by calling %CD% in the batch file and this works for both administrator and normal users. It does not, however, work for the case when I run the batch file directly. I still need to use %~dp0 in that case.

I don't like this solution, however, because it requires me to manually change all shortcuts I make and it also makes the icon look like a cmd prompt icon rather than a batch file.

4

1 回答 1

0

您是否已经考虑过完全不使用快捷方式?

例如,您可以创建一个batchfile_exec.bat包含您的呼叫的

REM optionally do 
REM cd /D working_directory
REM if you want to force a special working directory
D:\Temp\Utilities\batchfile.bat

并将所有快捷方式替换为batchfile_exec.bat. 如果双击batchfile_exec.bat,工作目录将是包含batchfile_exec.bat.

我个人不太喜欢 Windows 快捷方式,因为它们很难在修订控制系统中处理。正如您还注意到的,如果您想修改其中的很多内容,会非常耗时。

顺便说一句:如果batchfile.bat设计/编写为始终从它所在的目录运行,您也可以考虑修改batchfile.bat以强制该行为:

setlocal
cd /D %0\..

REM your original content

endlocal

%0批处理文件的路径中存储。

诀窍是假设这%0是一个目录,然后根据该目录将其更改为更低的级别。/D驱动器号也正确更改。

cd命令不关心是否%0真的是一个目录。实际上%d甚至不必存在(%0\dummy\..\..也可以)。

setlocal命令是在完成后恢复工作目录(如果从另一个批处理文件调用batchfile.bat这会很好)。batchfile.bat

我注意到该endlocal命令在这种情况下并不是真正必要的,因为它在batchfile.bat完成时隐式应用。

于 2014-11-05T12:49:01.910 回答