I am trying to separate a comma delimited file and for some reason I seem to not be getting the output that I was expecting to get.
Here is the code:
strCSVPath = "E:\cfaApp\tester.csv"
int77 = FreeFile
Open strCSVPath For Input As #int77
Do Until EOF(int77)
Input #int77, strName, intHours, strMon, strTue, strWed, strThu, strFri, strSat
Debug.Print strName & vbCr & intHours
'Debug.Print strName & vbCr & intHours & vbCr & strMon & vbCr & strTue & vbCr & strWed & vbCr & strThu & vbCr & strFri & vbCr & strSat & vbCr
Loop
The output looks like this:
1.0-Store Manager (1 Employee)
11:00 AM-8:30 PM
10:00 AM-7:30 PM
1.1-Assistant Managers
Wood, Chris
Above output is wrong. This is the CSV file (some of it, not all of it)
1.0-Store Manager (1 Employee),,,,,,,,
"Pro, Bob",1.0-Store Manager,47.5,5:30 AM-3:00 PM,5:30 AM-3:00 PM,11:00 AM-8:30 PM,11:00 AM-8:30 PM,9:00 AM-6:30 PM,OFF
1.1-Assistant Managers (3 Employees),,,,,,,,
"Crow, Billy",1.1-Assistant Managers,47.5,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,OFF,11:00 AM-8:30 PM
"Ras, Pat",1.1-Assistant Managers,47.5,5:30 AM-3:00 PM,9:00 AM-6:30 PM,10:00 AM-7:30 PM,,11:00 AM-8:30 PM,9:00 AM-6:30 PM
"Wood, Chris",1.1-Assistant Managers,47.5,,11:00 AM-8:00 PM,8:30 AM-6:30 PM,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM
1.2- Supervisors (7 Employees),,,,,,,,
As you can see, the output does skips Pro, Bob altogether. And then skips Crow and Ras before displaying Wood.
I also get an error of
Input past end of file
In short, Input # is not designed to read CSV. See http://msdn.microsoft.com/en-us/library/aa243386(v=VS.60).aspx
There may be an external CSV parser you could use from VB6 but I don't know of one offhand. You could write a CSV parser fairly easily that gets one character at a time and uses a finite state machine.
It's been a while since I have used VB6, but I happened to remember that it is possible to use the ADODB.Recordset object to query the CSV file like a SQL database.
Here is Microsoft documentation, including how to read CSV files without header rows.
Try using a other separator like ; or Pipe.
Then see if the output is okay.
If YES try to quote the commas in the name or set the name in other quotes like '
Have Fun,
John
Related
I'm fairly new to scripting and am in need of some help. I have come across a unique situation for a Non-Profit client of ours that requires us to compare two or more files in a specific folder and move the file with the lowest numerical value in the filename.
This organization runs a non-profit radio station which has content submitted from hundreds of volunteers that name their files (when they record more than one) with various numbers at the end that either represent the date or the order in which the files are to be aired.
Essentially I am looking to create a vbscript (because I think it can be done this way) that will run with windows task scheduler 30 minutes prior to the first air date of the content and move the file with the lowest value (if more than one file exists) to a folder where it will be automatically processed by the radio automation software.
Examples of files in a folder might look something like these:
Folder1: (in this instance, "news.mp3" is the lowest value)
news.mp3
news1.mp3
news2.mp3
Folder2:
entertainment24.mp3
entertainment26.mp3
Folder3:
localnews081420.mp3
localnews081520.mp3
Honestly, on this one, I'm not even sure where to start. I've found several scripts that can look at file date or a specific numerical or date format in the filename, but none that can parse numbers from a filename and move/copy a file based on the numerical value. I'm hoping there is someone out there smarter than me that can point me in the right direction. Thanks for looking at my problem!
One script I've been playing with (from the scripting guy) looks at specific years in a filename:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colFiles = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _
& “ResultClass = CIM_DataFile”)
Set objRegEx = CreateObject(“VBScript.RegExp”)
For Each objFile in colFiles
objRegEx.Global = True
objRegEx.Pattern = “\d{4}”
strSearchString = objFile.FileName
Set colMatches = objRegEx.Execute(strSearchString)
strYear = colMatches(0).Value
strNewFile = “C:\Test\” & strYear & “\” & objFile.FileName & _
“.” & objFile.Extension
objFile.Copy(strNewFile)
objFile.Delete
Next
...but I can't seem to make the leap to regular numbers and then take a lowest value...
You can use FileSystemObject to Work with Drives, Folders and Files.
Also i used GETNUM function to get number.
Try my way :
sFolder = "C:\Test\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile in oFSO.GetFolder(sFolder).Files
Number=GETNUM(objFile.Name)
strNewFile = sFolder & Number & "\" & objFile.Name
If NOT (oFSO.FolderExists(sFolder & Number)) Then
oFSO.CreateFolder(sFolder & Number)
End If
oFSO.MoveFile objFile, strNewFile
Next
Function GETNUM(Str)
For i=1 To Len(Str)
if IsNumeric(Mid(Str,i,1)) Then
Num=Num&Mid(Str,i,1)
End if
Next
GETNUM=Num
End Function
For understanding the used code and how they work, open these sites and read all pages very carefully.
MoveFile method
Vbs Script to check if a folder exist
I am trying to save excel file with previous date in vbscript.
Fill has been save but date format not like 5-Jun-2019.
Please help me..
MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now)-1, 2)
wbm.SaveAs "C:\report " & MyDateFormat & ".xlsx"
Expected Actual Result like:5-Jun-2019
date-1 gives yesterday "05.06.2019" in my case, this will not let you encounter day = 00, rest should be done by formatting
I am trying to get the system time in 24 hour format using VBScript. I had gone through the documents and found nothing. I need to compare the system date and time with my data and need to check the differences.
WScript.Echo right("0" & hour(time),2) & ":" & right("0" & minute(time),2)
WScript.Echo FormatDateTime(time, vbShortTime)
VBScript's Date datatype is format-agnostic. You can calculate the difference between the system time and some other timestamp using the DateDiff function, as long as VBScript recognizes the format of the other timestamp. Example (calculating the difference in seconds):
>>> systime = Now
>>> WScript.Echo systime
26.11.2013 12:48:52
>>> WScript.Echo DateDiff("s", systime, "11/25/2013 23:16:52")
-48720
>>> WScript.Echo DateDiff("s", systime, "25.11.2013 23:16:52")
-48720
i need to have an ascii file that have several lines in it for accounting.
in everyline i will have the text and numbers for example numbers and spaces with specific length for every column of data
first column is 3 char length
second is 5
third is 10 and etc...
then i need the end of the line to end with CR + LF
how do i do an ascii file from classic asp and vbscript?
You use FSO (FileSystemObject) to work with files in VBScript. This MSDN Page, Working with Files, shows you how to create and write to files.
Here's a page that has a sample that uses VBScript in an ASP page to create a text file.
My guess, you need to manage a text file like a database. If I'm right, you can do it using Text File Driver.
You need a schema.ini file for the data construct configuration and an existing text file (myfile.csv).
schema.ini
[myfile.csv]
Format=FixedLength
CharacterSet=ANSI
ColNameHeader=False
Col1=first Text Width 3
Col2=second Text Width 5
Col3=third Text Width 10
;[myotherfile.csv]
;Format=FixedLength
;CharacterSet=ANSI
; etc.
myfile.csv (maybe not certain but there are three columns per line with the above configuration.)
abcdefghijklmnopqrstu
123123451234567890
Things to do side of ASP are like classical database operations also.
Const adLockReadOnly = 1
Dim adoCon, adoRS
Set adoCon = Server.CreateObject("Adodb.Connection")
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.Mappath(".") & _
";Extended Properties=""text"""
Set adoRS = Server.CreateObject("Adodb.Recordset")
With adoRS
.Open "Select * From [myfile.csv]", adoCon, , adLockReadOnly
While Not .Eof
Response.Write( _
.Fields("first").Value & " - "& _
.Fields("second").Value & " - "& _
.Fields("third").Value & _
"<br />")
.MoveNext
Wend
.Close
End With
Set adoRS = Nothing
'Data insert : new line ends with CR + LF automatically.
adoCon.Execute "Insert Into [myfile.csv] Values('aaa','bbbbb','cccccccccc')"
adoCon.Close
Set adoCon = Nothing
I need to either write a batch file or a vbscript that will rename files. I need to keep everything in the file name up to the second "." but delete what comes after the second dot.
This is a sample of what the file names look like:
nnnnnnnnnnnnnnnn.xxxxxxxx.dddddddddd.pdf
n= 16 numbers 0-9
x= date in this format ex:02232008
d= 10 numbers 0-9, this is the part of the file name that I want to delete.
I need the d's from the sample above to be deleted but keep the rest of the file name the same. I need to be able to run this batch file on a folder that contains about 3,000 pdf files. It can either be put right back into the same folder or outputted into a different folder.
FOR /F "USEBACKQ delims=. tokens=1-4" %%F IN (`DIR /B /A-D "C:\Path\To\PDFs\"`) DO (
REN "%%~fF.%%G.%%H.%%I" "%%F.%%G.%%I"
)
If you have files that vary in how many periods there are, just need to add a simple argument to count how many period delimiters exist then execute.
In VBScript, you can use something like
' the file paths. hardcoded, but you could alternatively collect these via command line parameters
Const IN_PATH = "path\to\directory"
Const OUT_PATH = "path\to\another\directory"
' check that the directories exist. you could create them instead, but here
' it just throws an error as that's easier
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(IN_PATH) then
err.raise 1,, "Path '" & IN_PATH & "' not found"
end if
if not fso.FolderExists(OUT_PATH) then
err.raise 1,, "Path '" & OUT_PATH & "' not found"
end if
dim infolder: set infolder = fso.GetFolder(IN_PATH)
dim file
for each file in infolder.files
dim name: name = file.name
dim parts: parts = split(name, ".")
' we're expecting a file format of a.b.c.pdf
' so we should have 4 elements in the array (zero-indexed, highest bound is 3)
if UBound(parts) = 3 then
' rebuild the name with the 0th, 1st and 3rd elements
dim newname: newname = parts(0) & "." & parts(1) & "." & parts(3)
' use the move() method to effect the rename
file.move fso.buildpath(OUT_PATH, newname)
else
' log the fact that there's an erroneous file name
WScript.Echo "Unexpected file format: '" & name & "'"
end if
next 'file
You would run it in a batch file thus, redirecting output to a log file
cscript rename-script.vbs > logfile.txt
This assumes that you can simply rely on the period to delimit the parts of the file name rather than the specifics of the format of the delimited parts.
To rearrange the date, which I think is in the parts(1) array element, you can simply extract each bit of the string because it's in a specific format:
'date in format mmddyyyy
dim month_, day_, year_, date_
month_ = left(parts(1), 2)
day_ = mid(parts(1), 3, 2)
year_ = right(parts(1), 4)
date_ = year_ & month_ & day_ ' now yyyymmdd
so when rebuilding the filename, you can replace parts(1) with the new formatted date
dim newname: newname = parts(0) & "." & date_ & "." & parts(3)
Using StringSolver, a semi-automatic renaming tool, just rename the first file, check that the generalized renaming is ok, and then accept it on all other files.
> move 1234567890123456.02232008.1946738250.pdf 1234567890123456.02232008.pdf
Get the explanation:
> move --explain
the file name until the end of the second number + the extension
If you are satisfied, you can run the semi-automated tool using move --auto or the succint version:
> move
DISCLAIMER: I am a co-author of this free software made for academic purposes.