CMD:Bat 到 Exe 转换器 - 临时目录问题
我正在使用 'Bat to Exe Converter' 将我的批处理文件转换为 exe格式。
现在,我遇到了一些问题。 每当我转换某些内容,并将“工作目录”设置为“当前目录”,并以 echo on
模式启动 exe 时,这就是我最终检查是否存在特定文件的结果在我的exe目录中:
实际命令:如果不存在“%~dp0\file.txt”转到:nofile
输出:如果不存在“C:\Users\MyUser\AppData \Local\Temp\4CBC\\file.txt" goto :nofile
谁能帮我解决这个问题吗?我不希望它位于临时目录中,我希望它位于我的 exe 目录中。
谢谢。
i am using 'Bat to Exe Converter' to convert my batch files to exe format.
Now, i am running into some problems.
Whenever i convert something, and i set 'Working Directory' to 'Current Directory', and i start my exe in echo on
mode, this is what i end up with to check if there is a specific file in the directory of my exe:
the actual command: if not exist "%~dp0\file.txt" goto :nofile
output: if not exist "C:\Users\MyUser\AppData\Local\Temp\4CBC\\file.txt" goto :nofile
Can anyone help me with this? I don't want it in the temp directory, i want it to be in the directory of my exe.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果作者没有更改Bat to Exe,我认为您有两个选择:
删除访问 %~dp0 的需要
也许您可以将
file.txt
与 Bat to Exe 的包含选项合并到 EXE 文件中。如果是这样,运行编译好的exe时,“file.txt”
会自动解压到当前目录,您可以通过%CD%\file.txt
访问它。从外部获取
%~dp0
并将其作为命令行参数传递给exe。这可以通过一个简单的启动
bat
文件来完成,该文件与编译的主批处理文件位于同一目录中。该脚本应包含行然后,您编译的 exe 从 %1 获取其目录。因此,您无法将所有内容打包到一个 exe 中,但它的主要部分,也许对您来说就足够了。
Without having Bat to Exe changed by the author, I think you have two options:
Remove the need for accessing %~dp0
Perhaps you can merge
file.txt
with the include option of Bat to Exe into the EXE file. If so,"file.txt"
will automatically be unpacked in the current directory when running your compiled exe, and you can it access by%CD%\file.txt
.Get
%~dp0
from outside and pass it to the exe as a command line parameter.This can be done by a simple starter
bat
file that resides in the same directory as your compiled main batch file. This script schould contain the lineYour compiled exe then gets its directory from %1. So you cannot pack everything into one exe, but the main portion of it, perhaps that is sufficient for you.
好吧,显然您的批处理到 exe 转换器只是将批处理文件打包并在执行之前将其提取到临时目录。非常简单,很难出错(与实际理解批处理文件相比),但它引入了诸如您所描述的错误。
您最好的选择可能是使用另一个批处理到 exe 转换器;其中一些实际上更复杂一些。
Well, apparently your batch to exe converter simply packs the batch file and extracts it to a temporary directory prior to execution. Very simplistic, hard to get wrong (compared to actually understand the batch file) but it introduces errors such as the one you're describing.
Your best bet is probably to use another batch to exe converter; some of them are actually a little more sophisticated.
一般来说,这不是一个好主意。首先,它在不同的cmd特性上容易出现错误并且转换器不稳定。其次,坚定的黑客仍然可以解码您对该批次所做的事情。我的建议是,如果您非常害怕人们查看您的批次,
或者根本不要使用批次
Generally, this is not a good idea. firstly, its prone to errors and instability of the converter on different cmd features. secondly, a determined hacker can still decode what you are doing with the batch. My suggestion, if you are so afraid of people looking into your batch,
OR, don't use batch at all
这是一个 2.5 年前的主题,但有一个答案,因此我将其发布给其他碰巧在搜索中找到此内容的人。
B2EC 由 Fatih Kodak 编写,有一个“提交当前目录”选项。
使用此功能时,您可以在批处理文件中引用 %1 来获取已执行的 EXE 的路径(而不是实际运行的提取的 BAT 的路径)。
将鼠标悬停在 UI 中的该选项上会显示“提交当前工作目录作为最后一个参数”。我使用的“最后一个参数”始终是 %1,但您可以测试您的代码以确定。
This is an 2.5 yr old subject but there is an answer to this so I'm posting for anyone else that happens to find this in a search.
B2EC written by Fatih Kodak, has an option to "Submit current directory".
When this is used, you can reference %1 in your batch file to get the path of the EXE that was executed (instead of the path of the extracted BAT that is really being run).
Hovering over that option in the UI shows "Submit the current working directory as the last parameter". The "last parameter" in my use has always been %1 but you can test your code to be sure.
Fatih Kodak 的 Bat to Exe 的最新版本(撰写本文时为 2.1.4)在运行时创建了一个环境变量,可以替换 %~dp0 来引用 Exe 的路径。因此,您可以简单地将原始批处理文件中的%-dp0替换为%b2eprogrampathname%。
The latest version, 2.1.4 at time of writing, of Bat to Exe by Fatih Kodak creates an Environmental Variable at runtime that can be substituted in place of %~dp0 to reference the Exe's path. Therefore, you can simply replace %-dp0 with %b2eprogrampathname% in the original batch file.
您可以通过 f2ko 的批处理到 exe 转换器使用外部文件夹。拥有
单独的子程序文件夹可以使项目文件夹更加整洁。
要调用位于 mysubroutinesfolder\mysubroutine 中的 mysubroutine,
可以进行单行调用:
其中 xqt.cmd 是一个为您执行调用的程序:
(%* 表示“所有参数”)。
通过这种方式,您的批处理程序将作为批处理运行,并且未经修改它们将
使用 bat 到 exe 转换器进行编译,创建一个完全独立于文件夹的可执行文件。选择“临时目录”,
并将所有子例程/可执行文件包含在 mysubroutines 文件夹中
像往常一样用光标“全选”,然后点击“复制”。
确保也包含 xqt.cmd 程序;将其放在 mysubroutines 文件夹的“外部”。确保您的主程序可以访问它。如果您需要,请记住选择 x64
在 x64 机器上运行,否则可执行文件将找不到 SYSTEM32
文件。您可以在以下位置找到 f2k0 的批处理到 exe 转换器:
http://www.f2ko.de/programs .php?pid=b2e
You can use external folders with f2ko's batch to exe converter. Having
a separate folder for subroutines can neaten up a project folder.
To call mysubroutine that is located in mysubroutinesfolder\mysubroutine,
The call can be made a one liner:
where xqt.cmd is a program that does the call for you:
(the %* means "all of the arguments").
In this way your batch programs run as batch, and UNMODIFIED they will
compile with the bat to exe converter, creating a completely folder independent executable. Select "temporary directory",
and include all of the subroutines/executables in your mysubroutines folder
by "selecting them all" with your cursor as usual, then hit "copy".
Be sure to include the xqt.cmd program too; place it "outside" of your mysubroutines folder. Make sure that is is accessible by your main program. Remember to select x64 if you
are runnning on a x64 machine, or the executable will not find SYSTEM32
files. You can find f2k0's batch to exe converter at:
http://www.f2ko.de/programs.php?pid=b2e
尝试使用此批处理脚本开发环境,批处理编译器 。它拥有开发批处理程序所需的一切。并编译成稳定的独立可执行文件(Exe)。
快速下载:http://bc.gotek.info/files/BatchCompiler159.zip
干杯!
Try this development environment for batch scripts, Batch Compiler . It has everything you need to develop a batch program.And compile into stable stand alone executable (Exe).
Quick download : http://bc.gotek.info/files/BatchCompiler159.zip
Cheers!
%cd% 将为您提供当前目录:
%cd% will give you the current directory:
使用
%CD%
而不是%~dp0
。编辑:
B2EC 不是真正的转换器。配备的.cmd 文件的创建位置选择为
%TEMP%
,这是一个不错的选择。应用程序只是缺少脚本工作目录的第三个选项 - .exe 文件目录。我建议您向作者发送有关添加此内容的邮件。.exe 和创建的 .cmd 的不同路径会导致信息丢失,即,如果不向脚本提供附加信息(例如使用环境变量或将其作为第一个/最后一个参数传递),我们无法同时知道 .exe 目录和当前目录到脚本)。该脚本需要处理它,我们将结束为该转换器编写 cmd 脚本,这很糟糕。
%~dp0
- 脚本目录 (%TEMP%/.../
) - 几乎无用%cd%
- 工作目录(按照设置)在转换器中) - 目前只有2个选项:当前目录(.exe的工作目录)和临时目录(实际上等于%~dp0
,但没有尾部反斜杠)我认为可以解决通过修补内存中的
cmd.exe
实例来更改脚本路径,但这是 B2EC 开发人员的职责。旁注:通过为
CreateProcess
函数提供适当的lpApplicationName
和lpCommandLine
,可以使用指定的第 0 个参数轻松执行普通可执行文件。命令文件是通过cmd.exe
执行的,所以第0个参数不能这样设置。Use
%CD%
instead of%~dp0
.EDIT:
B2EC is not a real converter. Creation location of equipped .cmd file was chosen to be
%TEMP%
and this is a good choice. Application just lacks 3rd option for working directory of the script - .exe file directory. I advise you to mail the author about adding this one.Different paths for .exe and created .cmd lead to information loss, i.e. we are unable to know .exe directory and current directory at the same time without providing additional information to the script (e.g. using environment variable or passing it as first/last argument to the script). This script would need to handle it and we would end writing cmd scripts tailored for this converter, which is bad.
%~dp0
- script directory (%TEMP%/.../
) - practically useless%cd%
- working directory (as set up in the converter) - currently there are only 2 options: current directory (working directory of .exe) and temporary directory (actually equal to%~dp0
, but without trailing backslash)I think it can be solved by patching
cmd.exe
instance in memory to change the script path, but that's B2EC developer's duty.Side note: Normal executable files can be easily executed with specified 0th argument by providing appriopriate
lpApplicationName
andlpCommandLine
toCreateProcess
function. Command files are executed viacmd.exe
, so 0th argument cannot be set this way.