Jump to content

check if the input is a directory path or not


Recommended Posts

Hi
before creating a folder with DirCreate, i want to verify if the input path is a valid directory path and not a file path.
As long as the file does not exist yet, it is not possible to use FileGetAttrib to discriminate .
How can i do this ?

Edited by ericire
Link to comment
Share on other sites

2 minutes ago, ericire said:

FileExists () is for directories or files that are already exist, here i want to verify before creating the directory

Wrong read help file :

Return Value

Success: 1.
Failure: 0 if path/file does not exist.
Link to comment
Share on other sites

How will you determine that what the user wants to create for a folder name is a file path or a directory name? What is your criteria?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

A directory name with a dot in it is a valid path.  DirCreate will not create a file, only a directories.  See the example below.  No file is created.  

Global $sDir = "Dir1\Dir2\Dir.csv" ;Dir.csv is a directory, not a file.  
If Not DirCreate($sDir) Then Exit 1

Now if you want to restrict directory names, more so than Windows, that is another thing.  

 

Adam

Link to comment
Share on other sites

This "WiFi_Configuration Ver 3.6" is a valid directory name, and a real one on my computer. This isn't a file, and there's no way you're going to be able to differentiate between a file name and a directory name using a criteria of "if it has a dot and something after it, it's a file name"

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

An Isfile or isdirectory function would not matter as a dot is a valid character in a directory name.  If you are just worried about "extensions," then you can do something like this.  This will work with a path with an "extension" or not.  

Global $sDir = "Dir1\Dir2\Dir.csv"
$sDir = StringRegExpReplace($sDir, "\.[^.]*$", "")  
If Not DirCreate($sDir) Then Exit 1

 

Adam

Edited by AdamUL
Link to comment
Share on other sites

@ericire Wrong answer!

Local $aPath = [@ScriptName, ".", "C:\", "H:\MyFolder.special.version 1.2.3.4"]

For $s In $aPath
    _WhatIsPath($s)
Next

Func _WhatIsPath($sPath)
Local $ret = FileGetAttrib($sPath)
ConsoleWrite('"' & $ret & '"' & @CRLF)
    Select
        Case $ret = ""
            MsgBox(0, "Result", $sPath & " doesn't exist yet." & @LF & "It *may be* OK to create that directory.")
        Case StringInStr($ret, "D")
            MsgBox(0, "Result", $sPath & " is already a directory with attributes " & $ret & @LF & "No need to create it.")
        Case Else
            MsgBox(0, "Result", $sPath & " is an existing file with attributes " & $ret & @LF & "Can't create a directory with this name.")
    EndSelect
EndFunc

I wonder if you ever read the answer you got here.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The simple answer is, you can't

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...