Solved

Check if string is valid windows directory

Posted on 2008-10-01
5
3,305 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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 214
fix34  challenge 9 115
Need some help with Microsoft Visual Studio C++ 2003 5 59
Best book to learn C++ 4 78
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now