问题
I have a program that exports a bill of material (see CSV example data lower in post).
I wrote a VBA macro that uses FileSystemObject to loop through a user-selected folder, open the CSV files for reading and read the data into memory using ReadAll, then store and sort the data in excel.
The problem I have is after exporting the CSV files, I run the macro and the text in the CSV file is stored in my string variable (sTemp in the code below) as several square braces. I copied the value of the string variable and pasted it in a text editor and it appears as several repeating black boxes containing "SOH". Because the data is read incorrectly, the macro fails downstream.
If I open the original CSV file, the first two characters are black boxes that say "NUL". NUL NUL "Pattern","Qty","Size","Material","Description","Length (ft)" 2041,,"3","","316L Stainless Sch 10S PE",20 2041,,"3","","316L Stainless Sch 10S PE",2.21 5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD", 2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 Short Radius 90", 2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
If I save the CSV files in Excel close them, the two "NUL" characters are gone: "Pattern","Qty","Size","Material","Description","Length (ft)" 2041,,"3","","316L Stainless Sch 10S PE",20 2041,,"3","","316L Stainless Sch 10S PE",2.21 5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD", 2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 Short Radius 90", 2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
After running the macro on the saved files, the string variable (sTemp in the code below) contains the proper text in the CSV file.
I would like to know how to eliminate the NUL strings in the beginning of the CSV and would like to avoid opening and saving the CSV files programmatically if at all possible.
'Variables for accessing and cleaning text files
Dim oFSO As FileSystemObject 'File System Object grants access to computer files and folders
Dim sSourceFolder As String 'Path of oSourceFolder
Dim oSourceFolder 'Folder the original CSV file is stored in
Dim oFile 'The orignal CSV
Dim sFileToCopy As String 'Path of the copied CSV
Dim sTargetFolder As String 'The path of oTargetFolder
Dim oTargetFolder 'The folder the new CSV file is stored in
Dim oCopiedText 'The copied text file
Dim oCopiedFile 'The copied file itself
Dim oInput 'Represents the text in the CSV ror readin in CSV to memory
Dim sTemp As String 'For storing CSV data in memory
Dim cFiles As New Collection 'Storage for all the files in all the sub folders
Dim sPath As String 'The path of one of the files, will be reused for each file
'variables for progress
Dim iFiles As Integer
Dim Progress As Double
'Constants for reading and writing to text files
Const ForReading = 1
Const ForWriting = 2
'File System Object grants access to computer files and folders
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Select the folder to get CSV files from
sSourceFolder = GetFolder("C:\", "Select Folder Containing CSV files")
Set oSourceFolder = oFSO.GetFolder(sSourceFolder) 'Tell File System Object to get a hold of oSourceFolder so you can look into it
'Check/create the _Cleaned folder inside of the source folder
On Error Resume Next
Err.Clear
sTargetFolder = oSourceFolder.Path & "\" & oSourceFolder.Name & "_Cleaned"
Set oTargetFolder = oFSO.GetFolder(sTargetFolder)
If Err.Number <> 0 Then
'create the folder
oTargetFolder = oFSO.CreateFolder(sTargetFolder)
End If
On Error GoTo 0
'Loop through and clean each CSV so it can be opened in excel properly
iFiles = oSourceFolder.Files.Count
Progress = 0
For Each oFile In oSourceFolder.Files 'go through each file
Application.StatusBar = "Progress: " & Progress & " of " & CStr(iFiles) & ": " & Format(Progress / iFiles, "0%")
If Right(oFile, 3) = "csv" Then 'if it is a text file...
'Copy the original file path
sFileToCopy = oFile.Path
'Open txt file in memory
Set oInput = oFSO.OpenTextFile(sFileToCopy, ForReading)
'Input txt from file to memory
sTemp = oInput.ReadAll
'sTemp contains a repeating SOH string if I do not save the files first and the macro fails downstream
'If I open the CSV file in excel, save it, close it, then run the macro, sTemp contains the string data in the file and the macro runs fine
'Can I somehow delete the NUL strings at this point? . . .
I expect the sTemp value to read the actual text in the CSV file: "Pattern,Qty,Size,Material,Description,Length (ft) 2041,,3,,316L Stainless Sch 10S PE,20"
Instead the sTemp value in the VBA locals window is: "[[[[[[[[[[[[[[[[[[[[[[[[" And when I copy the value and paste it into a text editor it reads: "1 SOH SOH SOH SOH SOH SOH"
Files are saved here: https://www.dropbox.com/sh/e8ohn61bxqidejd/AAB9CMno8N_EXdlA83TBX602a?dl=0.
Any help is appreciated.
回答1:
You indead do have a file with the first two characters as ASCI 0.
Is see two main options:
- Fix the export that created these files to not generate the NULL's, or
- Strip the leading NULL characters.
For option2, it seems ReadAll fails, but ReadLine works.
Demo of stripping the NULLs
Sub Demo()
Dim oFSO As FileSystemObject
Dim oInput As TextStream
Dim sTemp
Set oFSO = New FileSystemObject
Set oInput = oFSO.OpenTextFile("Your\File.csv")
sTemp = oInput.ReadLine
Do While Left$(sTemp, 1) = Chr(0)
sTemp = Mid$(sTemp, 2)
Loop
Do Until oInput.AtEndOfStream
sTemp = sTemp & vbCrLf & oInput.ReadLine
Loop
End Sub
来源:https://stackoverflow.com/questions/54620208/how-to-remove-nul-strings-at-begining-of-csv-vba