Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if string is valid windows directory

Posted on 2008-10-01
5
Medium Priority
?
3,368 Views
Last Modified: 2013-11-20
I have a dialog box where the user enters a string for a directory name.
I need to check to see if the string is valid for a windows directory before it is created, such as it is not past the maximum path length, doesnt contain invalid characters, etc. I don't know if there's a function to do this, or do i have to do it manually.
0
Comment
Question by:steenpat
  • 3
5 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 22619863
well, first you should separate directories from left:
C:\abc\123\def\456 :

abc
123
def
456
 
then, cat directories and call CreateDirectory() API function. this API function returns BOOL and if fail will return 0. you can get errors reason using GetLastError() API function. so, you can find out that why CreateDirectory() failed -> because of exist of directory or include illegal characters

Regards
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 22620216
check out this page, it has a generic c++ (cross platform) and windows only implementation

http://www.tek-tips.com/viewthread.cfm?qid=960814&page=1
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22622884
as I told you start creating directory from left and if CreateDirectory returned 0, get last error using GetLastError API that returns DWORD.
if(GetLastError() == 208)  // or if(GetLastError() == ERROR_META_EXPANSION_TOO_LONG)
//means that has invalid character, so tell user to not enter invalid character

Error Code: 208
Description: The global filename characters, * or ?, are entered incorrectly or too many global filename characters are specified.  
Constant value name for use in code: ERROR_META_EXPANSION_TOO_LONG
(from MSDN)

Regards...
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22622963
for this code you should just copy folder name to foldername variable every time you get filenames.
for ex.
c:\test1\test2\test3
first copy test1 to foldername variable:
strcpy(foldername,"test1");
then test2
strcpy(foldername,"test2");
finally
strcpy(foldername,"test3");
after every copy you should use my if statement.
Regards

#include <windows.h>
#include <iostream.h>
#include <string.h>
 
int main()
{
//invalid characters for folder or file name are these characters:   \/:*?"<>|
char foldername[260];
strcpy(foldername,"test_of_folder_name:");
if(strchr(foldername,'\\') == NULL && strchr(foldername,'/') == NULL && strchr(foldername,':') == NULL && strchr(foldername,'*') == NULL && strchr(foldername,'?') == NULL && strchr(foldername,'\"') == NULL && strchr(foldername,'<') == NULL && strchr(foldername,'>') == NULL && strchr(foldername,'|') == NULL)
cout<<"valid"<<endl;
else
cout<<"invalid"<<endl;
system("pause");
}

Open in new window

0
 
LVL 4

Accepted Solution

by:
anthony_w earned 200 total points
ID: 22623353
You can use the Path functions from shell32.dll to help you.

PathCleanupSpec takes a directory and a file/directory name and reports whether the file/directory name is valid in the specified directory, and corrects it if possible.

PathGetCharType tells you whether a particular character is a valid part of a path, and if so what it is (e.g. directory separator, only valid in a long name, etc.)

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question