Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




Get Only The Directories Or Files Using The FindFirstFile API


How to make the FirstFirstFile API to return only the directories or files not both the directories and files ???




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How Do I Use The FindFirstFile/FindNextFile To Get Names Of The Files In The C:?
How do i use the FindFirstFile/FindNextFile to get names of the files in the c:?

FindFirstFile And Folders,files With (mnemonic) &
If I have a file or folder such as "Food & Drinks", FindFirstFile is causing the filename to become "Food Drinks"

How do I prevent this mnemonic interpretation?



Code:
Dim rec As WIN32_FIND_DATA
Dim strFilename As String
Dim lngFindFileRet As Long

lngFindFileRet = FindFirstFile(strFolder & "*.*", rec)

If lngFindFileRet <= 0 Then
Exit Function
Else
Do
' Read name
strFilename = Left$(rec.cFileName, InStr(rec.cFileName, Chr$(0)) - 1)

rec.cFileName is where it is "Food Drinks" rather than "Food & Drinks"

Find Files In Directories And Sub-directories
How find all mp3 files in all directories and sub-directories

Copying All Files And Nested Directories (and Files In Those Dirs)?
Help :s

I need an easy function where I supply an old path and a new path and it will copy everything from the old path and all sub directories contained within and files to the new one.

Anyone know how to do this?

Files And Their Directories
Hello, I'm new to this forum, and in addition new to using Visual Basic. I've made good use of both google (which brought me to this forum), and then this forums search topic. However, I've already spent a bit of time on google as it is, I've checked and browsed through a slew of topics, in addition to a few tutorials posted here. I hope this topic has not already been posted, and I ask you pardon me if it is.

I would like to be able to get a list of files in the current directory (which is the directory that the visual basic file itself is in), assign this list to a variable, perhaps as a string, and then (for now) display it in a picture box. I would like to do this without user input. My code thus far is:


Code:
Private Sub Form_Load()
Dim list As String
list = Dir()
picAnswer.Print list
End Sub
I understand this seems to be a ridiculiously easy question, but I ask you to have patience with me, for I am both new to programming and to Visual Basic (I've only used Python and some light Dark BASIC before.) I was unsure of which forum to post this in, so I hope this is the correct one.

Files/Directories Using Dir
Hey All,

This problem is something rather simple but I got a mental block unfortunatly.

What I'm doing is reading all the files an folders from a directory using the code:-



'Changes Path
sSourceDir = "c:up"

'Starts Directory Read
sNextFile = Dir(sSourceDir, vbDirectory)

'Loops Through Directory
While sNextFile <> ""

'Gets Next Detail
sNextFile = Dir

'Just To Test
msgbox(sNextFile)

<< Code Here Of No Concern>>

Wend



My problem is for the life of me I can't remember how to tell the difference between a file without an extension and a file without an extension and a directory. I know I done it once before and it wasn't checking to see if a directory exisit or not (i.e anything without an extension check to see if a directory is there, if not it is a file.

I know it wasn't that hard but I got the biggest mental blank at the moment any help anyone can offer would be greatly appreicated.

Stevil

Directories And Files...
Does anyone know how I can block an file or directory from a user and when he/she wants to open the directory that i can show a form that askes for a password....??? Maybe with a API call...??? And if it works ofcourse I would like to know how to unblock...

Gr. Bob

Files, Directories, Etc.
I'm trying to get the next file in a directory given a current file name. If there is no next file, I want it to return the first file in the directory.

This what I tried:


VB Code:
Option Explicit Dim strFolder As StringDim strLastFile As String Private Sub Form_Load()Dim strF As String   strFolder = "D:FolderFolder"  strLastFile = "D:FolderFolderfile.ext"        MsgBox GetNextFile(strFolder, strLastFile)End Sub Private Function GetNextFile(strPath As String, strFile As String) As StringDim i As Integer, s As String, strFirst As String, bAt As Boolean   bAt = False   s = Dir(strPath, vbDirectory)  While Len(s)    i = i + 1    s = Dir        If bAt = True Then      GetNextFile = s            Exit Function    ElseIf i = 1 Then      strFirst = s    ElseIf s = strFile Then      bAt = True    ElseIf s = "" Then      GetNextFile = strFirst            Exit Function    End If      Wend    GetNextFile = ""End Function


But it doesn't work. How can I accomplish this?

Help With Files And Directories
Help, I’m trying to open and edit all files in a directory that match a file extension I want. The problem is I don’t know how to loop through all the file names. My program works to open edit and close it, I just can’t cycle through the files.

Any help will be greatly appreciated.

Thanks Greg…

Files And Directories
hi,
how to copy files from one folder/directory to another folder/directory.

Ex:
because i am trying to import .xls files into sqlserver database. so for this i developed one package for importing files in a Batch. i am submitting TWO directory to DTS package.

i am created three folders/directories under
C: ONE TWO THREE
at runtime when i click the ONE folder i want to add all files to list Box(with check box facility).
when i check / selected files from list box i want to copy those files into TWO folder ,
after submitting the TWO folder to DTS package.
i want to delete .xls files from ONE and TWO before deleting i want to add all .xls files from ONE folder to Three foldre/directory.

bye.

DLL To ZIP Files And Directories?
Hello

I need to find a DLL (either COM or non-COM, but I prefer the latter) for a VB5 application that can not only zip files, but also directories recursively.

Using VbAccelerator's vbzip11.dll, I can zip files, but this code doesn't work to zip a directory: Instead of zipping everything that lives in the path I give, it zips files that live in App.Path

Does someone know either why this code doesn't work as intended, or another DLL that works?

Thank you.

DLL To ZIP Files And Directories?
Hello

I need to find a DLL (either COM or non-COM, but I prefer the latter) for a VB5 application that can not only zip files, but also directories recursively.

Using VbAccelerator's vbzip11.dll, I can zip files, but this code doesn't work to zip a directory: Instead of zipping everything that lives in the path I give, it zips files that live in App.Path

Does someone know either why this code doesn't work as intended, or another DLL that works?

Thank you.

Directories And Files
I would like to know if anybody outhere has code that will compare 2 directores and their subdirectories and return back the files that are in one
and not in the other.

Directories & Files
Hi,

How does one determine the number of files in a directory and the size (MB) of that directory?

Thanks,

Rob

Files In Directories
How would I go about storing all of the *.mp3 filenames in an array from a certain directory? Also, would I be able to recurse the subdirectories? Thanks a lot everyone, your the best!

Jason

Copying All Files And Sub Directories
Im having trouble understanding shfileop. how do i use it to copy a folder and all contents?

Copy Files And Directories
Hi, all you people!

First of all I gotta say I'm spanish, so forgive my english.


I'm programming with VB6 since a short time, and I don't know how to copy a full directory (with sub-directories), for example, "My Documents" folder.

I've heard of an instruction, FileCopy, but this instruction only copies files, and I want to copy files and directories.


PLEASE, SOMEONE HELP ME!!!!!!
Thanx.

Just post answers or email me at guitarman@terra.es

Delete Files And Directories
h I know how to delete files with kill, but how do you delete directories, or can you just delete entire directories with vb?

Deleteing Files And Directories
Hello.
I know that I can delete a file with Kill and I know I can delete a empty(!) directory with RmDir.
But what if I need to delete directory full with other files I don't know about.
How can I get information about the files inside a diretory?
A little example would be cool.
Thanks!

Dealing With Files And Directories
Does anyone know the following functions ( if such functions exists in VB ) and how they are used syntax-wise:

- To delete a file.
- To rename a file.
- To delete a directory.
- to rename a directory.
- to clear the contents of a file without deleting it (making it zero byte in content length).
- List the files in a certain directory.
- Knowing the file size in bytes

Thank you

VB Code To Map Directories And Files??
Hi, im hoping someone can assist with this (not sure if right forum section).

Im wanting some VB code that will work through a given direcoty (e.g C:Windows) and make a note of each file/folder found in that directory and print it into a txt document.

All help greatly appreciated

Manipulating Directories &amp; Files
Can any one help me and provide the list of commands or ways to manipulate (create/delete/move/copy/rename) directories and files.

Could you also tell me how I can find out "the drives available" and how i can get the list of all the files and folders in a certain "Directory or a Drive"


Thanks a lot

How To Pass All Files And Sub Directories?
Hi guys
I'm trying to use VBAccelerator zip to compress all content of directory and all files inside,

how to use dir command to get all files in child directories?

thanks

Directories And Sub Dir Respective Files
"C:Directorysubdir1file1.txt"
"C:Directorysubdir1file2.txt"
"C:Directorysubdir1file3.txt"
"C:Directorysubdir1abc.txt"
"C:Directorysubdir2123.txt"
"C:Directorysubdir2xyz.txt"
"C:Directoryfolderfil1.txt"
"C:Directoryvbfiles1.txt"
"C:Directoryvbfiles2.txt"
"C:Directoryvbfiles3.txt"
"C:Directorysubdir32.txt"


HOW TO GET THE ABOVE RESULT USING FOR AND NEXT LOOP & PRINT #1

REGDS

SAMF

Accessing Directories And Files
Hi,
I am developing an application where I have to go through the structure of a directory and the files inside its subdirectories to read data in the files. My directory has a variable number of subdirectories and a variable number of files inside every subdirectory. I already have my function that will read the data from the files set up; but I do not know how to get to them dynamically. Any suggestions, code, or ideas are welcome.
Thanks in advance!

Managing Files And Directories
I'm still very new to VB6.

My question:
When u create a file with VB, but now want to delete that file, how do u do that. U cant use the OPEN statement as the file will be open.

Any help welcome.

acidburn

Comparing Files In Different Directories
Hi,
I am new to the board and also new to VB programming. So let me start by apologising if the question I am about to ask is old hat to you all.

I need to write a program that compares two sets of directories for files that may be out of synch. If I find a file that is out of synch I need to update both directories with the latest version.

The trouble I have is that I don't know how to read a set of directories recursively and store the contents in accessible arrays so that I can do my comparison.

I thought that the approach I needed to adopt was as follows:

Read both sets of directories into a storage area

Start with a directory set and look for files. If a file is found in the directory then look for the same filename in the second directory structure and compare the file modified dates. Depending upon each of these then copy one way or the other.

Is this sane?

Any help with code samples on the reading and storing of the directory structure would be invaluable to me. I am a novice in all this and I am finding some of the curve a little steep,

Kind Regards,
Sam.

Working With Files And Directories
Where can i find information about copying files, making directories, looking if a file exists?

Directories / Files Commands
Hi,

I am quite new in VB and hopefully someone can give me some help.

Have a number of textual files with a certain extention (.rsd). The filenames itself and the content are unique and have to be analysed, processed and the results written to another file.

Questions:
- How can I read the complete directory (*.rsd) (in one go - without user interaction) - whereby all the filenames from this directory are written into an array
- This array I want to use to analyse all the filenames and
- analyse the content of this file (how to read the lines in each file 1-by-1?)

Thanks,
Jamie

PS: is there somewhere a complete overview of all the commands in VB?

Sharing Of Files And Directories..
hi all...

How do i access the sharing property of filesand directories.I create some files and directories on a clientmachine,i'm suppossed to set the sharing property of the files anddirectories to full access.how do i access these...

please help me in this,itz very urgent.....

thank you..

prathap...

Delete Certain Files In Sub Directories
In vb 6, I am trying to write a simple app that will run and delete
files that are older than 10 days in a specific directory.

No problem so far.

Now, it has to go through every subdirectory below my specified
directory. Thankfully, they only go one layer deep.

Any ideas would be greatly appreciated, samples would be even better.

Creating Directories And Files...
Hi guys I am trying to write a simple app that will try and write a file to a directory, if the directory is non-existent i want the prog to create the directory then write to it. I also want this to loop every 1/5 of a second. The psuedo code would go something like this:

while running

    write to "pathfile"
    sleep 200ms

I cant seem to get eVB to even write a file at the moment, can anyone help me? Is it going to be easier to do this in eVC++ ?

any help would be much appreciated...

Getting A List Of Files/directories
Hi,
I currently have a number of clients who each have a folder on my machine. I want to lod the files from each folder and sub-folder into my database.

In each client folder, is a number of sub-folder, i.e. client1 has the sub-folders, Faxes, Invoices and general documents.


i can select client1, now i need to write VB code to select each sub-folder, then then each document in the subfolder, and then i can put it into my database. the description for each document is the subfolder name. So the output i want would be

client1, faxes, fax1.doc
client1, faxes, fax2.doc
client1, invoices, invoice1.doc

etc,
is this possible with VB?

Please help if you can.....

many thanks

Is There An API To Search Through Files/directories?
Hi,

I have tried a couple of methods (nothing spectacular) to search through files / directories (recursive searches). All work - but...

If I compare the speeds of these searches to that of "Start > Search > For Files And Folders", my searches are woefully slow.

So, is there an API that I can use in VB/VBA that is as fast as the "Start > Search > For Files And Folders" provided in Windows?

Thanks.

Down Lading Files To Directories
Hi All!
I need some help regarding a particular routine. I have 4 combo boxes, one has State names, another has city names ,next one has directory structure and the last one has files.
when user selects a state, all its cities will be displayed, when user selects city all the directory structure under that will be displayed in the combo box.
What I need to do is I need to create jsu like windows explorer on the hard drive where I can have a structure like this State-&gt;City-&gt;Directory-&gt;Files

Can some one help me please
Thanks

Installing Files In Specific Directories
Hi

Can anyone help me on the installtion problem I have. I want a certain file to be copied to a certain directory i.e. c:mydirectory. How do I specify this in the deployment wizard?

Thanks

Help Getting List Of Remote Directories And Files
im just trying to figure out a way to retrieve the list of drives, directories, and files from a remote computer. i have already tried, but the part i get stuck at is the file part from trying to retrieve all the files total from the HD. i would wonder if anyone could help me by letting me know the correct way to retrieve all the drives directories and files, and for an added bonus, tell me how to retrieve them remotely. thanx, anyone's help is greatly appreciated!!!

How To Sort The Treeview Directories First And Then Files?
Hello
im building treeview dirs/files structure but when i add beneath curtain node
its child dirs and files the nodes that represents the dirs and files are by alphabetic order without any
indication of there type, what i mean is how can i order first the dirs and then the files
somethink like that:
-------------------------------------
rootDir
--dir1
--dir2
--dir3
--file.exe
--file.bat
--file.bmp
.....
....
--------------------------------------
thanks

Accessing Files Nested In Sub, Sub Directories
Hello,

I need to go to a directory, and if there are subfolders contained in those subfolders, I need to get/list all *.doc or *.xls files located in these sub, sub folders into a ListBox control. This code was suggested to me:


Code:
Dim colFolders As Collection
Set colFolders = New Collection
Dim s As String

colFolders.Add "C:My Documents" 'Must end with a slash!

'Loop through all the folders in the collection
Do While colFolders.Count > 0

'Loop through all sub folders of this folder
s = Dir(colFolders.Item(1), vbDirectory)
Do While Len(s)

'Is this a file or a folder?
If (GetAttr(colFolders.Item(1) & s) And vbDirectory) = vbDirectory Then

'Its a folder so add it to the collection to be scanned
colFolders.Add colFolders.Item(1) & s & "/" 'It also adds the slash
Else

'Its a file so add it to your list
ListBox1.AddItem colFolders.Item(1) & s
End If

'Move to the next file/folder
s = Dir()
Loop

'Remove this folder from the collection
colFolders.Remove 1

Loop


The following line of code is creating an infinite loop:

Code:
'Loop through all the folders in the collection
Do While colFolders.Count > 0

So, there are 2 things wrong with the code, #1 the infinite loop, #2 I need the subdirectories and the files to write to the ListBox.
I'm unsure how to fix this. The Loop is always >0.

Thanks for any help,
CyberJar

Search A Websites Files And Directories
hey..is it possible to make a prog to Search a websites Files and Directories?? like find a particular file on the website?

Making Directories And Then Copying Files.
OK, after quite a lot of thought, i have decided to just make the directories,

and then Put the files into the directories i make.

So :


VB Code:
Private Sub Form_Load()On Error Resume Next ' This should stop it crashing when i try to make an exsisting Dir Correct?Me.Icon = frmSettings.IconMkDir frmInstall.Dir1.Path & "httpdocs"FileCopy App.Path & "index.html", frmInstall.Dir1.Path & "httpdocsindex.html"End Sub


Now no matter if the directory exsists, or not, this shouldnt cause problems?

Or will it?

I know there is like 99% chance that for most of the directories im going to make exsisting, but just to be sure they are there, im gonna try and make them,,

So this should work right?

Deleting Files And Directories Error
VB Code:
Sub deldir(path As String)    Dim tempvar As String, pos As Integer    pos = 0    tempvar = Dir(path + "*.*", 22)    Do While tempvar <> ""        If tempvar <> "." And tempvar <> ".." Then        If (GetAttr(path + "" + tempvar) And 16) = 0 Then            SetAttr path + "" + tempvar, 0            Kill path + "" + tempvar        Else            deldir (path + "" + tempvar)            tempvar = Dir(path + "*.*", 22)        End If        End If        tempvar = Dir    Loop    RmDir pathEnd Sub


I have a directory C:Dir1 that contains 2 directories (Dir2 and Dir3)
Dir2 and Dir3 each contain files

Can anyone tell me why the above code always gives me a Path/File access error on the last statement (Rmdir path). I want to delete the Root directory (C:Dir1) and all under it. Debug tells me that the path is C:Dir1 at the "RmDir path" statement

I cant see anything in the routine that locks the directory Dir1
I have no idea whats doing it. Any help would be great thankx.

Finding Files - Transcending Directories
I would like to be able to search an entire drive for a particular type of file. I know I can use the DIR function to search within a specific directory but is there a way to start at the root and search all the directories?

Accessing Files And Directories From A Module...
I know this is probably easy, but don't know quite how to do it. Previously every time that I needed to access files, I would put a directory and file control on a form and make it invisible so the user does not see it. Now I am trying to access files from a module and don't quite know the proper way to do it. I tried to declare a DirListBox in my code, but it didn't seem to like that. Could someone help me out?

Thanks,

Shawn

VB5, WinNT

Browsing Files/directories Tool
I am trying to make a browsing files/directories tool with ASP, and I have found some code from Microsoft Support
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q224364



Code:

<%@LANGUAGE="VBSCRIPT"%>
<%
Option Explicit
On Error Resume Next

' this section is optional - it just denies anonymous access
If Request.ServerVariables("LOGON_USER")="" Then
Response.Status = "401 Access Denied"
End If

' declare variables
Dim objFSO, objFolder
Dim objCollection, objItem

Dim strPhysicalPath, strTitle, strServerName
Dim strPath, strTemp
Dim strName, strFile, strExt, strAttr
Dim intSizeB, intSizeK, intAttr, dtmDate

' declare constants
Const vbReadOnly = 1
Const vbHidden = 2
Const vbSystem = 4
Const vbVolume = 8
Const vbDirectory = 16
Const vbArchive = 32
Const vbAlias = 64
Const vbCompressed = 128

' don't cache the page
Response.AddHeader "Pragma", "No-Cache"
Response.CacheControl = "Private"

' get the current folder URL path
strTemp = Mid(Request.ServerVariables("URL"),2)
strPath = ""

Do While Instr(strTemp,"/")
strPath = strPath & Left(strTemp,Instr(strTemp,"/"))
strTemp = Mid(strTemp,Instr(strTemp,"/")+1)
Loop

strPath = "/" & strPath

' build the page title
strServerName = UCase(Request.ServerVariables("SERVER_NAME"))
strTitle = "Contents of the " & strPath & " folder"

' create the file system objects
strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPhysicalPath)
%>
<html>
<head>
<title><%=strServerName%> - <%=strTitle%></title>
<meta name="GENERATOR" content="The Mighty Hand of Bob">
<style>
BODY { BACKGROUND: #cccccc; COLOR: #000000;
FONT-FAMILY: Arial; FONT-SIZE: 10pt; }
TABLE { BACKGROUND: #000000; COLOR: #ffffff; }
TH { BACKGROUND: #0000ff; COLOR: #ffffff; }
TD { BACKGROUND: #ffffff; COLOR: #000000; }
TT { FONT-FAMILY: Courier; FONT-SIZE: 11pt; }
</style>
</head>
<body>

<h1 align="center"><%=strServerName%><br><%=strTitle%></h1>
<h4 align="center">Please choose a file/folder to view.</h4>

<div align="center"><center>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th align="left">Name</th>
<th align="left">Bytes</th>
<th align="left">KB</th>
<th align="left">Attributes</th>
<th align="left">Ext</th>
<th align="left">Type</th>
<th align="left">Date</th>
<th align="left">Time</th>
</tr>

<%
''''''''''''''''''''''''''''''''''''''''
' output the folder list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.SubFolders

For Each objItem in objCollection
strName = objItem.Name
strAttr = MakeAttr(objItem.Attributes)
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align="left"><b><a href="<%=strName%>"><%=strName%></a></b></td>
<td align="right">N/A</td>
<td align="right">N/A</td>
<td align="left"><tt><%=strAttr%></tt></td>
<td align="left"><b><DIR></b></td>
<td align="left"><b>Directory</b></td>
<td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

<%
''''''''''''''''''''''''''''''''''''''''
' output the file list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.Files

For Each objItem in objCollection
strName = objItem.Name
strFile = Server.HTMLEncode(Lcase(strName))

intSizeB = objItem.Size
intSizeK = Int((intSizeB/1024) + .5)
If intSizeK = 0 Then intSizeK = 1

strAttr = MakeAttr(objItem.Attributes)
strName = Ucase(objItem.ShortName)
If Instr(strName,".") Then strExt = Right(strName,Len(strName)-Instr(strName,".")) Else strExt = ""
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align="left"><a href="<%=strFile%>"><%=strFile%></a></td>
<td align="right"><%=FormatNumber(intSizeB,0)%></td>
<td align="right"><%=intSizeK%>K</td>
<td align="left"><tt><%=strAttr%></tt></td>
<td align="left"><%=strExt%></td>
<td align="left"><%=objItem.Type%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

</table>
</center></div>

</body>
</html>
<%
Set objFSO = Nothing
Set objFolder = Nothing

' this adds the IIf() function to VBScript
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function

' this function creates a string from the file atttributes
Function MakeAttr(intAttr)
MakeAttr = MakeAttr & IIf(intAttr And vbArchive,"A","-")
MakeAttr = MakeAttr & IIf(intAttr And vbSystem,"S","-")
MakeAttr = MakeAttr & IIf(intAttr And vbHidden,"H","-")
MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,"R","-")
End Function
%>




However I would like to use the tool that will be put in a web server to browse files on a file server, therefore I've changed the line


strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("strPhysicalPath")

TO
strPhysicalPath = "\machine_nameFolder_name"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("strPhysicalPath")

to specify the path, but it didn't work. Anyone help would be appreciate, thanks in advance!

Browsing Files/directories Tool
I am trying to make a browsing files/directories tool with ASP, and I have found some code from Microsoft Support
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q224364


Code:

<%@LANGUAGE="VBSCRIPT"%>
<%
Option Explicit
On Error Resume Next

' this section is optional - it just denies anonymous access
If Request.ServerVariables("LOGON_USER")="" Then
Response.Status = "401 Access Denied"
End If

' declare variables
Dim objFSO, objFolder
Dim objCollection, objItem

Dim strPhysicalPath, strTitle, strServerName
Dim strPath, strTemp
Dim strName, strFile, strExt, strAttr
Dim intSizeB, intSizeK, intAttr, dtmDate

' declare constants
Const vbReadOnly = 1
Const vbHidden = 2
Const vbSystem = 4
Const vbVolume = 8
Const vbDirectory = 16
Const vbArchive = 32
Const vbAlias = 64
Const vbCompressed = 128

' don't cache the page
Response.AddHeader "Pragma", "No-Cache"
Response.CacheControl = "Private"

' get the current folder URL path
strTemp = Mid(Request.ServerVariables("URL"),2)
strPath = ""

Do While Instr(strTemp,"/")
strPath = strPath & Left(strTemp,Instr(strTemp,"/"))
strTemp = Mid(strTemp,Instr(strTemp,"/")+1)
Loop

strPath = "/" & strPath

' build the page title
strServerName = UCase(Request.ServerVariables("SERVER_NAME"))
strTitle = "Contents of the " & strPath & " folder"

' create the file system objects
strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPhysicalPath)
%>
<html>
<head>
<title><%=strServerName%> - <%=strTitle%></title>
<meta name="GENERATOR" content="The Mighty Hand of Bob">
<style>
BODY { BACKGROUND: #cccccc; COLOR: #000000;
FONT-FAMILY: Arial; FONT-SIZE: 10pt; }
TABLE { BACKGROUND: #000000; COLOR: #ffffff; }
TH { BACKGROUND: #0000ff; COLOR: #ffffff; }
TD { BACKGROUND: #ffffff; COLOR: #000000; }
TT { FONT-FAMILY: Courier; FONT-SIZE: 11pt; }
</style>
</head>
<body>

<h1 align="center"><%=strServerName%><br><%=strTitle%></h1>
<h4 align="center">Please choose a file/folder to view.</h4>

<div align="center"><center>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th align="left">Name</th>
<th align="left">Bytes</th>
<th align="left">KB</th>
<th align="left">Attributes</th>
<th align="left">Ext</th>
<th align="left">Type</th>
<th align="left">Date</th>
<th align="left">Time</th>
</tr>

<%
''''''''''''''''''''''''''''''''''''''''
' output the folder list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.SubFolders

For Each objItem in objCollection
strName = objItem.Name
strAttr = MakeAttr(objItem.Attributes)
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align="left"><b><a href="<%=strName%>"><%=strName%></a></b></td>
<td align="right">N/A</td>
<td align="right">N/A</td>
<td align="left"><tt><%=strAttr%></tt></td>
<td align="left"><b><DIR></b></td>
<td align="left"><b>Directory</b></td>
<td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

<%
''''''''''''''''''''''''''''''''''''''''
' output the file list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.Files

For Each objItem in objCollection
strName = objItem.Name
strFile = Server.HTMLEncode(Lcase(strName))

intSizeB = objItem.Size
intSizeK = Int((intSizeB/1024) + .5)
If intSizeK = 0 Then intSizeK = 1

strAttr = MakeAttr(objItem.Attributes)
strName = Ucase(objItem.ShortName)
If Instr(strName,".") Then strExt = Right(strName,Len(strName)-Instr(strName,".")) Else strExt = ""
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align="left"><a href="<%=strFile%>"><%=strFile%></a></td>
<td align="right"><%=FormatNumber(intSizeB,0)%></td>
<td align="right"><%=intSizeK%>K</td>
<td align="left"><tt><%=strAttr%></tt></td>
<td align="left"><%=strExt%></td>
<td align="left"><%=objItem.Type%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
<td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

</table>
</center></div>

</body>
</html>
<%
Set objFSO = Nothing
Set objFolder = Nothing

' this adds the IIf() function to VBScript
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function

' this function creates a string from the file atttributes
Function MakeAttr(intAttr)
MakeAttr = MakeAttr & IIf(intAttr And vbArchive,"A","-")
MakeAttr = MakeAttr & IIf(intAttr And vbSystem,"S","-")
MakeAttr = MakeAttr & IIf(intAttr And vbHidden,"H","-")
MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,"R","-")
End Function
%>




However I would like to use the tool that will be put in a web server to browse files on a file server, therefore I've changed the line


strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("strPhysicalPath")

TO
strPhysicalPath = "\machine_nameFolder_name"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("strPhysicalPath")

to specify the path, but it didn't work. Anyone help would be appreciate, thanks in advance!

Showing Directories,subdirectories And Files
I am trying to get all the subdirectories and files inside a folder into a file. I needed the same output as I get while I do in DOS with the command - "dir /s", the same output I needed to get it from VB.

Can anybody help me to get this.

Thanks
Disha

Browsing Files/directories Tool
I am trying to make a browsing files/directories tool with ASP, and I have found some code from Microsoft Support
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q224364

CODE<%@LANGUAGE="VBSCRIPT"%>
<%
   Option Explicit
   On Error Resume Next

   ' this section is optional - it just denies anonymous access
   If Request.ServerVariables("LOGON_USER")="" Then
      Response.Status = "401 Access Denied"
   End If

   ' declare variables
   Dim objFSO, objFolder
   Dim objCollection, objItem

   Dim strPhysicalPath, strTitle, strServerName
   Dim strPath, strTemp
   Dim strName, strFile, strExt, strAttr
   Dim intSizeB, intSizeK, intAttr, dtmDate

   ' declare constants
   Const vbReadOnly = 1
   Const vbHidden = 2
   Const vbSystem = 4
   Const vbVolume = 8
   Const vbDirectory = 16
   Const vbArchive = 32
   Const vbAlias = 64
   Const vbCompressed = 128

   ' don't cache the page
   Response.AddHeader "Pragma", "No-Cache"
   Response.CacheControl = "Private"

   ' get the current folder URL path
   strTemp = Mid(Request.ServerVariables("URL"),2)
   strPath = ""

   Do While Instr(strTemp,"/")
      strPath = strPath & Left(strTemp,Instr(strTemp,"/"))
      strTemp = Mid(strTemp,Instr(strTemp,"/")+1)
   Loop

   strPath = "/" & strPath

   ' build the page title
   strServerName = UCase(Request.ServerVariables("SERVER_NAME"))
   strTitle = "Contents of the " & strPath & " folder"

   ' create the file system objects
   strPhysicalPath = Server.MapPath(strPath)
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
   Set objFolder = objFSO.GetFolder(strPhysicalPath)
%>
<html>
<head>
<title><%=strServerName%> - <%=strTitle%></title>
<meta name="GENERATOR" content="The Mighty Hand of Bob">
<style>
BODY  { BACKGROUND: #cccccc; COLOR: #000000;
        FONT-FAMILY: Arial; FONT-SIZE: 10pt; }
TABLE { BACKGROUND: #000000; COLOR: #ffffff; }
TH    { BACKGROUND: #0000ff; COLOR: #ffffff; }
TD    { BACKGROUND: #ffffff; COLOR: #000000; }
TT    { FONT-FAMILY: Courier; FONT-SIZE: 11pt; }
</style>
</head>
<body>

<h1 align="center"><%=strServerName%><br><%=strTitle%></h1>
<h4 align="center">Please choose a file/folder to view.</h4>

<div align="center"><center>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
   <th align="left">Name</th>
   <th align="left">Bytes</th>
   <th align="left">KB</th>
   <th align="left">Attributes</th>
   <th align="left">Ext</th>
   <th align="left">Type</th>
   <th align="left">Date</th>
   <th align="left">Time</th>
</tr>

<%
   ''''''''''''''''''''''''''''''''''''''''
   ' output the folder list
   ''''''''''''''''''''''''''''''''''''''''

   Set objCollection = objFolder.SubFolders

   For Each objItem in objCollection
      strName = objItem.Name
      strAttr = MakeAttr(objItem.Attributes)
      dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
   <td align="left"><b><a href="<%=strName%>"><%=strName%></a></b></td>
   <td align="right">N/A</td>
   <td align="right">N/A</td>
   <td align="left"><tt><%=strAttr%></tt></td>
   <td align="left"><b><DIR></b></td>
   <td align="left"><b>Directory</b></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

<%
   ''''''''''''''''''''''''''''''''''''''''
   ' output the file list
   ''''''''''''''''''''''''''''''''''''''''

   Set objCollection = objFolder.Files

   For Each objItem in objCollection
      strName = objItem.Name
      strFile = Server.HTMLEncode(Lcase(strName))

      intSizeB = objItem.Size
      intSizeK = Int((intSizeB/1024) + .5)
      If intSizeK = 0 Then intSizeK = 1

      strAttr = MakeAttr(objItem.Attributes)
      strName = Ucase(objItem.ShortName)
      If Instr(strName,".") Then strExt = Right(strName,Len(strName)-Instr(strName,".")) Else strExt = ""
      dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
   <td align="left"><a href="<%=strFile%>"><%=strFile%></a></td>
   <td align="right"><%=FormatNumber(intSizeB,0)%></td>
   <td align="right"><%=intSizeK%>K</td>
   <td align="left"><tt><%=strAttr%></tt></td>
   <td align="left"><%=strExt%></td>
   <td align="left"><%=objItem.Type%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
</tr>
<% Next %>

</table>
</center></div>

</body>
</html>
<%
   Set objFSO = Nothing
   Set objFolder = Nothing

   ' this adds the IIf() function to VBScript
   Function IIf(i,j,k)
      If i Then IIf = j Else IIf = k
   End Function

   ' this function creates a string from the file atttributes
   Function MakeAttr(intAttr)
      MakeAttr = MakeAttr & IIf(intAttr And vbArchive,"A","-")
      MakeAttr = MakeAttr & IIf(intAttr And vbSystem,"S","-")
      MakeAttr = MakeAttr & IIf(intAttr And vbHidden,"H","-")
      MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,"R","-")
   End Function
%>

Error Checking: Files And Directories
A bit of history, I picked up a copy VB 6 (without MSDN) over the weekend; my last coding in basic was with GW Basic, so you *won't* find anyone with less experience than I have. I've actually found answers to a few questions already, but if some kind soul would care to elaborate on a "simple" error checking routine for both creating files/folders and opening existing files, then I'd be grateful.

I was working with the drive1, dir1, and file1 thingies on the form, but discovered the common dialog method, which is what I'd like to use. I've managed to find some snippits here for the common dialog routine, but not really the error checking itself.
 
TIA



Edited by - pseudocoder on 7/15/2004 5:47:19 PM

Copyright © 2005-08 www.BigResource.com, All rights reserved