简体   繁体   English

如何在%appdata%中创建文件夹,在其中创建.bat文件,然后执行它?

[英]How to create folder in %appdata%, create .bat file in it, and then execute it?

I'm creating C++ code that will create some .bat file and store it in the %appdata% folder. 我正在创建C ++代码,它将创建一些.bat文件并将其存储在%appdata%文件夹中。 I've successfully to created the file, but still fail to create the folder and execute it. 我已成功创建该文件,但仍无法创建该文件夹并执行它。

Below is my simple code, it doesn't look simple but it works to create .bat file in %appdata% , maybe someone can help me to find the simple one. 下面是我的简单代码,它看起来并不简单,但它可以在%appdata%创建.bat文件,也许有人可以帮我找到简单的。

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#include <direct.h>

int main(int argc, char **argv) {
  using namespace std;
  std::ofstream aaa;
  ostringstream aaa;
  aaa.open(aaa1.str());
  aaa1 << getenv("appdata") << "/"
       << "test.bat";
  aaa.open(aaa1.str());
  Updater << "@echo on" << endl;
  Updater << "echo \"on\"" << endl;
  return 0;
}

The code successfully creates the .bat file in %appdata% , but I need to store in new folder in %appdata% , say New Folder , and then execute the .bat file. 代码在%appdata%成功创建了.bat文件,但是我需要在%appdata%新文件夹中存储,比如说New Folder ,然后执行.bat文件。

Creating/running an executable in a user writable location is something to be careful with (exploit people into running your process elevated, then running an attack payload), otherwise just a couple of things to tie together. 在用户可写位置创建/运行可执行文件是需要注意的事项(利用人员来提升运行流程,然后运行攻击负载),否则只需要将几件事情联系在一起。

On Windows, most of those environment variables exist for legacy / compatibility reasons, SHGetKnownFolderPath is the modern way to find the folders. 在Windows上,由于遗留/兼容性原因,大多数环境变量都存在, SHGetKnownFolderPath是查找文件夹的现代方法。 It allocates enough space for the path, be careful with manual memory from C-API, get it a unique_ptr or wstring as soon as possible. 它为路径分配足够的空间,小心C-API的手动内存,尽快得到unique_ptrwstring It works from Vista, there are older API's if really needed. 它适用于Vista,如果确实需要,还有较旧的API。

wchar_t *str = nullptr;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &str); // CHECK RETURN
...use str...
CoTaskMemFree(str);

Also be aware of Unicode and spaces in file paths. 还要注意文件路径中的Unicode和空格。

Processes have two options, there is the system(command_line) in the cstdlib header, or for advanced use check out the Windows CreateProcessW . 进程有两个选项, cstdlib头中有system(command_line) ,或者高级用户检查Windows CreateProcessW Something like: 就像是:

STARTTUPINFO startup;
startup.cb = sizeof(startup);
PROCESS_INFORMATION pi;
CreateProcessW(NULL, L"cmd.exe /C C:\\ThePath\\myfile.bat", NULL, NULL, FALSE, 0, NULL, NULL, &startup, &pi);

Obviously specific to Windows. 显然特定于Windows。 Linux, Mac, etc. have their own filesystem layouts and security. Linux,Mac等都有自己的文件系统布局和安全性。

C++ fstream won't create directories for you automatically. C ++ fstream不会自动为您创建目录。 You could set up such directories as part of an installer, but to do it at runtime C++17 has std::filesystem::create_directories , which takes a path. 您可以将这些目录设置为安装程序的一部分,但要在运行时执行此操作C ++ 17具有std::filesystem::create_directories ,它采用路径。 If you can't use C++17, use CreateDirectory or _mkdir . 如果您不能使用C ++ 17,请使用CreateDirectory_mkdir Again on Windows be aware of Unicode. 在Windows上再次注意Unicode。

Create Directory 1st get the path using _dupenv_s() in string add new folder name "\\New Folder" 创建目录 1st获取路径使用_dupenv_s()在字符串中添加新文件夹名称“\\ New Folder”
2nd Create Directory using _mkdir(str.c_str()); 第二个使用_mkdir创建目录(str.c_str()); 3rd Create "test.bat" using std::ofstream outf(str); 3rd使用std :: ofstream outf(str)创建“test.bat”;

 #include "stdafx.h" #include<fstream> #include<iostream> #include<conio.h> #include<direct.h> using std::cout; using std::cin; using std::endl; int tmain(int argc, TCHAR* argv[]) { char *pValue; size_t len; errno_t err = _dupenv_s(&pValue, &len, "APPDATA"); std::string NewFile = "\\\\new"; std::string str(pValue); str = str + NewFile; _mkdir(str.c_str()); str = str + "\\\\Sample.bat"; // std::ofstream outf(str); if (!outf) { printf("error "); } outf << "this is line1" << endl; outf << "line 2" << endl; return 0; } 
Please! 请! Don't Forgot to Vote If its Helps 如果有帮助,不要忘记投票

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM