简体   繁体   English

环境变量中的批处理文件变量

[英]Batch file variable in an environment variable

I have a program named go.exe , and I pass it a file name example so that it can make a log file named example_golog.txt . 我有一个名为go.exe的程序,并向其传递了一个文件名example以便它可以创建名为example_golog.txt的日志文件。

The problem here is that I want to only call the executable without explicitly giving a filename or making a user have to give a file name. 这里的问题是我只想调用可执行文件,而没有明确给出文件名或使用户不得不给出文件名。

My solution to this was to name a system variable called GO whose value is go.exe %~n0 . 我对此的解决方案是命名一个名为GO的系统变量,其值为go.exe %~n0 What's wrong with this is instead of the %~n0 part getting the file name of the batch file that called it, my go.exe file just makes a text file called %~n0_golog.txt . 这是怎么回事,而不是%~n0部分获得了调用它的批处理文件的文件名,我的go.exe文件只是制作了一个名为%~n0_golog.txt的文本文件。

Is there any way around this so that %~n0 will do it's magic in the batch files that call it in go.exe %~n0 ? 有什么办法可以解决,以便%~n0 go.exe %~n0go.exe %~n0中调用它的批处理文件中go.exe %~n0

EDIT: 编辑:

My system variable: Name: GO Value: go %~n0 我的系统变量:名称: GO值: go %~n0

My test.bat file: 我的test.bat文件:

@echo on
%GO% 
pause

When I run test.bat , the %~n0 does not expand out. 当我运行test.bat%~n0不会扩展。

So instead of test_golog.txt being made, %~n0_golog.txt is made 因此,不是制作test_golog.txt而是制作%~n0_golog.txt

In your batch file, you can call it to dereference the variable. 在批处理文件中,可以call它以取消引用变量。

echo %go% will report go %~n0 . echo %go%将报告go %~n0

call echo %go% will report go test (from a batch file named test). call echo %go%将报告go test (来自名为test的批处理文件)。

You can use whatever other sets you need from there. 您可以从那里使用所需的任何其他集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM