CMD:Bat 到 Exe 转换器 - 临时目录问题

发布于 2024-08-16 05:55:04 字数 466 浏览 5 评论 0原文

我正在使用 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

独闯女儿国 2024-08-23 05:55:04

如果作者没有更改Bat to Exe,我认为您有两个选择:

  1. 删除访问 %~dp0 的需要

    也许您可以将 file.txtBat to Exe 的包含选项合并到 EXE 文件中。如果是这样,运行编译好的exe时,“file.txt”会自动解压到当前目录,您可以通过%CD%\file.txt访问它。

  2. 从外部获取%~dp0并将其作为命令行参数传递给exe。

    这可以通过一个简单的启动 bat 文件来完成,该文件与编译的主批处理文件位于同一目录中。该脚本应包含行

    YourCompiled.Exe %~dp0% %%* 
    

    然后,您编译的 exe 从 %1 获取其目录。因此,您无法将所有内容打包到一个 exe 中,但它的主要部分,也许对您来说就足够了。

Without having Bat to Exe changed by the author, I think you have two options:

  1. 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.

  2. 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 line

    YourCompiled.Exe %~dp0% %%* 
    

    Your 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.

两相知 2024-08-23 05:55:04

好吧,显然您的批处理到 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.

冷心人i 2024-08-23 05:55:04

一般来说,这不是一个好主意。首先,它在不同的cmd特性上容易出现错误并且转换器不稳定。其次,坚定的黑客仍然可以解码您对该批次所做的事情。我的建议是,如果您非常害怕人们查看您的批次,

1) let only the people who are authorized to use your batch to use it
2) give them the correct permissions.

或者根本不要使用批次

1) create a central interface such as a web interface, where all tasks to be done goes through that interface, like using an ATM machine where only buttons are allowed and all the available user options can be done by pushing buttons...etc..
2) authenticate your users through a central authentication system, eg Active Directory, or LDAP or a database.

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,

1) let only the people who are authorized to use your batch to use it
2) give them the correct permissions.

OR, don't use batch at all

1) create a central interface such as a web interface, where all tasks to be done goes through that interface, like using an ATM machine where only buttons are allowed and all the available user options can be done by pushing buttons...etc..
2) authenticate your users through a central authentication system, eg Active Directory, or LDAP or a database.
淡忘如思 2024-08-23 05:55:04

这是一个 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).

enter image description here

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.

enter image description here

伴梦长久 2024-08-23 05:55:04

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.

怪我入戏太深 2024-08-23 05:55:04

您可以通过 f2ko 的批处理到 exe 转换器使用外部文件夹。拥有
单独的子程序文件夹可以使项目文件夹更加整洁。
要调用位于 mysubroutinesfolder\mysubroutine 中的 mysubroutine,

...
pushd mysubroutinesfolder
call mysubroutine
popd
...

可以进行单行调用:

call xqt mysubroutine

其中 xqt.cmd 是一个为您执行调用的程序:

pushd mysubroutinesfolder
call %*
popd
exit /b

(%* 表示“所有参数”)。
通过这种方式,您的批处理程序将作为批处理运行,并且未经修改它们将
使用 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,

...
pushd mysubroutinesfolder
call mysubroutine
popd
...

The call can be made a one liner:

call xqt mysubroutine

where xqt.cmd is a program that does the call for you:

pushd mysubroutinesfolder
call %*
popd
exit /b

(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

作妖 2024-08-23 05:55:04

尝试使用此批处理脚本开发环境,批处理编译器 。它拥有开发批处理程序所需的一切。并编译成稳定的独立可执行文件(Exe)。

Friendly user interface.
Debugger, Check your code for syntax errors.
Powerful, versatile compiler.
Allows mouse input in batch files.
Use Windows Common Dialog Boxes.(BrowseFiles,BrowseFolders)
Draw graphics in batch files.
Reverse engineering proof encryption of source code.
Include Company name, Copyright info and Version info.
Make invisible(silent) executables.
Executables with administrator privileges.
Run & debug your script while editing.
Embed resources with executable.(music,images,files)
Advance Commands (BrowseFiles,LaunchSilent,MouseCMD)
Stand-alone executables.No dependencies needed.
Executables are woking on almost all windows operating systems.(98 to 10)

快速下载: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).

Friendly user interface.
Debugger, Check your code for syntax errors.
Powerful, versatile compiler.
Allows mouse input in batch files.
Use Windows Common Dialog Boxes.(BrowseFiles,BrowseFolders)
Draw graphics in batch files.
Reverse engineering proof encryption of source code.
Include Company name, Copyright info and Version info.
Make invisible(silent) executables.
Executables with administrator privileges.
Run & debug your script while editing.
Embed resources with executable.(music,images,files)
Advance Commands (BrowseFiles,LaunchSilent,MouseCMD)
Stand-alone executables.No dependencies needed.
Executables are woking on almost all windows operating systems.(98 to 10)

Quick download : http://bc.gotek.info/files/BatchCompiler159.zip

Cheers!

晒暮凉 2024-08-23 05:55:04

%cd% 将为您提供当前目录:

if not exist "%CD%\file.txt" goto :nofile

%cd% will give you the current directory:

if not exist "%CD%\file.txt" goto :nofile
池木 2024-08-23 05:55:04

使用 %CD% 而不是 %~dp0

编辑:

B2EC 不是真正的转换器。配备的.cmd 文件的创建位置选择为%TEMP%,这是一个不错的选择。应用程序只是缺少脚本工作目录的第三个选项 - .exe 文件目录。我建议您向作者发送有关添加此内容的邮件。

.exe 和创建的 .cmd 的不同路径会导致信息丢失,即,如果不向脚本提供附加信息(例如使用环境变量或将其作为第一个/最后一个参数传递),我们无法同时知道 .exe 目录和当前目录到脚本)。该脚本需要处理它,我们将结束为该转换器编写 cmd 脚本,这很糟糕。

%~dp0 - 脚本目录 (%TEMP%/.../) - 几乎无用

%cd% - 工作目录(按照设置)在转换器中) - 目前只有2个选项:当前目录(.exe的工作目录)和临时目录(实际上等于%~dp0,但没有尾部反斜杠)

我认为可以解决通过修补内存中的 cmd.exe 实例来更改脚本路径,但这是 B2EC 开发人员的职责。

旁注:通过为 CreateProcess 函数提供适当的 lpApplicationNamelpCommandLine ,可以使用指定的第 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 and lpCommandLine to CreateProcess function. Command files are executed via cmd.exe, so 0th argument cannot be set this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文