Skip to content

Commit b607a1a

Browse files
committed
Ensure script path exists on "New Script"
If the Scripts directory didn't exist, then the script would be saved in "My Documents" (or some other default), which would mean it would not be available. Directory (and any missing parents) are now created upon New Script if they're not there.
1 parent daad094 commit b607a1a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

PythonScript/src/PythonScript.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,46 @@ static void showConsole()
553553
}
554554
}
555555

556+
static void ensurePathExists(const tstring& path)
557+
{
558+
BOOL created = FALSE;
559+
tstring createPath(path);
560+
if (!::CreateDirectory(createPath.c_str(), NULL))
561+
{
562+
std::list<tstring> pathsToCreate;
563+
564+
// Add the deepest directory to the top of the list, so it will be created last
565+
pathsToCreate.push_back(createPath);
566+
567+
do {
568+
569+
createPath.erase(createPath.find_last_of(_T('\\')));
570+
if (!::CreateDirectory(createPath.c_str(), NULL))
571+
{
572+
pathsToCreate.push_back(createPath);
573+
}
574+
else
575+
{
576+
created = TRUE;
577+
}
578+
579+
} while (createPath.find(_T('\\')) != tstring::npos
580+
&& !created);
581+
582+
if (created)
583+
{
584+
for(std::list<tstring>::reverse_iterator iter = pathsToCreate.rbegin(); iter != pathsToCreate.rend(); iter++)
585+
{
586+
::CreateDirectory(iter->c_str(), NULL);
587+
}
588+
}
589+
}
590+
else
591+
{
592+
created = TRUE;
593+
}
594+
}
595+
556596
static void newScript()
557597
{
558598

@@ -561,6 +601,7 @@ static void newScript()
561601

562602
ofn.lStructSize = sizeof(OPENFILENAMEA);
563603
ofn.hwndOwner = nppData._nppHandle;
604+
ensurePathExists(ConfigFile::getInstance()->getUserScriptsDir());
564605
std::shared_ptr<char> userScriptsDir = WcharMbcsConverter::tchar2char(ConfigFile::getInstance()->getUserScriptsDir().c_str());
565606
ofn.lpstrInitialDir = userScriptsDir.get();
566607
//ofn.lpstrFileTitle = "Choose filename for new script";

0 commit comments

Comments
 (0)