How to read csv file with commas in it? - asp-classic

Here is my code in reading csv file using asp. But I'm having a problem if there is a comma within the field.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*** Check Exist Files ***'
If Not objFSO.FileExists(Server.MapPath(sFileName)) Then
Response.write("File not found.")
Else
'*** Open Files ***'
Set oInStream = objFSO.OpenTextFile(Server.MapPath(sFileName),1,False)
Do Until oInStream.AtEndOfStream
sRows = oInStream.readLine
arrRows = Split(sRows,",")
response.write(arrRows(0))
response.write(arrRows(1))
Loop
oInStream.Close()
Set oInStream = Nothing
End IF

Related

Non-printable characters in file names break my recursive file listing VB Script

I created a VB script to recursively list all of its file and subfolder files. The script begins fine but eventually crashes in any folder containing a file with a non-printable character/s in their filenames, i.e. I see little squares when I browse the folder in Explorer. I'm not sure how to change my below error handling to continue when it finds a file with such characters.
Any advice or solutions would be appreciated. Thank you.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\Input\"
Set objFolder = objFSO.GetFolder(strFolder)
Set NewFile = objFSO.CreateTextFile("C:\Output\" & objFolder.Name & " FileList.txt", True)
Set colFiles = objFolder.Files
On Error Resume Next
For Each objFile In colFiles
NewFile.WriteLine(objFile.Path)
If Err Then
Err.Clear
End If
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
NewFile.WriteLine(objFile.Path)
If Err Then
Err.Clear
End If
Next
ShowSubFolders(objSubFolder)
Next
End Sub
NewFile.Close
Create the output text file as unicode so it can handle "non printable" characters. Third parameter of CreateTextFile.
Set NewFile = objFSO.CreateTextFile(" ... ", True, True)
EDITED
If you can not work with unicode files, then file/folder names should be converted from unicode to ansi before writing to output file. This will do the conversion
Function Unicode2Ansi( text )
Unicode2Ansi = text
With (WScript.CreateObject("ADODB.Stream"))
' Put data into stream
.Type = 2 '( adTypeText )
.Charset = "x-ansi"
.Open
.WriteText text
'Retrieve data from stream
.Position = 0
Unicode2Ansi = .ReadText
.Close
End With
End Function
And adapt code to call it NewFile.WriteLine Unicode2Ansi(objFile.Path)

Batch script: Write date of oldest file in a dir to text file

I am trying to write the modified date of the oldest file in a directory to a text file. At the moment i can write all the file names (i named them the date they were created but) in oldest to newest order but cant seem to limit this to just output the oldest or indeed get the modified or created date/time. My directory is remote in case it makes a diff and my current attempt is as follows:
dir "\\dirxxxx\xxxxxxx\xxxxx\xxxxx\xxxxx\*.*" /b /a-d /o-d`
Any ideas how i can get the creation or modified date of a file written to a text file in same dir?
try this:
for /f "delims=" %i in ('dir /b/o-d/a-d') do set "oldesttime=%~ti"
>"log.txt" echo %oldesttime%
Solution: use vbs instead.
Option Explicit
Dim fso, path, file, recentDate, recentFile, objFileHandle
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
For Each file in fso.GetFolder("\\xxxxxx\xxxxxxx").Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf (file.DateLastModified < recentFile.DateLastModified) Then
Set recentFile = file
End If
Next
If recentFile is Nothing Then
WScript.Echo "no recent files"
Else
WScript.Echo recentFile.DateLastModified
Set objFileHandle = fso.OpenTextFile("\\Vxxxxxx\xxxxx", 2, "True")
objFileHandle.Write(recentFile.DateLastModified)
objFileHandle.Close
End If

How do I update a files content using vbscript without updating it's modified date?

A few large sites are online with incorrect doctypes (4.0 not 4.01). I have written a script to update any files with this content but it updates the date modified for the file and I want to keep this as it's original date, is this possible in vbscript (with classic asp)?
Here is my code to simply replace the file contents:
' My folder root is here
FileRoot = "D:\myRootHere\"
' Now I use Persits upload because I need the impersonation setting below it
Set Upload = Server.CreateObject("Persits.Upload")
' Set impersonation to authorise file over-writing (this is correct and working)
Upload.LogonUser "localhost","myUsername","myPassword"
Set Dir = Upload.Directory(FileRoot & "*.asp", SORTBY_TYPE)
For Each item in Dir
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(FileRoot & item.FileName, ForReading)
strText = objFile.ReadAll
objFile.Close
If instr(strText,"HTML 4.0 Transitional") then
strNewText = Replace(strText,"HTML 4.0 Transitional","HTML 4.01 Transitional")
Set objFile = objFSO.OpenTextFile(FileRoot & item.FileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End If
Next
Set Upload = nothing
I took this example from here
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(FilePath)
Set objFolderItem = objFolder.ParseName(FileName)
objFolderItem.ModifyDate = "01/01/2013 8:00:00 AM"
And tested ok on a file inside a folder with Everyone having Full Control

What's the correct way to specify file path in VBscript?

New to VBscript and spending way too much time trying to find the right way to open a file for reading. Whatever I've tried I always get "Path not found" error.
This is the real path to my files:
D:\InetPub\vhosts\lamardesigngroup.com\httpdocs\
The file that I am trying to run is:
D:\InetPub\vhosts\lamardesigngroup.com\httpdocs\ifp\files.asp
and I want to read this file:
D:\InetPub\vhosts\lamardesigngroup.com\httpdocs\ifp\css\style.css
Here is the code:
Dim objFSO, strTextFile, strData, strLine, arrLines
CONST ForReading = 1
'name of the text file
strTextFile = "//css/style.css"
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
'Split the text file into lines
arrLines = Split(strData,vbCrLf)
'Step through the lines
For Each strLine in arrLines
response.write(strLine & "<br>")
Next
'Cleanup
Set objFSO = Nothing
and I get "12|800a004c|Path_not_found 80"
I've also tried
strTextFile = "D:\InetPub\vhosts\lamardesigngroup.com\httpdocs\ifp\css\style.css"
' and
strTextFile = "\\css\style.css"
strTextFile = "css\style.css"
strTextFile = "css/style.css"
' and many other combinations
I'm obviously lost...
Morning Harley,
Give this a try:
strTextFile = server.MapPath("css/style.css")
It should result in recognizing your specific server location. I ran into this problem trying to get some vbscript file upload code to work. It should start from the folder your page is working in and go from there.

ASP SaveToDisk method takes an incredible amount of time

This is a method in ASP Classic that saves a file to disk. It takes a very long time but I'm not sure why. Normally, I wouldn't mind so much, but the files it handles are pretty large so need this needs to faster than 100kB a second save. Seriously slow. (old legacy system, band aid fix till it gets replaced...)
Public Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex
If sPath = "" Or FileName = "" Then Exit Sub
If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\" '"
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then Exit Sub
Set oFile = oFS.CreateTextFile(sPath & FileName, True)
For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next
oFile.Close
End Sub
I'm asking because there are plenty of WTF's in this code so I'm fighting those fires while getting some help on these ones.
I don't see your definition for "FileData" anywhere in your code - where is this coming from? Is there a reason you're writing it to disk a single character at a time? I'd suspect this is your problem - writing 100K of data takes 100K trips through this loop, which could be the reason for your slowdown. Why can't you replace the write loop at the bottom:
For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next
with a single statement to write the file all at once?
oFile.Write FileData
What you should do is read the binary request into an ADODB.Stream object and convert it to plain ASCII text in a single fast step.
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.Write Request.BinaryRead(Request.TotalBytes)
objStream.Position = 0
objStream.Type = 2
objStream.Charset = "ISO-8859-1"
FormData = objStream.ReadText
objStream.Close
Set objStream = Nothing
Notice how the variable FormData now contains the form data as text. Then you parse this text and locate the start and length of each file, and use ADODB.Stream CopyTo method to extract the specific portion of the file and save it do disk.

Resources