windows CMD: Bat to Exe Converter - 临时目录问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1992393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMD: Bat to Exe Converter - Temp directory problem
提问by Deniz Zoeteman
i am using 'Bat to Exe Converter'to convert my batch files to exe format.
我正在使用“Bat to Exe Converter”将我的批处理文件转换为 exe 格式。
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:
现在,我遇到了一些问题。每当我转换某些内容,并将“工作目录”设置为“当前目录”,并以echo on
模式启动我的 exe 时,这就是我最终检查 exe 目录中是否有特定文件的结果:
the actual command: if not exist "%~dp0\file.txt" goto :nofile
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
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.
谁能帮我这个?我不希望它在临时目录中,我希望它在我的 exe 目录中。
Thanks.
谢谢。
回答by Doc Brown
Without having Bat to Exechanged by the author, I think you have two options:
在没有作者将Bat更改为 Exe的情况下,我认为您有两种选择:
Remove the need for accessing %~dp0
Perhaps you can merge
file.txt
with the include option of Bat to Exeinto 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 lineYourCompiled.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.
无需访问 %~dp0
也许您可以将Bat to Exe
file.txt
的 include 选项合并到 EXE 文件中。如果是这样,运行你编译的exe时会自动解压到当前目录下,你可以通过."file.txt"
%CD%\file.txt
%~dp0
从外部获取并将其作为命令行参数传递给exe。这可以通过一个简单的启动
bat
文件来完成,该文件与您编译的主批处理文件位于同一目录中。此脚本应该包含该行YourCompiled.Exe %~dp0% %%*
您编译的 exe 然后从 %1 获取其目录。所以你不能把所有东西都打包成一个 exe,但它的主要部分,也许对你来说就足够了。
回答by Joey
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.
好吧,显然您的批处理到 exe 转换器只是将批处理文件打包并在执行前将其解压缩到一个临时目录中。非常简单,很难出错(与实际理解批处理文件相比),但它引入了诸如您所描述的错误。
Your best bet is probably to use another batch to exe converter; some of them are actually a little more sophisticated.
您最好的选择可能是使用另一个批处理到 exe 转换器;其中一些实际上更复杂一些。
回答by ghostdog74
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,
一般来说,这不是一个好主意。首先,转换器在不同的 cmd 功能上容易出错和不稳定。其次,坚定的黑客仍然可以解码您对批处理所做的事情。我的建议,如果你害怕别人看你的批次,
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.
回答by RLH
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.
这是一个 2.5 岁的主题,但有一个答案,所以我发布给任何在搜索中碰巧找到这个的人。
B2EC written by Fatih Kodak, has an option to "Submit current directory".
B2EC 由 Fatih Kodak 编写,具有“提交当前目录”的选项。
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).
使用时,您可以在批处理文件中引用 %1 以获取执行的 EXE 的路径(而不是实际运行的提取 BAT 的路径)。
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.
将鼠标悬停在 UI 中的该选项上会显示“提交当前工作目录作为最后一个参数”。我使用的“最后一个参数”一直是 %1,但您可以测试您的代码以确保。
回答by Robert Wigley
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.
在撰写本文时,Fatih Kodak 的 Bat to Exe 的最新版本 2.1.4 在运行时创建了一个环境变量,可以替换 %~dp0 以引用 Exe 的路径。因此,您可以简单地将原始批处理文件中的 %-dp0 替换为 %b2eprogrampathname%。
回答by pollaris
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,
您可以在 f2ko 的批处理到 exe 转换器中使用外部文件夹。有一个单独的子程序文件夹可以整理项目文件夹。要调用位于 mysubroutinesfolder\mysubroutine 中的 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:
其中 xqt.cmd 是一个为您调用的程序:
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:
(%* 表示“所有参数”)。通过这种方式,您的批处理程序作为批处理运行,并且未修改,它们将使用 bat 到 exe 转换器进行编译,创建一个完全独立于文件夹的可执行文件。选择“临时目录”,并像往常一样用光标“全选”将所有子例程/可执行文件包含在 mysubroutines 文件夹中,然后点击“复制”。确保也包含 xqt.cmd 程序;将它放在 mysubroutines 文件夹的“外部”。确保您的主程序可以访问它。如果您在 x64 机器上运行,请记住选择 x64,否则可执行文件将找不到 SYSTEM32 文件。您可以在以下位置找到 f2k0 的批处理到 exe 转换器:
回答by isuru arunoda
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).
试试这个批处理脚本的开发环境,批处理编译器。它拥有开发批处理程序所需的一切。并编译成稳定的独立可执行文件 (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
快速下载:http: //bc.gotek.info/files/BatchCompiler159.zip
Cheers!
干杯!
回答by przemoc
Use %CD%
instead of %~dp0
.
使用%CD%
代替%~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.
B2EC 不是真正的转换器。选择安装的.cmd 文件的创建位置,%TEMP%
这是一个不错的选择。应用程序只是缺少脚本工作目录的第三个选项 - .exe 文件目录。我建议你给作者发邮件添加这个。
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.
.exe 和创建的 .cmd 的不同路径导致信息丢失,即我们无法同时知道 .exe 目录和当前目录而不向脚本提供附加信息(例如使用环境变量或将其作为第一个/最后一个参数传递)到脚本)。这个脚本需要处理它,我们将结束为此转换器量身定制的 cmd 脚本,这很糟糕。
%~dp0
- script directory (%TEMP%/.../
) - practically useless
%~dp0
- 脚本目录 ( %TEMP%/.../
) - 几乎没用
%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)
%cd%
- 工作目录(在转换器中设置) - 目前只有 2 个选项:当前目录(.exe 的工作目录)和临时目录(实际上等于%~dp0
,但没有尾部反斜杠)
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.
我认为可以通过修补cmd.exe
内存中的实例来更改脚本路径来解决,但这是 B2EC 开发人员的职责。
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.
附注:正常的可执行文件,可以很容易地通过提供appriopriate与指定零参数执行lpApplicationName
,并lpCommandLine
以CreateProcess
功能。命令文件通过 执行cmd.exe
,因此第 0 个参数不能以这种方式设置。
回答by BlueRaja - Danny Pflughoeft
%cd% will give you the current directory:
%cd% 会给你当前目录:
if not exist "%CD%\file.txt" goto :nofile