Getting internal server error when trying to list top 100 newest files in folder - asp.net

I have a folder with 114,000 files in it (not under my control) and I want to list only the latest 100 of them that have been modified. So I hobbled together an ASP script from different sources, but it's giving me an internal server error when I run it.
<%
Function SortFiles(files)
ReDim sorted(files.Count - 1)
Dim file, i, j
i = 0
For Each file in files
Set sorted(i) = file
i = i + 1
Next
For i = 0 to files.Count - 2
For j = i + 1 to files.Count - 1
If sorted(i).DateLastModified < sorted(j).DateLastModified Then
Dim tmp
Set tmp = sorted(i)
Set sorted(i) = sorted(j)
Set sorted(j) = tmp
End If
Next
Next
SortFiles = sorted
End Function
dim fileserver,files,file,i
set fileserver=Server.CreateObject("Scripting.FileSystemObject")
set files=fileserver.GetFolder(Server.MapPath(".")).Files
i = 0
For Each file in SortFiles(files)
Response.write(x.Name & "\t" & x.Size & "\n")
i = i + 1
If i > 100 Then
Exit For
End If
Next
set fo=nothing
set files=nothing
%>
The files I want listed just need to have their name and size separated by new lines. I am new to ASP so I'm not sure how to debug this.

Move the code from aspx to aspx.cs (code behind) and you can debug it by adding breakpoint, and F10/F11 to step over/step into. I don't know if you can debug inline code, looks like you can by some SO answer.
At first glance, SortFiles does not return a list of file, so it can not be used in a For Each loop.
For Each file in SortFiles(files) //exception
And what is this for?
SortFiles = sorted
I don't know VB, but you can debug yourself.

Related

How to get telegram bot getUpdates json from vbscript and read it

i wanted to create my bot in VBScript (i know its like troll and bad idea probably, i can do it in lua, python, C#, PHP, ...., but i decided to try and make it from vbscript)
the hard part is that i'm trying to Retrieve information from Telegram getUpdates
i've made this code for example and it kind of works, i'll explain what works and what doesn't
Dim fso, outFile, TeleTest
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)
set TeleTest = fso.CreateTextFile("TeleTest.txt", True)
Dim url, req, json
Set req = CreateObject("MSXML2.XMLHTTP")
url = "https://api.telegram.org/bot"[TOKEN]"/getUpdates"
req.open "GET", url, False
req.send
If req.Status = 200 Then
TeleTest.Write req.responseText
End If
' Load the JSON array into a JsonArray:
set jsonArray = CreateObject("Chilkat_9_5_0.JsonArray")
success = jsonArray.Load("TeleTest.txt")
If (success <> 1) Then
outFile.WriteLine(jsonArray.LastErrorText)
WScript.Quit
End If
' Get some information from each record in the array.
numRecords = jsonArray.Size
i = 0
Do While i < numRecords
outFile.WriteLine("------ Record " & i & " -------")
' jsonRecord is a Chilkat_9_5_0.JsonObject
Set jsonRecord = jsonArray.ObjectAt(i)
outFile.WriteLine(" ok: " & jsonRecord.StringOf("ok"))
outFile.WriteLine(" result: " & jsonRecord.SizeOfArray("result"))
' Examine information for this record
u = 0
Do While u < nummessage
nummessage = jsonRecord.SizeOfArray("result[u].message")
Loop
outFile.WriteLine("Number of message: " & nummessage)
j = 0
Do While j < nummessage
jsonRecord.J = j
outFile.WriteLine(" message text: " & jsonRecord.StringOf("result[j].message[j].text"))
j = j + 1
Loop
i = i + 1
Loop
outFile.Close
so the first part that should get updates and save it ino TeleTest.txt works fine, it gets updates, it saves the json in to the .txt file (or anything, i can also save it into string in the vbs, or .json file)
the problem is that the second part where i'm using Chilkat gives error
Blockquote
ChilkatLog: Load:
ChilkatVersion: 9.5.0.78
Unable to get array at index 0. --Load
--ChilkatLog
any help or any idea would be appereciated, also if Chilkat is not good for doing this, maybe tell me why and give me something else?! (Chilkat was the only dll i found to work with vbscript and does json reading, stuff)
i got it to working, i found out that from this example
Chilkat needs the Json file to like this
[ { json } ]
but the Telegram json is like this
{ json }
so, the fix would be easy to just change line 15 from TeleTest.Write req.responseText to this code below
TeleTest.Write "[" + req.responseText + "]"
my code now works fine , if anyone else found something wrong or any answer to my question it would be appreciated
i hope someone else who needs this find this

Classic ASP which folder does a file belong to

I have a folder structure which stores images by the thousand, take for example the following folder names;
00001-01000
01001-02000
02001-03000
03001-04000
04001-05000
05001-06000
06001-07000
I am then dynamically rendering a page to display all sorts of images. In the page i am trying to build a link to the image, but what i need is to figure out what folder an image belongs to. For instance, i am using the following to get the image id from the database
<%= rs.Fields("imageid") %>
If that were to return '04232' the folder name that would belong to would be '04001-05000'.
Is there any way that i could figure out what the folder name would be, by only having the imageid in classic asp?
For this you need two small methods. First, padding a string:
Function PadLeft(str, padChar, desiredLength)
Dim result
result = CStr(str)
Do Until Len(result)>=desiredLength
result = padChar & result
Loop
PadLeft = result
End Function
And the method to find the folder name:
Function ExtractFolderName(imageFileName)
Dim numericValue, rangeStart, rangeEnd
numericValue = CInt(imageFileName)
rangeStart = (Fix((numericValue / 1000)) * 1000) + 1
rangeEnd = (Fix((numericValue / 1000)) + 1) * 1000
ExtractFolderName = PadLeft(rangeStart, "0", 5) & "-" & PadLeft(rangeEnd, "0", 5)
End Function
To use it:
folderName = ExtractFolderName(rs("imageid"))
Basically, the method performs some math on the name to find the desired range. Note this will throw error in case of a non numeric value in the database.

FileSystemObject.FolderExists returning wrong value

The following code:
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(path) Then
fso.CreateFolder(path)
End If
Is producing the following error:
Microsoft VBScript runtime error '800a003a' File already exists
If I delete the folder, so that ASP is able to create it itself, it works as expected.
It's only when I manually (or using DOS MKDIR) create it that FolderExists returns false and CreateFolder throws the above error.
What's going on here?
EDIT:
The variable path contains the string C:\Windows\Temp\email_attachments\ and FolderExists seems to be returning false for directories that have been around since before the last startup.
Hi my worked code.
file_path="C:\inetpub\wwwroot\upload\2018\4"
set fso=Server.CreateObject("Scripting.FileSystemObject")
tmpArr = Split(file_path, "\")
tmpPath = tmpArr(0)
For i = 1 To UBound(tmpArr)
If Not fso.FolderExists(Server.MapPath(tmpPath)) Then
fso.CreateFolder Server.MapPath(tmpPath)
End If
tmpPath = tmpPath & "\" & tmpArr(i)
Next

Check to see if file name exists in directory and rename if it does before completing upload?

I am creating a application that allows users to upload an image to the server of my site. But I want to allow users to upload multiple files with the same name by renaming the new file.
But I don't know what methods to use to perform this check.
I am thinking about trying to put all of the filenames into an arraylist individually and creating a loop to check the new filename against the rest and if there is a match then I would randomly generate a string of letters to tag onto the new filename.
But I haven't figured out how to populate the arraylist yet and this is the code I have for checking for repeats against the arraylist:
Dim i As Integer = 0
For i = 0 To arrayFileNames.Count
If (fileName = arrayFileNames(i)) Then
Dim random As Random = New Random()
random.Next(1, 100000)
fileName = fileName & random.ToString
End If
i = i + 1
Next
But this code throws the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Any help would be appreciated, thank you.
If you're creating a For loop, you don't need to manually increase the counter. Remove the line i = i + 1.
Dim i As Integer = 0
For i = 0 To arrayFileNames.Count
If (fileName = arrayFileNames(i)) Then
Dim random As Random = New Random()
random.Next(1, 100000)
fileName = fileName & random.ToString
End If
Next
The for loop already takes care of increasing i.
You'll have a problem if you get a new fileName that is equal to a previous index.
When you create a new instance of random, you'll get the same value.
random.ToString() doesn't return what you think it does (check the output).
You might get a very big filename if you keep appending a big number.
You'll have to change your logic (I haven't tested this code, it's an example to help you out).
Dim random As Random = New Random()
Do While arrayFileNames.Contains(fileName)
fileName = fileName & random.Next(1, 9).ToString
Loop

Classic ASP Array not returning values, error 500

I'm working on executing the same code several times to produce a table. My first thoughts went out to using an array to do this.
Here is what i have got so far:
Dim iRow
iRow = 0
'alternate color for rows
Do While Not rsGlobalWeb.EOF
If iRow Mod 2 = 0 Then
response.write "<tr bgcolor=""#FFFFFF"">"
Else
response.write "<tr bgcolor=""#EEEEEE"">"
End If
'some other code
SqlBackup = "SELECT * FROM CMDBbackup WHERE Naam_Cattools = '" & rsGlobalWeb("Device_name") & "'"
Set rsBackup = Server.CreateObject("ADODB.Recordset")
rsBackup.Open SqlBackup, dbGlobalWeb, 3
'declaration of array
Dim fieldname(5),i
fieldname(0) = "Device_name"
fieldname(1) = "Image"
fieldname(2) = "Backup"
fieldname(3) = "Uptime"
fieldname(4) = "Processor"
fieldname(5) = "Nvram"
For i = 0 to 5
If rsGlobalWeb(fieldname(i)) <> "" Then
response.write("<td>" & rsGlobalWeb(fieldname(i)) & "</td>")
Else
If Not rsBackup.EOF Then
If Not IsNull(rsBackup(fieldname(i))) And (rsBackup(fieldname(i)) <> "") Then
response.write("<td>" & rsBackup(fieldname(i)) & " (backup)</td>")
End if
Else
response.write("<td>No data found</td>")
End if
End if
Next
response.write("</tr>")
iRow = iRow + 1
rsGlobalWeb.MoveNext
Loop
The issue i have now is that the following error occurs even tho i have friendly messages turned off:
"500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed."
The logfile shows the following:
"DaEngineSDB.asp |58|800a000d|Type_mismatch 80 -" Where the 58 is the line with the Dim Fieldname.
Without the array it does show the remainder of the code (i have 1 other field which gets added). If i remove the array and fill the fieldname(i) with a normal string value it also works fine.
I was trying out stuff that google says but after attempting several things i am still running up to a wall.
Any ideas what it could be?
Thanks in advance,
Erik
First you should turn on error displaying in your iis, or read the error log for its description, google it if not sure how.
Without error description, it's way too difficult to check what is wrong.
Problem solved!
After banging my head against the wall for a day i found out that i stupidly declared the array inside the DO WHILE loop. Moved the declaration out of it and problem solved.

Resources