Please confine each of your topics to a single thread. You currently have more than three threads all requesting the same solution. Not only is this confusing to the members, but to anyone trying to follow along at home.
You are given me VB Script but by using this we can create blank excel file. but by using below code we can replace *.txt file in *.xls.
set dr=%systemdrive%\yourfolderpath&CLS
for /f "tokens=1*" %%a in ('dir /a %dr% /b') do (
call copy "%dr%\%%a%%b"
rename * *.EXL
)
The solution is very easy, just copy the files and save them as .xls .
If only it were that easy. You cannot turn files into proprietary file formats by copying them and changing the extensions.
The solution is not confined to VBScript. However, VBScript is installed with Windows, making it accessible to the most users. In any case you need to create an instance of Excel and let Excel handle the format of the resulting workbook.
Const F_FOLDER = "c:\temp"
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set xlApp = CreateObject("Excel.Application")
Set f = fso.GetFolder(F_FOLDER)
Set fc = f.Files
For Each fs In fc
If LCase(fso.GetExtensionName(fs)) = "txt" Then
Set xlBook = xlApp.Workbooks.Add()
Set xlSheet = xlBook.Worksheets(1)
Set h = fso.OpenTextFile(F_FOLDER & "\" & fso.GetBaseName(fs) & ".hdr", ForReading)
numRow = 1
numCol = 3
xlSheet.Cells(1,1).Value = "Date"
xlSheet.Cells(1,2).Value = "Time"
Do Until h.AtEndOfStream = True
strHdr = h.ReadLine()
If Not IsNumeric(strHdr)
xlSheet.Cells(numRow, numCol).Value = strHdr
numCol = numCol + 1
End If
Loop
h.Close
Set t = fso.OpenTextFile(F_FOLDER & "\" & fso.GetBaseName(fs) & ".txt", ForReading)
numRow = 2
Do Until t.AtEndOfStream = True
xlSheet.Cells(numRow, 1).Value = Date
xlSheet.Cells(numRow, 2).Value = Time
arrTxt = Split(t.ReadLine(), " ") 'space delimiter in txt file
numCol = 3
For Each txt in arrTxt
xlSheet.Cells(numRow, numCol).Value = txt
numCol = numCol + 1
End If
numRow = numRow + 1
Loop
t.Close
xlBook.SaveAs F_FOLDER & "\" & fso.GetBaseName(fs) & ".xls"
End If
Next
xlApp.Quit
Thank you for the warm New Year's greeting. As my New Year's gift to you, I give you the responsibility of testing the code.
I didn't think it fair that I have all the fun.
Save the script with a
vbs extension and run from the command prompt as
wscript scriptname.vbsGood luck.
Notes: You never mention how the data in the
txt files was arranged. The script uses a space as the delimiter. This can be changed.
Excel workbooks have XLS extensions, not EXL