MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SetWorkingDir

Function SetWorkingDir

source/script2.cpp:7628–7667  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7626
7627
7628ResultType SetWorkingDir(LPTSTR aNewDir)
7629// Throws a script runtime exception on failure, but only if the script has begun runtime execution.
7630// This function was added in v1.0.45.01 for the reason described below.
7631{
7632 // v1.0.45.01: Since A_ScriptDir omits the trailing backslash for roots of drives (such as C:),
7633 // and since that variable probably shouldn't be changed for backward compatibility, provide
7634 // the missing backslash to allow SetWorkingDir %A_ScriptDir% (and others) to work as expected
7635 // in the root of a drive.
7636 // Update in 2018: The reason it wouldn't by default is that "C:" is actually a reference to the
7637 // the current directory if it's on C: drive, otherwise a reference to the path contained by the
7638 // env var "=C:". Similarly, "C:x" is a reference to "x" inside that directory.
7639 // For details, see https://blogs.msdn.microsoft.com/oldnewthing/20100506-00/?p=14133
7640 // Although the override here creates inconsistency between SetWorkingDir and everything else
7641 // that can accept "C:", it is most likely what the user wants, and now there's also backward-
7642 // compatibility to consider since this workaround has been in place since 2006.
7643 // v1.1.31.00: Add the slash up-front instead of attempting SetCurrentDirectory(_T("C:"))
7644 // and comparing the result, since the comparison would always yield "not equal" due to either
7645 // a trailing slash or the directory being incorrect.
7646 TCHAR drive_buf[4];
7647 if (aNewDir[0] && aNewDir[1] == ':' && !aNewDir[2])
7648 {
7649 drive_buf[0] = aNewDir[0];
7650 drive_buf[1] = aNewDir[1];
7651 drive_buf[2] = '\\';
7652 drive_buf[3] = '\0';
7653 aNewDir = drive_buf;
7654 }
7655
7656 if (!SetCurrentDirectory(aNewDir)) // Caused by nonexistent directory, permission denied, etc.
7657 return FAIL;
7658 // Otherwise, the change to the working directory succeeded.
7659
7660 // Other than during program startup, this should be the only place where the official
7661 // working dir can change. The exception is FileSelect(), which changes the working
7662 // dir as the user navigates from folder to folder. However, the whole purpose of
7663 // maintaining g_WorkingDir is to workaround that very issue.
7664 if (g_script.mIsReadyToExecute) // Callers want this done only during script runtime.
7665 UpdateWorkingDir(aNewDir);
7666 return OK;
7667}
7668
7669
7670

Callers 4

OpenIncludedFileMethod · 0.85
IsDirectiveMethod · 0.85
PerformMethod · 0.85
BIV_DECL_WFunction · 0.85

Calls 1

UpdateWorkingDirFunction · 0.85

Tested by

no test coverage detected