%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%
这可能会帮助你。 –