Check if path is directory or file

13 posts in this topic

#1 ·  Posted (edited)

What would be the best way to check if a specified path is a directory or a file?

I have a script that allows people to enter a source path and a destination path. If the user enters a source and destination directory like:

```C:\myDir
D:\DestinationDir```

it will copy ALL files

But I need to also be able to handle:

```C:\myDir\*.exe
D:\DestinationDir```

and

```C:\myDir\program.*
D:\DestinationDir```

and

```C:\myDir\*.*
D:\DestinationDir```

What would be the best way to check the string for whether or not it contains a full or partial filename? I was gonna use "FileExists" and based on the error i would decide, but FileExists also supports DirectoryExists functionality (probably a bad idea). So I can't think of any other method.

Edited by DssTrainer

Share on other sites

FileGetAttrib()

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Share on other sites

FileGetAttrib()

D'oh! Genius! Thanks!

Share on other sites

Hmm Doesn't seem to work if I use UNC paths: \\netname\c\$\test\xml\myfile.xml

just returns a null.

Share on other sites

And what will return this:

FileExists("\\netname\c\$\test\xml\nul")

?

The \\ is defined as root dir (same as StringLeft(@ScriptDir, 2) & "\netname\c\$\test\xml\nul") i think, so this file i supose, is not exists .

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

My Work...

Spoiler

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating )

* === My topics === *

==================================================

==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share on other sites

#6 ·  Posted (edited)

And what will return this:

FileExists("\\netname\c\$\test\xml\nul")

?

The \\ is defined as root dir (same as StringLeft(@ScriptDir, 2) & "\netname\c\$\test\xml\nul") i think, so this file i supose, is not exists .

Your example is incorrect. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share on other sites

And what will return this:

FileExists("\\netname\c\$\test\xml\nul")

?

The \\ is defined as root dir (same as StringLeft(@ScriptDir, 2) & "\netname\c\$\test\xml\nul") i think, so this file i supose, is not exists .

But \\ is the standard (for windows anyway) to signify a network name.

Share on other sites

have you tried running that as a different user?

Share on other sites

#9 ·  Posted (edited)

have you tried running that as a different user?

Well I can reach that path through Windows directly, and my "FileFindFirstFile(\$path/*.*)" works on the UNC path. DirGetSize also doesn't work on UNC paths.

Most Attribute functions don't work on network paths. I think it might be for other programs, not just AutoIt.

Edited by DssTrainer

Share on other sites

Well I can reach that path through Windows directly, and my "FileFindFirstFile(\$path/*.*)" works on the UNC path. DirGetSize also doesn't work on UNC paths.

Most Attribute functions don't work on network paths. I think it might be for other programs, not just AutoIt.

You have some other issue. This works fine for me on a networked UNC path:

```\$path = "\\my_server\my_share\my_directory"
\$file = \$path & "\my_file.txt"
If FileExists(\$path & "\") Then
MsgBox(64, "Attribs: Dir", FileGetAttrib(\$path & "\"))
MsgBox(64, "Size: Dir", DirGetSize(\$path & "\"))
MsgBox(64, "Attribs: File", FileGetAttrib(\$file))
Else
MsgBox(16, "Error", "Didn't see UNC path")
EndIf```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share on other sites

You have some other issue. This works fine for me on a networked UNC path:

```\$path = "\\my_server\my_share\my_directory"
\$file = \$path & "\my_file.txt"
If FileExists(\$path & "\") Then
MsgBox(64, "Attribs: Dir", FileGetAttrib(\$path & "\"))
MsgBox(64, "Size: Dir", DirGetSize(\$path & "\"))
MsgBox(64, "Attribs: File", FileGetAttrib(\$file))
Else
MsgBox(16, "Error", "Didn't see UNC path")
EndIf```

Hmm. i will have to see whats going wrong here. Thanks for trying that.

Share on other sites

#12 ·  Posted (edited)

How can we check whether a value stored in a string is file or folder? Both in UNC and Local paths.

Edited by ur

Share on other sites

#13 ·  Posted (edited)

I am not sure that this question justifies resurrecting and 8 years old thread but check for a "D" in the result of FileGetAttrib(), which was already given as answer earlier in this thread!
It works both for UNC and LocalPaths for me.

Jos

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.