DssTrainer

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 this post


Link to post
Share on other sites



FileGetAttrib()

D'oh! Genius! Thanks!

Share this post


Link to post
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 this post


Link to post
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 :whistle: .


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

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

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

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
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 :whistle: .

Your example is incorrect. Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
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 :whistle: .

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

Share this post


Link to post
Share on other sites

have you tried running that as a different user?

Share this post


Link to post
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 this post


Link to post
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

:whistle:


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 this post


Link to post
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

:whistle:

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

Share this post


Link to post
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 this post


Link to post
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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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