%0
是您的批處理文件的名稱。您可以使用%〜dp0與pushd
來切換到您的批處理文件所在的目錄,並從那裏工作到任何目錄。所以,這樣的批處理文件的一般結構爲:
@echo off
pushd %~dp0
rem batch file commands go here
popd
至於你的Visual Studio的問題...用戶模式環境變量可用於環境變量的設置後,每個進程開始。但由於某種原因,目前的流程沒有收到它。但Explorer.exe(處理啓動菜單和運行命令的人似乎在每次需要它時都會獲取環境的新副本)。
以這種方式啓動的進程將具有新環境,而啓動的進程命令行會繼承舊的環境,而不會被setx
設置新的變數。
您可以通過更改您的批處理文件既set
和setx
變量緩解這一問題。
下面是一些代碼,讓你開始吧
- 顯示當前的工作目錄
- 更改其中的腳本所在
- 設置變量MYTEST在它的運行
- 從shell設置變量在用戶的環境
- 啓動記事本外殼的目錄,所以你可以嘗試打開一個文件名%MYTEST%
- 用%嘗試它,它的工作原理
- 注意當前目錄是如何改變的,順便說一句。
- 請您從開始菜單
下面是代碼手動啓動記事本,HTH
@echo off
::This is where we start
echo Current directory is %CD%
echo %0
::We change the current directory to where the script is running
pushd %~dp0
echo Current directory is %CD%
::if you want, you can move relatively from it
cd..
::Set an environement variable
set MYTEST=%~f0
::Make a copy avaiable to other processes
setx MYTEST "%MYTEST%"
::Now I should be able to fire notepad and open %%MYTEST%%
::(you can use the %% sign in the open box)
::Let's start a copy from this process
::
echo Starting notepad, open the file %%MYTEST%%, you should this
echo file thanks to the set statement.
notepad
echo Now launch Notepad from the start menu and open the file %%MYTEST%%,
echo you should this file thanks to the setx statement.
::Wherever you end up, restore the current directory
popd
echo Current directory is %CD%
這可能會幫助你。 –