简体   繁体   English

如何检测是否从ZIP存档运行Windows批处理文件

[英]How to detect whether a Windows batch file is run from a ZIP archive

One of the ways our app is distributed (on Windows) is as a ZIP archive. 我们的应用程序分发方式之一(在Windows上)是ZIP存档。 Inside this archive is a batch file used for starting the app. 此存档内部是用于启动应用程序的批处理文件。 The user is supposed to unzip the archive before running our app, but frequently users ignore our instructions and try to run the app without unzipping the archive first. 用户应该在运行我们的应用程序之前解压缩存档,但是用户经常忽略我们的说明并尝试运行应用程序而不首先解压缩存档。 When users do this, the app fails to start, but it's not obvious (to the user) exactly what caused the failure. 当用户这样做时,应用程序无法启动,但是(对用户)确切地说是导致失败的原因并不明显。 We'd like to detect from within the batch file whether it's being run from inside a ZIP archive, and if so, show the user a message reminding him to unzip the archive first. 我们想从批处理文件中检测它是否从ZIP存档内部运行,如果是,则向用户显示一条消息,提醒他先解压缩存档。

However, it's not clear at all to me how to detect this condition. 但是,我根本不清楚如何检测这种情况。 While the batch variable %cmdcmdline% contains the command which ran the batch file, there seems to be no way to use the path to reliably tell whether the path points into a ZIP archive. 虽然批处理变量%cmdcmdline%包含运行批处理文件的命令,但似乎无法使用该路径可靠地判断路径是否指向ZIP存档。 For example, I put the following batch file, named test.bat, into a ZIP archive: 例如,我将以下名为test.bat的批处理文件放入ZIP存档中:

echo %cmdcmdline%
pause

The output, when run from within the archive, was: 从存档中运行时,输出为:

cmd /c ""C:\Users\liana\AppData\Local\Temp\Temp1_test.zip\test.bat" "

Now, it seems likely that Temp1_test.zip is a ZIP archive, not a real directory, but there's no guarantee that it is. 现在,它很可能是Temp1_test.zip是一个ZIP压缩文件,而不是一个真正的目录,但不能保证它是。 Clearly, Windows knows how to tell the difference, so there must be some more reliable way than simply checking whether ".zip" shows up in the path somewhere. 显然,Windows知道如何区分,所以必须有一些更可靠的方法,而不是简单地检查“.zip”是否出现在某个地方的路径中。 But how? 但是怎么样?

Your program isn't actually being run from "Within" the archive. 您的程序实际上并未从存档中的“内部”运行。 The archive is first expanded out to a temporary directory (The location for which can vary depending on which archiving tool is being used), before being run. 在运行之前,存档首先扩展到临时目录(其位置可能因使用的归档工具而异)。

With this in mind it becomes clear that it isn't ever possible to tell for sure one way or another, so perhaps the best thing to do is instead to check for the presence of the other files you need to run your program, relative to the batch file's location. 考虑到这一点,很明显,无论如何都无法确定这种或那种方式,所以最好的办法是检查运行程序所需的其他文件是否存在,相对于批处理文件的位置。

Check if another file in the archive exists in the same dir? 检查存档中的另一个文件是否存在于同一个目录中?

if not exist "%~dp0\someotherfile" goto print_please_unzip_ffs_message

%~dp0 is the directory of the bat file (drive and directory of argument zero, which is the bat-file itself) %~dp0是bat文件的目录(参数为零的驱动器和目录,这是bat文件本身)

Why does it fail? 为什么会失败? I presume you're missing some files because Windows only unzipped the batch file and not the entire archive. 我认为你错过了一些文件,因为Windows只解压缩批处理文件而不是整个存档。 If that's true you can check to see if those files exist when the batch file starts. 如果这是真的,您可以检查批处理文件启动时是否存在这些文件。

Or you could distribute your app as a self-extracting zip archive. 或者您可以将您的应用程序分发为自解压zip存档。 Doing that would strongly encourage users to run the SFX before running the app. 这样做会强烈鼓励用户在运行应用程序之前运行SFX。 (Although, (for some archivers) an SFX is also a ZIP, so they could rename the SFX to a .zip and then they'd be back to what you have today). (虽然,(对于一些归档者)SFX也是一个ZIP,因此他们可以将SFX重命名为.zip,然后他们将回到你今天拥有的东西)。

The Windows shell (explorer.exe) associates the .zip extension with the internal zip-folder handler. Windows shell(explorer.exe)将.zip扩展名与内部zip-folder处理程序相关联。 You can rename anything to .zip and the shell will attempt to treat it as such. 您可以将任何内容重命名为.zip,shell将尝试对其进行处理。 The zip-folder handler is a shell extension that, over time, has become fairly integrated into the shell. zip文件夹处理程序是一个shell扩展,随着时间的推移,它已经完全集成到shell中。

The Windows command line environment, which your batch file operates in, has no concept of zipped folders. 批处理文件操作的Windows命令行环境没有压缩文件夹的概念。 In that case all you can do is look for the .zip extension. 在这种情况下,您所能做的就是寻找.zip扩展名。

One thing you can do is distribute your package as an .msi instead, then you won't have these problems. 你可以做的一件事是将你的包分发为.msi,然后你就不会有这些问题。

If you create a batch file named "setup.bat" Windows will silently unzip ALL the files in the zip file to the temp directory instead of prompting the user if they want to do so. 如果您创建名为“setup.bat”的批处理文件,Windows将以静默方式将zip文件中的所有文件解压缩到临时目录,而不是提示用户是否要这样做。

In setup.bat you can then begin with cd /d %~dp0 to change to the currently executing directory. 在setup.bat中,您可以从cd /d %~dp0开始更改为当前正在执行的目录。 You can then just run your program as normal, or do some sort of installation. 然后,您可以正常运行程序,或进行某种安装。

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

相关问题 Windows 10:如何确定是否正在从网络映射的驱动器运行批处理文件 - Windows 10: How can I determine whether a batch file is being run from network mapped drive Windows中如何检测一个文件是否为可执行文件? - How to detect whether a file is an executable file in Windows? 批处理文件:-检测Windows版本并运行exe文件 - Batch file:- Detect windows version and run exe file 如何检测文件夹是否为空(Windows 批处理文件)? - How to detect if folder is not empty (Windows Batch File)? 如何在Windows批处理文件中编写命令以将zip文件从FTP路径复制到计算机中的本地文件夹 - How to write command in windows batch file to copy a zip file from an FTP path to a local folder in my computer 如何在Windows上从批处理文件运行FTP脚本? - How to run FTP script from batch file on Windows? 如何检测调用powershell脚本的批处理文件是否由Windows启动事件运行 - How to detect if batch file that calls powershell script was run by Windows startup event or not 如何测试 Zip 文件在 Windows 批处理文件中是否有效 - How to test if a Zip file is valid in a windows batch file 从Windows批处理文件运行Shell脚本 - Run shell script from windows batch file 从批处理文件在Windows中运行Tomcat 7 - Run Tomcat 7 in Windows from batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM