Count all files in folder and subfolder with VB6 - count

In VB6, I am using a Webbrowser form to display all files and subfolders as icons in the style of Windows Explorer. On the form, I display a count of all files in the main folder and all subfolders. This is my code so far, though haven't been able to find any information about counting subfolders.
Private Sub Load_Form()
Dim myPathName as String
Dim iTotalCount as Long
some stuff here ....
WebBrowser1.Navigate myPathName
WebBrowser1.Document.CurrentViewMode = 5 'medium icons
iTotalCount = FileCount(myPathName)
lblLabel.Caption = "Total Files = " & iTotalCount
more stuff here ....
End Sub
Public Function FileCount(myPathName as String) as Long
Dim FSO as New FileSystemObject
Dim fld as Folder
If FSO.FolderExists(myPathName) Then
Set fld = FSO.GetFolder(myPathName)
FileCount = fld.Files.Count
End If
End Function
This StackOverFlow question is similar, though for vb.net (which I don't know). I'd appreciate someone pointing me in the right direction. Many thanks.

Related

Search files based on the date modified entered by user in console application VB.NET

i been given a assignment to create one console application using VB.NET. The assignment will able to open the file that based on the date modified entered by user. can anyone help me to solve my problem. I'm new in vb.net and most of the tutorial are using C# . together i put the latest code i has already done but still if i put the date modified the error file will display .
Thank you in advance
Imports System.IO
Module Module3
Sub Main()
While True
' Read value.
Dim s As DateTime = Console.ReadLine()
' Test the value.
If s = File.GetLastWriteTime("path") Then
Dim p As New Process()
p.StartInfo = New ProcessStartInfo("notepad.exe", "path")
p.Start()
Else
Dim p As New Process()
p.StartInfo = New ProcessStartInfo("notepad.exe", "path2")
p.Start()
End If
' Write the value.
Console.WriteLine("You typed " + s)
End While
End Sub
End Module
in the code snippet you are not searching for a file you are setting the time last modified which isn't what you want to do.
you will need to search through each file until you find the date modified information which is inputted by the user:-
dim s as datetime = console.readline()
'Target Directory
Dim directory = "C:\Users\Peter\Desktop\TestFolder\"
For Each filename As String In IO.Directory.GetFiles(directory, "*", IO.SearchOption.AllDirectories)
Dim fName As String = IO.Path.GetExtension(filename)
dim datemod as string = File.GetLastWriteTime(directory & fname)
If s = datemod Then
Dim p As New Process()
p.StartInfo = New ProcessStartInfo("notepad.exe", directory & fname)
p.Start()
Else
'do something else
endif
next
things that you will need to add are, what to do when it doesn't find a file with that variable.
hope this gets you a little further.

Rename Sharepoint folder using ClientContext

been stuck for this for a while. I need to rename a SharePoint folder using ClientContext. I created a function like so:
Public Function renameFolder(_folders As ListItemCollection, _newFolderName As String) As Boolean
Try
Using _clientContext As New ClientContext(vSharepointSite)
AddHandler _clientContext.ExecutingWebRequest, AddressOf vClaimsHelper.clientContext_ExecutingWebRequest
Dim _folder = _folders(0)
_folder.Item("Title") = _newFolderName
_folder.Item("FileLeafRef") = _newFolderName
_folder.Item("DisplayName") = _newFolderName
_folder.Update()
_clientContext.ExecuteQuery()
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
This function takes a folder collection (actually I pass a collection of only 1 folder) and the new folder name. The function executes well. Inspecting the _folder after the ExecuteQuery, everything looks as expected. However nothing happens in SharePoint, meaning that the folder name remains the original name.
Any suggestions?
Best regards and....HAPPY NEW YEAR!!!!
Ariel
Make sure List Item ( _folder variable in your example) is associated with Folder object.
How to determine whether List Item is associated with a Folder object
Using ctx As New ClientContext(webUrl)
Dim list = ctx.Web.Lists.GetByTitle(listTitle)
Dim item = list.GetItemById(itemId)
ctx.Load(item.Folder)
ctx.ExecuteQuery()
Dim isFolderItem = Not item.Folder.ServerObjectIsNull.Value
End Using
How to rename Folder using SharePoint CSOM
The following example demonstrates how to rename a Folder:
Public Sub RenameFolder(folder As Folder, folderName As String)
Dim ctx = folder.Context
Dim folderItem = folder.ListItemAllFields
folderItem("FileLeafRef") = folderName
folderItem("Title") = folderName
folderItem.Update()
ctx.ExecuteQuery()
End Sub
Usage
Using ctx As New ClientContext(webUrl)
Dim folder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl)
RenameFolder(folder, "Orders")
End Using
Use the BaseName field to rename the folder.
_folder.Item("BaseName") = _newFolderName

Read text from a .doc or .docx file in asp.net using vb.net

I can upload a text file and read its text in a textbox.
Now I want to do the same for .doc or .docx files.
When I tried it in the similar way I read the text files I got some text which is in encrypted format in the whole document. The code for reading from a .txt file is as follows :
txtReadFiles.Text = My.Computer.FileSystem.ReadAllText(Path)
can anyone suggest me some idea?
What you want is an ifilter for .doc(x) files. Ifilters were designed to be used by Windows for its indexing service, but they are frequently pressed into use also for other applications to read text from binary files that contain text. IFilters are frequently released for free - I believe this contains the correct ifilters for doc/docx files (and other Office files).
That said, I've never used the ifilter interface in .net, only in unmanaged c++, but it should be possible. A quick googling turned up this as a likely place to start (it has some recommendations of things to avoid, and some code. I make no guarantee that the code works, you might have to find something else. But the ifilter technology itself does work, I've used it in projects before. Other than the ifilter for pdfs that ships with Reader, which only just "works", barely, last I checked. The Office ifilters work fine, though.)
Imports Microsoft.Office.Interop.Word 'above public class
If OpenFileDialogFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
TBfile.Text = OpenFileDialogFile.FileName 'alamat n nama file asli
'-----------
Dim ext As String
ext = Path.GetExtension(OpenFileDialogFile.FileName)
If ext = ".txt" Then
'tampilkan isi file
TB1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialogFile.FileName)
ElseIf ext = ".doc" Then
Dim App As Application = New Application
Dim doc As Document
Try
doc = App.Documents.Open(OpenFileDialogFile.FileName)
Dim co As Integer = doc.Words.Count
For i As Integer = 1 To co
Dim tex As String = doc.Words(i).Text
'tampilkan isi file
TB1.Text += tex
Next
doc.Close()
Catch ex As Exception
End Try
ElseIf ext = ".docx" Then
Dim App As Application = New Application
Dim doc As Document
Try
doc = App.Documents.Open(OpenFileDialogFile.FileName)
Dim co As Integer = doc.Words.Count
For i As Integer = 1 To co
Dim tex As String = doc.Words(i).Text
'tampilkan isi file
TB1.Text += tex
Next
doc.Close()
Catch ex As Exception
End Try
End If
'----------
Else
Call kosongkan()
CBkunci1.Focus()
End If

VBS: FileSystemObject use in Recursion

Currently I'm working on a script that will go through a given folder and search for all files with a specific extension. It will then print out the name and sum the file size. I believe i have most of the issues sorted out, so this question isn't about how to do this.
Instead, I want to know what would be the best practice for using FileSystemObject streams in a recursive function. Should I be using a single stream for all calls (either global, or passed), or should I be creating a new stream for each recursive step?
For extra fun, I'm planning on having this access multiple PCs, and over UNC path. And yes, I expect there's a better way of doing this, but I'm relatively new with VBS.
Current code:
'Recursive Function handles search of files in folder and subfolders.
Function UNCSearchFolder(strUNCRootPath, strUNCNextFolder)
Dim objFSOUNCSearchFolder, objFSOUNCSearchFolder2, colFolderList, objFolder, strCurrentFolder, strSubFolder
'Get list of Subfolders in folder: <Rootpath>\<Nextfolder>
strCurrentFolder = strUNCRootPath & "\" & strUNCNextFolder & "\"
Set objFSOUNCSearchFolder = CreateObject("Scripting.FileSystemObject")
Set objFSOUNCSearchFolder2 = objFSOUNCSearchFolder.GetFolder(strCurrentFolder)
Set colFolderList = objFSOUNCSearchFolder2.SubFolders
'Subfolder dive
For Each objFolder in colFolderList
strSubFolder = objFolder.name
'REMOVE THIS ECHO LATER
wscript.echo strSubFolder
UNCSearchFolder(strCurrentFolder, strSubFolder)
Next
'Search for files here
'GC on File Streams
Set objFSOUNCSearchFolder2 = Nothing
Set objFSOUNCSearchFolder = Nothing
End Function
So, should one filestream be used for all accesses or should each step use one separately? Is it a moot point? Will this cause multiple connections to each system or should it only use one? Basically I want the script to work without disrupting users, or causing weird responses (ie, running out of active connections). The script will only be used a couple times for an audit we're doing, but may eventually be repurposed for future audits.
Let me know what you think. Thanks for any help,
If you choose to set a reference to FSO inside your function,
then in each recursion will be used new FSO object.
Using single FSO object (either global, or passed) is quite enough.
At least I don't know any benefit of using multiple FSO instances.
[EDIT] I appreciate #AnsgarWiechers comment, and to make the code ready for re-using, while kept the FSO out of the function, we can wrap our function in a class.
With New FileInfo
WScript.Echo .FileSize("C:\temp", "txt", True)
End With
Class FileInfo
Private m_oFSO
Public Function FileSize(sRootDir, sExtension, bRecursive)
Dim oFolder, oFile, sFExt
sFExt = LCase(sExtension)
Set oFolder = m_oFSO.GetFolder(sRootDir)
For Each oFile In oFolder.Files
If LCase(m_oFSO.GetExtensionName(oFile.Name)) = sFExt Then
FileSize = FileSize + oFile.Size
End If
Next
If bRecursive Then
Dim oSubFolder
For Each oSubFolder In oFolder.SubFolders
FileSize = FileSize + FileSize(oSubFolder, sExtension, True)
Next
End If
End Function
Private Sub Class_Initialize
Set m_oFSO = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate
Set m_oFSO = Nothing
End Sub
End Class

Is there a macro/extension to set all web projects in Visual Studio to "Don't Open a Page"?

Occasionally, I'm debugging a problem that occurs across several of our back-end web services, and I have them all setup as startup projects; when I hit F5, they all launch new tabs in whatever browser I have set to default at the time -- it would be useful to me to be able to click a button, and have all of the Project > Properties > Web > Start Action settings set to "don't open a page." Anyone know of a macro or extension that might be able to help me out?
I wrote most of the solution for you, except I wasn't sure how to check if a Project is an ASP.NET project or not. One way to do it could be to check whether Project.ExtenderNames contains the string "WebApplication", but I'm not sure that's the best solution.
Sub ChangeAllWebProjectsStartActionToDontStartWebPage()
Dim project As EnvDTE.Project
If DTE.Solution.IsOpen Then
For Each project In DTE.Solution.Projects
NavProj(project)
Next
End If
End Sub
Sub NavProj(ByVal project As Project)
Dim outputPathProperty As EnvDTE.Property
Dim outputPath As String
If Not (project.ConfigurationManager Is Nothing) Then
' It's a project!
Dim extenderNames As String()
extenderNames = project.ExtenderNames
If (Array.IndexOf(extenderNames, "WebApplication") >= 0) Then
project.Properties.Item("WebApplication.DebugStartAction").Value = 4 ' WebStartAction.NoStartPage
End If
Else
NavProjItems(project.ProjectItems)
End If
End Sub
Sub NavProjItems(ByVal projItems As ProjectItems)
Dim projectItem As EnvDTE.ProjectItem
If Not (projItems Is Nothing) Then
For Each projectItem In projItems
If Not (projectItem.SubProject Is Nothing) Then
' Recurse, can be an Enterprise project in
' Visual Studio .NET 2002/2003 or a solution folder in VS 2005+
NavProj(projectItem.SubProject)
End If
Next
End If
End Sub
This is based on the WebStartAction which is defined as:
enum WebStartAction
{
CurrentPage = 0,
SpecificPage = 1,
Program = 2,
URL = 3,
NoStartPage = 4
}

Resources