Hi, everybody
I' m looking over the net for the answer to this question with no luck.
When running thefunction, it returns a single dot "." what does this mean?Code:Dir ("somePath" & "\" ,vbdirectory)
Hi, everybody
I' m looking over the net for the answer to this question with no luck.
When running thefunction, it returns a single dot "." what does this mean?Code:Dir ("somePath" & "\" ,vbdirectory)
From my old DOS days, a single dot refers to the current directory you are in when the command is issued.
Anybody there ?
Ok thanks for the answer but i' m getting confused, it supposed to return the existing folder name, otherwise null string. Is this a bug or something?
try moving the close parens
Dir ("somepath" & "/"), vbdirectory
it's not working, it gives me " 16" (spaces and 16 for the vbdirectory)
I run this sub and indeed it returns among the other files and folders "." and ".." in my directory. I' ve also mooved my files to a new directory but still the dots are there, ihave no clue whats going on
Code:Sub smthing() Dim fList As String Dim fName As String fName = Dir(currentproject.path & "\Data\Backup\", vbDirectory) Do While fName <> "" ' Store the current file or directory name in the string fList. fList = fList & vbNewLine & fName ' Get the next file or directory within "C:\". fName = Dir() ' The variable fName now contains the name of the next file or directory within "C:\". Loop ' Display the list of directories in a message box. MsgBox ("File List:" & fList) End Sub
the only workaround i can think is to put something like that:
...but it is still mystery to meCode:Do while Left(str,1) = "." str = Dir() Loop
The single dot (.) is the current directory.
The double dot (..) is the parent directory.
To test this......
1) Open a command window.
2) Enter the command DIR (you should see a list)
3) Then type CD, a space, then a directory name from the DIR list. (This will change to the sub directory.)
4) Now type DIR ..
You will see the same listing as the first DIR command - the parent directory listing.
5) Type "CD .." (no quotes) This will move you up one level (to the parent directory) in the directory structure.
...mmm interesting, but what is the purpose of getting involved with dir function at vba? this can make you crazy as it can ruin your logic of code, am i missing smthing?
Well.....
Lets say you have a folder with 30 CSV files and 35 Excel files and you need to import only the CSV files.
You might use code similar to the following AIR code:
Beats having to pick them one at a time......Code:tmpFile = Dir("C:\AcctFiles\2016\*.csv") Do While tmpFile <> "" Import code for the csv file goes here ' get next csv file name tmpFile = Dir Loop
I don't get it, I would use the code that you mentioned, for same purpose, but what this has to do with the "." and ".." . Why the above code wouldn't work having in mind that theese two "files" do not exist?
Anyway, it is very strange that is not mentioned anywhere in the explanation of dir func. Thanks for your help.
Last thoughts, I promise
"." and ".." are not files any more that the "Start Up" folder (aka directory) is a file.Why the above code wouldn't work having in mind that these two "files" do not exist?
Think of them as pointers - they are how DOS keeps track of a directory in the linked list of directories.
Check the examples below to get some idea about Dir() command in VBA. These commands can be directly executed on the Debug Window.
Sample folder structure and files:
Sample Commands and their result:Code:C:\FirstFolder\InnerFolder\201605.pdf \201607.pdf \ACCESS2.txt C:\FirstFolder\Survey.pdf
Code:? dir("c:\FirstFolder\InnerFolder",vbNormal) - Returns Empty String Above command checks whether InnerFolder is a file inside the path c:\FirstFolder\ or not. InnerFolder is not a file, hence returns an empty string. ? dir("c:\FirstFolder\InnerFolder",vbDirectory) - Returns InnerFolder InnerFolder inside the path c:\FirstFolder\ is a Folder and returns it's Name. ? dir("c:\FirstFolder\InnerFolder\",vbNormal) - Returns 201605.pdf Looks for Files inside specified path and returns the first file name. Note: After executing the above command, run ? Dir() command without parameters to return the next file name. This can be repeated until all files are accessed this way. After that it will run into Error, if executed without necessary parameters. ? dir("c:\FirstFolder\InnerFolder\",vbDirectory) - Returns . (dot) stating that ..\InnerFolder\ is a Folder. Unlike the previous command it will not return the first file name. ? dir("c:\FirstFolder\InnerFolder\..",vbNormal) - Returns the file name: Survey.pdf usage of \.. gets the first filename (if files exists), one level up from \InnerFolder folder. ? dir("c:\FirstFolder\InnerFolder\..",vbDirectory) - Returns . (dot) One level up is a folder above the folder \InnerFolder. ? dir("c:\FirstFolder\InnerFolder\..\..",vbNormal) - Returns an empty string, if no files in c:\ (Root Directory) otherwise it returns the first file name. ? dir("c:\FirstFolder\InnerFolder\..\..",vbDirectory) - Returns the first folder name from C:\ directory. Subsequent dir() command without any parameters will get the next folder name from C:\.
Last edited by apr pillai; 07-27-2016 at 04:42 AM. Reason: highlighting the command parameter