0

I am new to c++ programing and working with visual Studio 2015. I want to create a Folder with a Name of current date in a particular path. I searched on the Internet but I was not able to find the satisfactory solution. Moreover I tried by my own but unfortunately the result is not successful. I used _mkdir() and CreateFolder().

For example, if i compile the program today (07.07.2017) then it should create a Folder with a Name: "07072017" and if i compile it again then it should Show the message: "Folder already exists".

Thanks in advance.

DNS
  • 13
  • 4
  • So by "today's date" you mean the compilation date? Visual Studio provides a `__DATE__` macro that contains just that. See [here](https://webcache.googleusercontent.com/search?q=cache:f8HwK-mNIf8J:https://msdn.microsoft.com/en-us/library/b0084kay.aspx+&cd=1&hl=it&ct=clnk&gl=it&client=firefox-b-ab) – Federico klez Culloca Jul 07 '17 at 07:17
  • @FedericoklezCulloca it should create a folder when I run the program. – DNS Jul 07 '17 at 07:25
  • Well, assign date to a std::string, modify it to the format you want, pass it to mkdir() if directory doesn't yet exist? – stijn Jul 07 '17 at 07:50
  • `__DATE__` is standard C++ (not just VS) for getting the compile time (of the current compilation unit). It is a string literal in the form `mmm dd yyyy` (e.g. `"Jan 30 2017"`). Process using that to the form you need as a starting point. Then all you need is a means of creating a directory with a specified name - in C++17, that can be done with `create_directory()` in ``. – Peter Jul 07 '17 at 10:57

3 Answers3

1

You could try using WINDOWS API CreateDirectory function if we are talking about windows. More information : https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx

Use argv[0] to get your .exe current directory from main(int argc, char argv[]) and remember to do #include <windows.h>. You could also try to execute shell or cmd.exe with arguments mkdir folderName to make it.

As for retrieving current date i would recommend reading How to get current time and date in C++?

Shrouded
  • 21
  • 4
1

As mentioned by @user8210143 you can use the c++ standard library experimental bits.

If the date format isn't critical for your purpose, you could just use the __DATE__ compiler define:

Live On Coliru

#include <experimental/filesystem>
#include <iostream>

namespace fs = std::experimental::filesystem;

int main() {
    auto dirname = fs::current_path() / __DATE__;

    std::cout << "creating directory " << dirname << "\n";
    if (create_directories(dirname)) {
        std::cout << "directory didn't exist yet\n";
    }
}

Which prints something like

creating directory "/home/sehe/Projects/stackoverflow/Jul  7 2017"
directory didn't exist yet

On my system

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Can i Change the path of creating Directory also? For example I want to create a Folder in "E:/ProjectWork/Project/MyWork". Because when i am using current_path() it is creating a folder where program is saved. – DNS Jul 11 '17 at 12:49
  • Using `current_path()` obviously uses... the current path (which is the [working directory](https://en.wikipedia.org/wiki/Working_directory). Of course you can. Why is that a question? Just fill it anywhich way you want: `fs::path("E:/ProjectWork/Project/MyWork") / __DATE__;` – sehe Jul 11 '17 at 13:26
-1

Here is an example using <experimental/filesystem> and visual studio.

#include "stdafx.h"
#include <iostream>
#include <experimental\filesystem>

using namespace std;

int main() {
int sizeofdate = strlen(__DATE__);
char* dirname = new char[4 + sizeofdate];
memcpy(dirname, "C:/", 3);
memcpy(dirname + 4, __DATE__, sizeofdate);

cout << "creating directory" << endl;
if (!experimental::filesystem::exists(dirname)) {
    experimental::filesystem::create_directory(dirname);
}
else {
    cout << "directory already exists!" << endl;
}

delete[] dirname;

int thing;
cin >> thing;

return 0;
}
  • @user821043 while runnuing your Code I am getting thiis error: Unhandled exception at 0x77B0A9F2 in ConsoleApplication1.exe: Microsoft C++ exception: std::experimental::filesystem::v1::filesystem_error at memory location 0x00AFF504. – DNS Jul 07 '17 at 08:36
  • 1
    This can be answered using portable C++, not "Visual Studio". Also, memcpy and new have no place in modern C++. It's bad advice to give. – sehe Jul 07 '17 at 09:36
  • Same thing for `delete`, lack of exception safety (leaks), `using namespace` (see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Thank you for contributing, I do realize you are just starting out. Keep in mind that answering on SO is a fast track to learning modern C++ (again), so feedback is really just extra win for us! – sehe Jul 07 '17 at 09:49
  • I appreciate feedback, it helps me learn. I don't like using 40 byte strings and stuff like that, I stick with char*. I don't think error checking is necessary in a simple example program. Not really trying to learn modern c++ you would use in the office. As for the error, I'm not getting an error. So I don't know what the problem is. – user8210143 Jul 07 '17 at 10:05
  • 40 byte strings? – The Techel Jul 07 '17 at 10:24
  • You could have told me my delete was incorrect instead of just alluding to it =/. Idk why I thought char* was different in that case... char* should be deleted with delete[]. Also, how did you concatenate strings with a `/`. That's slick. – user8210143 Jul 07 '17 at 10:35
  • c++ strings are generally around 40-48 bytes. Do a sizeof. – user8210143 Jul 07 '17 at 10:36
  • I changed my code right before I copy pastad it here, didn't test it. Sorry, noob mistake. I fixed the problem. – user8210143 Jul 07 '17 at 10:52
  • What should I do instead of memcpy? I really want to use char* keep in mind. I would rather not make an extra filesystem::path either. I remember now, filesystem::path overloads / for easy concats. memcpy is better than iterating over the string in a loop at least. – user8210143 Jul 07 '17 at 11:04