windows 批处理:运行exe,将文件复制到appdata,并启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7624010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:11:19  来源:igfitidea点击:

Batch: Running exe, copying file to appdata, and put it in startup

windowsbatch-filecopy

提问by Thew

For example, i have 2 exe's. Let's call them 1.exe and 2.exe, to keep it simple. And i want to make a zip file, with 3 things in it, 1.exe, 2.exe and setup.bat.

例如,我有 2 个 exe。为了简单起见,我们称它们为 1.exe 和 2.exe。我想制作一个 zip 文件,里面有 3 个东西,1.exe、2.exe 和 setup.bat。

First off, i want to know that the user is okay that we start the first exe (1.exe). So we type:

首先,我想知道用户是否可以启动第一个 exe(1.exe)。所以我们输入:

@echo off
cls
echo Are you sure you want to install 1.exe? 
echo If not, click exit right now. If you are okay with it,
pause

Here comes the first question. So we want to start 1.exe. How do i start 1.exe, that is in the same folder as the bat file?

第一个问题来了。所以我们要启动1.exe。如何启动与 bat 文件位于同一文件夹中的 1.exe?

Okay, lets continue. When 1.exe is finished, i want to copy 2.exe, place it in %appdata%, and then add it to startup. And thats the second question. How do i do that.

好的,让我们继续。当1.exe完成后,我想复制2.exe,把它放在%appdata%. 这就是第二个问题。我怎么做。

So the questions are:
1. How do i start 1.exe, wich is in the same map as setup.bat
2. How do i copy 2.exe wich is in the same map as setup.bat to %appdata%
3. How do i properly add 2.exe wich is in %appdata%now to startup?

所以问题是:
1. 我如何启动 1.exe,它与 setup.bat 位于同一映射中
2. 我如何将与 setup.bat 位于同一映射中的2.exe 复制到%appdata%
3. 我如何正确添加%appdata%现在启动的2.exe吗?

Note: Just using C:\documents and settings\all users\desktop\1.exeisn't going to work. I want it to work in all sorts of languages, and in some languages the folders might be called different.

注意:仅使用C:\documents and settings\all users\desktop\1.exe是行不通的。我希望它可以在各种语言中工作,并且在某些语言中,文件夹的名称可能不同。

回答by Dave Newton

1.exewill run 1.exe, just like on the command line. copy 2.exe %appdata%will copy 2.exe. I don't know what question 3 means.

1.exe将运行1.exe,就像在命令行上一样。 copy 2.exe %appdata%将复制2.exe。我不知道问题3是什么意思。

Define "work in all sorts of languages"? If you need to pass in an argument to the batch file, do so: http://commandwindows.com/batch.htm

定义“以各种语言工作”?如果您需要将参数传递给批处理文件,请执行以下操作:http: //commandwindows.com/batch.htm

回答by PA.

You are right you should never hard code "Documents and Settings" or "Program Files" in a BAT file, because these folder names don't "work in all sorts of languages". You need to refer to them using special folder ids or environment variables.

你是对的,你永远不应该在 BAT 文件中对“文档和设置”或“程序文件”进行硬编码,因为这些文件夹名称并不“适用于各种语言”。您需要使用特殊文件夹 ID 或环境变量来引用它们。

In your case, you need to create a program shortcut (.LNK file) in the startup folder. There are two parts.

在您的情况下,您需要在启动文件夹中创建一个程序快捷方式(.LNK 文件)。有两个部分。

  1. creating a shortcut. Unfortunately there is no way to create a shortcut using only windows commands. You need to rely on a third party tool, there are many free command line tools that may do it; or write your own.

  2. locating the Startup folder and placing the shortcut there. There are two startup folders. The common startupand the user startupfolder. Choose one. Then, you need to use either the %ALLUSERSPROFILE%\Start Menu\Programs\StartUpor the %USERPROFILE%\Start Menu\Programs\StartUp.

  1. 创建快捷方式。不幸的是,无法仅使用 Windows 命令创建快捷方式。你需要依赖第三方工具,有很多免费的命令行工具可以做到;或者自己写。

  2. 找到启动文件夹并将快捷方式放在那里。有两个启动文件夹。在常见的启动用户启动文件夹中。选一个。然后,您需要使用%ALLUSERSPROFILE%\Start Menu\Programs\StartUp%USERPROFILE%\Start Menu\Programs\StartUp

So putting all pieces together in your SETUP.BAT, it would look something like this...

所以把所有的部分放在你的 . 中SETUP.BAT,它看起来像这样......

@echo off
echo Are you sure you want to install 1.exe? 
echo If not, click exit right now. If you are okay with it,
pause
1
copy 2.exe %appdata%
makelink %appdata%.exe %USERPROFILE%\Start Menu\Programs\StartUp.lnk

One suggestion. Avoid all this mess. It seems to me that you need to install a program. If so, I'd recommend you to try Inno Setup. http://www.jrsoftware.org/.

一个建议。避免所有这些混乱。在我看来,您需要安装一个程序。如果是这样,我建议您尝试Inno Setuphttp://www.jrsoftware.org/

Inno Setup is a free installer for Windows. First introduced in 1997, Inno Setup today rivals and even surpasses many commercial installers in feature set and stability.

Inno Setup 是 Windows 的免费安装程序。Inno Setup 于 1997 年首次推出,如今在功能集和稳定性方面可与许多商业安装程序相媲美,甚至超过了许多。

...

...

  • Supports creation of a single EXE to install your program for easy online distribution. Disk spanning is also supported.

  • Standard Windows 2000/XP-style wizard interface.

  • Customizable setup types, e.g. Full, Minimal, Custom.

  • Complete uninstall capabilities.

  • Installation of files: Includes integrated support for "deflate", bzip2, and 7-Zip LZMA/LZMA2 file compression. The installer has the ability to compare file version info, replace in-use files, use shared file counting, register DLL/OCX's and type libraries, and install fonts.

  • Creation of shortcuts anywhere, including in the Start Menu and on the desktop.

  • Creation of registry and .INI entries.

  • Running other programs before, during or after install.

  • ...

  • 支持创建单个 EXE 来安装您的程序,以便于在线分发。还支持磁盘跨越。

  • 标准的 Windows 2000/XP 风格的向导界面。

  • 可自定义的设置类型,例如完整、最小、自定义。

  • 完整的卸载功能。

  • 文件安装:包括对“deflate”、bzip2 和 7-Zip LZMA/LZMA2 文件压缩的​​集成支持。安装程序能够比较文件版本信息、替换正在使用的文件、使用共享文件计数、注册 DLL/OCX 和类型库以及安装字体。

  • 在任何地方创建快捷方式,包括在开始菜单和桌面上。

  • 创建注册表和 .INI 条目。

  • 在安装之前、期间或之后运行其他程序。

  • ...

回答by Bali C

This should do what you want.

这应该做你想做的。

@echo off
cls
echo Are you sure you want to install 1.exe? 
echo If not, click exit right now. If you are okay with it,
pause
start /wait 1.exe
xcopy 2.exe %appdata% /y
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v "2" /d %appdata%.exe

The last line will make a reg entry instead of copying it to the startup folder which will not create a shortcut on the desktop and you don't need anything more than batch.

最后一行将创建一个 reg 条目,而不是将其复制到启动文件夹,这不会在桌面上创建快捷方式,而且您只需要批处理。