Blog: Tag Archives: FileSystemObject

ASP – Get a Random file from folder

Posted On:

<% 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set fold = fso.GetFolder(Server.MapPath("folderName")) 
    Set fileset = fold.files 
    fileCount = fileset.count 
 
    if fileCount > 0 then 
 
        counter = 1 
        randomize 
        fileToPick = clng((rnd * fileCount) + 0.5) 
 
        for each file in fileset 
            if counter < fileToPick then 
                counter = counter + 1 
            else 
                randomFile = file.name 
                exit for 
            end if 
        next 
 
        response.write randomFile 
 
    else 
 
        response.write "Empty folder." 
 
    end if 
 
    set fileset = nothing 
    set fold = nothing 
    set fso = nothing 
%>

ASP – Generate a CSV file on the fly

Posted On:

strOutPut = "Name, Email Address"
IF ListType="" THEN
	strOutPut = strOutPut&", Product News, Company News, Special Offers"
END IF

strOutPut = strOutPut&vbcrlf


	DO WHILE NOT objRecordSet.EOF
	strOutPut = strOutPut & objRecordSet("Mailing_Name")&","&objRecordSet("Mailing_EmailAddress")
	IF ListType="" THEN
		IF objRecordSet("Mailing_ProductNews")=1 THEN
			strOutPut=strOutPut&",Y, "
		ELSE	
			strOutPut=strOutPut&",N, "
		END IF
		IF objRecordSet("Mailing_CompanyNews")=1 THEN
			strOutPut=strOutPut&"Y, "
		ELSE	
			strOutPut=strOutPut&"N, "
		END IF
		IF objRecordSet("Mailing_SpecialOffers")=1 THEN
			strOutPut=strOutPut&"Y"
		ELSE	
			strOutPut=strOutPut&"N"
		END IF
	END IF	
		strOutPut=strOutPut&vbcrlf
	objRecordSet.MoveNext
	LOOP
	objRecordSet.Close

Set objRecordSet = nothing


Response.Clear
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "filename=" & FileName & ";"
Response.Write(strOutPut)