Checking to see if a Folder Exist

I am writing a little program to do some functions on some PC our company sells. The PC have a security feature that locks them down to prevent tampering. However, in order to do things, this feature can be temporary disabled. The program I am writing requires the PC to be "unlocked" prior to continuing. For our PCs running Windows 10 I have this sorted out and that part of my code works fine. However for our Windows 7 PCs I am having issues. The Windows 7 PCs had an old Security setup which really only altered the GroupPolicy folder by changing its name and ran gpudate. Well I am trying to see if the _GroupPolicy folder exist or not and that is where I am hitting a wall.

I posted a test code I using to see if I can get the right results so the code is not from the actual program but rather just to test the results. Regardless of how i set it up I get the same results of it running the if part rather than the else part even if the GroupPolicy folder has been renamed _GroupPolicy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 //

#include <iostream>
#include <cstring>
#include <fstream>
#include <io.h>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <Windows.h>
using namespace std;

int main()
{

	int osver = 0.0;

	NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW);

	OSVERSIONINFOEXW osInfo;

	*(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");

	if (NULL != RtlGetVersion)
	{
		osInfo.dwOSVersionInfoSize = sizeof(osInfo);
		RtlGetVersion(&osInfo);
		osver = osInfo.dwMajorVersion;
	}

	if ((osver) > 9) {

		// Windows 10 IoT Devices

		struct stat st;
		if (stat("C:\\Synectics\\PSA_LOCK", &st) == 0) {

			
			cout << endl << endl << "Locked" << endl << endl << endl << endl;

			Sleep(7000);

		}

		else {

			cout << endl << endl << "Unlocked" << endl << endl << endl << endl;

		}
	}

	else {

		// Windows 7 Embedded Clients

		struct stat st;
		if (stat("C:\\Windows\\System32\\_GroupPolicy\\", &st) == 0) {



			cout << endl << endl << "   The client does not apear to be unlocked.  Check lock status and run again once unlocked." << endl << endl << endl << endl;

			Sleep(7000);

		}

		else {

			std::cout << "Windows 7\n";

		}

	}

	Sleep(7000);

	return 0;
}
Under windows it should be _stat not stat. Why is there an extra backslash at the end of the path? Try remove it.

However, if you have access to a recent compiler (which supports c++17) you should consider using std::filesystem. With this it's easy to check whether the path exists. See:

https://en.cppreference.com/w/cpp/filesystem/exists

filesystem also exist in boost.
It seems it could be something to do with permssions for both _stat and C++17 filesystem exists(). Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <filesystem>
#include <iostream>

int main()
{
	if (std::filesystem::exists("c:\\Windows\\System32\\GroupPolicyTest"))
		std::cout << "GroupPolicyTest exists\n";
	else
		std::cout << "GroupPolicyTest not found\n";

	if (std::filesystem::exists("c:\\Windows\\System32\\GroupPolicy"))
		std::cout << "GroupPolicy exists\n";
	else
		std:: cout << "GroupPolicy not found\n";
}


A partial listing of my system32 folder is:


30/09/2012  12:46    <DIR>          GroupPolicy
17/09/2020  12:27    <DIR>          GroupPolicyTest
14/07/2009  03:34    <DIR>          GroupPolicyUsers


and the output from the program is:


GroupPolicyTest not found
GroupPolicy exists


????

Last edited on
coder777 if I remove the last backslash at the end of the path the whole line errors out and so does all the following code.

I tried what seeplus did and was getting errors that filesystem was not valid and exist is not part of the std.

I think seeplus is right though as this might have to do with permissions.
Topic archived. No new replies allowed.