Solved

Check if string is valid windows directory

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now