VBA Folder contents Empty Move to next Folder path - directory

I've written a VBA to copy files from a list of directory over to a specific folder.
The code works fine and what I now need to do is add an IF statement for the VBA to move to the next folder when one folder is empty and copy any files in the next folder.
below is the current vba
Sub ExtractChecklistData()
Dim FSO As Object
Dim SourcePath As String
Dim DestinationPath As String
Dim FileExtn As String
'Starters
SourcePath = Sheet4.Range("B2")
SourcePath2 = Sheet4.Range("B4")
SourcePath3 = Sheet4.Range("B6")
SourcePath4 = Sheet4.Range("B8")
SourcePath5 = Sheet4.Range("B10")
SourcePath6 = Sheet4.Range("B12")
SourcePath7 = Sheet4.Range("B14")
SourcePath8 = Sheet4.Range("B16")
SourcePath9 = Sheet4.Range("B18")
SourcePath10 = Sheet4.Range("B20")
SourcePath11 = Sheet4.Range("B22")
SourcePath12 = Sheet4.Range("B24")
SourcePath13 = Sheet4.Range("B26")
SourcePath14 = Sheet4.Range("B28")
SourcePath15 = Sheet4.Range("B30")
DestinationPath = "C:\Users\kov4n\OneDrive\Documents\Excel Templates\Management Reports\Data
Extract Folder\"
FileExtn = "*.xlsx*"
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFile Source:=SourcePath & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath2 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath3 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath4 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath5 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath6 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath7 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath8 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath9 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath10 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath11 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath12 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath13 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath14 & FileExtn, Destination:=DestinationPath
FSO.CopyFile Source:=SourcePath15 & FileExtn, Destination:=DestinationPath
The user selects the month on the dashboard sheet and sheet4 holds the file path names which changes based on the selected month. The folders are already set up and each month the team will add files to the area they are working on. Some months, some areas would not have any work so the folder related to that area will not have any files.
Testing this brings up the error message 'run-time error 53 - file not found'
So it copies the first file into the assigned folder moves to the next file path and as it has no files it brings up the above error.
What I need is possible an IF statement that will move to the next folder path when a folder is empty...
Hope someone can help further

Related

Hyperlink does not react when click

What I'm trying to do is to use hyperlink to redirect to local folder. The problem that I facing at the moment is the hyperlink seem to do nothing at all? Here are some of the method that I've tried:
1)
Dim FILEPATH As String = ("file:///D:/try/2019/" & id & "/")
HyperLink1.NavigateUrl = FILEPATH
2)
lblFileLink1.Text = " <a href='D:/HitoSensor_Attachment/2019/" & id & "/'>click</a> to see file"
3)
Dim FILEPATH As String = ("D:/try/2019/" & id & "/")
HyperLink1.NavigateUrl = FILEPATH
I've tried all these three yet the hyperlink react the same which is nothing. Is the problem with my hyperlink or is there anything else that I should fix?
Just do
Process.Start("ValidPath")

Copy and rename folder into another folder based on field value name

I have this code, my objective is to copy and paste a folder (with all its content) from a current template folder to paste onto a destination file path that is based on the field values as a condition to the new file destination name.
I am receiving an error:
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
Not sure why, can you please help. Thank you.
Private Sub Command83_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Database Test Center\Master" '<< Change
ToPath = "C:\Database Test Center\Projects\" & Me.ProjectName.Value & "-" &
Me.Lead.Value & "\MasterTemplate"
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub

File filtering (ASP.Net, VB.Net)

I am making a filtering on files selected by user. Here is my coding that I currently use for button upload click event.
Dim validFileTypes As String() = {"jpg", "JPG", "jpeg", "JPEG", "png", "PNG"}
Dim ext As String = Path.GetExtension(fileUpload.PostedFile.FileName)
Dim isValidFile As Boolean = False
If fileUpload.HasFile Then
For i As Integer = 0 To validFileTypes.Length - 1
If ext = "." & validFileTypes(i) Then 'if selected url got extension like listed above
isValidFile = True 'file is valid
Dim hfc As HttpFileCollection = Request.Files
For j As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(j)
If hpf.ContentLength > 0 Then
hpf.SaveAs(Server.MapPath("source") & "\" & Path.GetFileName(hpf.FileName))
End If
Next
Else
Alert("Failed to upload! Please select file with valid extension.")
End If
Next
Else
Alert("Please select image!")
End If
So if user select file with wrong extension, file will not be uploaded into temporary file. Since the validFileTypes consist of 6 types so the Alert("Failed to upload! Please select file with valid extension.") will appear 6 times even though the file selected is only 1. I tried to fix it but need some guidance from you guys. Thank you.
I have made some improvements on your code and fixed some of the downsides for you.
Dim validExtensions As String() = {"jpg", "JPG", "jpeg", "JPEG", "png", "PNG"}
' Check if request has no file
If Request.Files.Count = 0 Then
Alert("Please select image!")
Else
'Otherwise get files from request
Dim files As HttpFileCollection = Request.Files
'Loop through file names
For Each fileName as String In files.AllKeys
'Get file from posted files
Dim file As HttpPostedFile = files(fileName)
'Check content length of file
If file.ContentLength > 0 Then
'Get file extension of file
Dim extension As String = Path.GetExtension(file.FileName)
'Check if file extension is valid
If validFileTypes.Contains(extension) Then
'Save file
file.SaveAs(Server.MapPath("source") & "\" & Path.GetFileName(file.FileName))
Else
Alert("Failed to upload! Please select file with valid extension.")
Exit For 'Break loop
End If
End If
Next
End If

Issues with exporting table data to excel using Classic ASP

I have a report page which has two labels,two dropdowns,a command button and a table to display the data.
The issue is,while exporting the data to excel, the labels and dropdowns are also getting exported along with the table.
How do I export just the table not any html controls?
It's better export the contents of table to an excel file, by FileSystemObject.
for example :
dim vPath , vRecordset
vPath = "reports/" & objFileSystem.GetTempName & ".xls"
set vRecordset= DBConnection.Execute(SqlQueryString)
set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
set objFile= objFileSystem.OpenTextFile(Server.MapPath(vPath),8,true,-1)
dim strHeader,strLine
strHeader = "<table border=1><tr>"
strLine = ""
for each col in vRecordset.fields
strHeader = strHeader &"<th bgcolor=#bed9fa><b>" & col.name & "</b></th>"
next
objFile.WriteLine strHeader & "</tr>"
do while not vRecordset.eof
strLine = "<tr>"
for each col in vRecordset.fields
strLine = strLine & "<td>" & col.value & "</td>"
next
objFile.WriteLine strLine & "</tr>"
vRecordset.movenext
loop
objFile.WriteLine "</table>"
response.write "<a href='"& vPath &"'>report download</a>"
It will be more smooth for user downloading an existing file to responsing all contents of page directly.

get_included_files in classic ASP?

Is there an equivalent to PHP's get_included_files in classic ASP?
No, there is not.
A very ugly function for that:
<!--#include file="include/common.asp"-->
<%
Function GetIncludedFiles()
Dim Url
Dim Fso
Dim Fs
Dim Src
Dim Arr
Dim Ret
Dim i
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
ReDim Ret(-1)
Url = Request.ServerVariables("URL")
Set Fs = Fso.OpenTextFile(Server.MapPath(Url))
Src = Fs.Readall()
Fs.Close
Set Fs = Nothing
Set Fso = Nothing
Arr = Split(Src, "<" & "!--#include file=")
For i = 0 To UBound(Arr)
Arr(i) = Left(Arr(i), InStr(Arr(i), "-->"))
Arr(i) = Replace(Arr(i), "-", "")
Arr(i) = Replace(Arr(i), "'", "")
Arr(i) = Trim(Replace(Arr(i), """", ""))
If Arr(i) <> "" Then
ReDim Preserve Ret(UBound(Ret) + 1)
Ret(UBound(Ret)) = Arr(i)
End If
Next
GetIncludedFiles = Ret
End Function
Dim File
For Each File In GetIncludedFiles()
Response.Write File & "<br />"
Next
%>
The simple way is to create a main file in a specific directory (for example /include/mainfile.asp) and then include all the other files to this file. Something like:
<!#include File="[your directory here/file1.asp]"-->
<!#include File="[your directory here/file2.asp]"-->
<!#include File="[your directory here/file3.asp]"-->
Then, You can include your main file using "virtual" to the rest of your pages that you want to access those other included files.
<!#include Virtual="/include/mainfile.asp"-->
Not as such, but I vaguely remember seen a tool or two floating around that will give you the equivalent report. It might have been on Code Project or somewhere similar... its been a long time since I last ran across it.

Resources