Solved

Check if string is valid windows directory

Posted on 2008-10-01
5
3,334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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