Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if string is valid windows directory

Posted on 2008-10-01
5
Medium Priority
?
3,358 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 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

722 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