Computing.Net > Forums > Programming > Batch to read data and output

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to read data and output

Reply to Message Icon

Name: AKL-MFCU
Date: November 15, 2005 at 15:53:38 Pacific
OS: Windows XP Professional
CPU/Ram: 2.8ghz
Comment:

Hi all,

Is there anyway to set up a batch file or other program to read data from a .csv file and tell me if any from a preselected list is missing? Currently every day we run a report that lets us know how many and what types of reports have been run that day. I have to manually go through 3 pages of excel formatted names and checkmark them if they are not there(tedious and paper wasting). These program names are standard without any variation in their name, so is there a way to set it up to check if acpd001, acrd003, aurs001, etc, etc. is in the csv file and if they aren't to output it to a txt file called missing.txt for review? Let me know if this is something easily possible or not. The csv file has other title names, but in particular I check the ones under PROGRAM NAME




Sponsored Link
Ads by Google

Response Number 1
Name: Dr. Nick
Date: November 15, 2005 at 18:17:29 Pacific
Reply:

I wanted to brush up on my WSH skills :)

How about something like this?

'Check for correct number of arguments
If WScript.Arguments.length <> 2 Then
   WScript.Echo "Usage: cscript.exe CheckLog.vbs LogFile InputFile" & _
      vbCrLf & vbCrLf & "LogFile: Report log file." & vbCrLf & _
      "InputFile: List of report names that should appear in LogFile " & _
      "(one name per line)."
   WScript.Quit 1
End If

'Dim the variables we're going to use
Dim WshShell, strWorkDir, numFiles, numFolders, fso, logFilePath
Dim logFile, inputFilePath, inputFile, resultFile, today
Dim logFileText, inputFileText, reportNames, val

'Some useful constants
Const ForReading = 1, ForWriting = 2

'Create the almighty shell and FSO
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'Get file names from command line
logFilePath = WScript.Arguments(0)
inputFilePath = WScript.Arguments(1)

'Make sure the files exist
If logFilePath = "" Or Not fso.FileExists(logFilePath) Then
   WScript.Echo "Invalid LogFile specified."
   WScript.Quit 1
End If

'And again...
If inputFilePath = "" Or Not fso.FileExists(inputFilePath) Then 
   WScript.Echo "Invalid InputFile specified."
   WScript.Quit 1
End If

'Begin scan
WScript.Echo "Starting scan..." & vbCrLf

'Format today's date
today = Year(Date) & "-" & Month(Date) & "-" & Day(Date)

'Open the three files we're going to use
Set logFile = fso.OpenTextFile(logFilePath, ForReading)
Set inputFile = fso.OpenTextFile(inputFilePath, ForReading)
Set resultFile = fso.OpenTextFile(today & " Report Log.log", ForWriting, true)

'Start results file with the date
resultFile.WriteLine("Missing Reports for " & today & ":" & vbCrLf)

'Read in the two input files
logFileText = logFile.ReadAll()
inputFileText = inputFile.ReadAll()

'Split the names file into an array
reportNames = Split(inputFileText, vbCrLf, -1, 1)

'For every name in the array, check and see if it's in the report log
For Each name In reportNames
   val = InStr(1, logFileText, name, 1)
   If val = 0 Or val = Null Then
      WScript.Echo "Report '" & name & "' not found."
      resultFile.WriteLine(name)
   End If
Next

'Finish up
resultFile.WriteLine(vbCrLf & "End Scan")
WScript.Echo vbCrLf & "Finished."

'Clean up
inputFile.close
logFile.close
resultFile.close
Set fso = Nothing
Set resultFile = Nothing
Set logFile = Nothing
Set inputFile = Nothing

Copy/paste that into a text file named "CheckLog.vbs". It runs from a command-line but you can see the syntax by double-clicking the file. You'll need to create a file containing a list of report names to check for to test it. This should run fine under 2000, XP, and 2003.

Does it work like you wanted? If not, please post (or email me [click the "Dr. Nick" link at the top of this post]) a copy of the report you're trying to check so I can get an idea of what input to expect.



0

Response Number 2
Name: Dr. Nick
Date: November 15, 2005 at 18:19:04 Pacific
Reply:

Doh, forgot to mention that it outputs the results of the scan to a file called "YYYY-MM-DD Report Log.log" in addition to the console.


0

Response Number 3
Name: Mechanix2Go
Date: November 16, 2005 at 02:51:23 Pacific
Reply:

Hi AKL,

Try this:

:: chk4miss.bat
@echo off > missing.txt

:main
for %%C in (*.csv) do call :checker %%C
goto :eof

:checker
call :datalist %1
goto :eof

:datalist
for /f %%D in (chekdata.txt) do find "%%D" < %1 > nul & if errorlevel 1 echo %1 is missing %%D >> missing.txt
goto :eof
:: DONE


If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: AKL-MFCU
Date: November 16, 2005 at 13:59:15 Pacific
Reply:

I'm understanding Dr. Nick's script a little bit more (still trying to figure out exactly how the data needs to be entered since alot of them are matching variables) but on your's Mech, it seems pretty simple enough. Where do i insert the list that it's supposed to look for in the script or should I set it to look into the reader document- an excel sheet that is exported to csv. Let me know!


0

Response Number 5
Name: Mechanix2Go
Date: November 16, 2005 at 22:17:37 Pacific
Reply:

Hi AKL,

The chk4miss.bat checks all the CSV files in the current directory.

It looks for the strings in chekdata.txt which I created for testing. It contains:

acpd001
acrd003
aurs001

So it checks each csv to see if it has those strings. If any are missing it writes to MISSING.TXT as you requested.

I can modify this to take a command line string or to make a cumulative log.

HTH


If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Response Number 6
Name: AKL-MFCU
Date: November 17, 2005 at 13:42:10 Pacific
Reply:

Maybe i'm not setting it up right. For whatever reason it was saying that it could not find any of the files inside that csv, and I even changed it to a txt and made it exactly like the other. Still put out a missing.txt file that had all the items in chekdata.txt still missing. Do you want me to e-mail you the examples so you can look for yourself? Let me know. Thanks.


0

Response Number 7
Name: Mechanix2Go
Date: November 17, 2005 at 13:52:58 Pacific
Reply:

Hi AKL,

Sure send the files. ZIP them up, BUT rename the ZIP to AKL.ABC because I have ZIPs filtered. [because of all the malware]


If at first you don't succeed, you're about average.

M2


0

Response Number 8
Name: Mechanix2Go
Date: November 17, 2005 at 14:27:42 Pacific
Reply:

Hi AKL,

Your query.txt has entries like:

ACPD004C
ACPD004D
ACPD005
ACPD013

and your chekdata.text has:

atrd401
atrd700
aupd255
aurd001
aurd002

Note that one is mostly, or all, uppercase and the other lower.

If you want the bat to find data regardless of case:

:: c4i.bat
@echo off > missing.txt

:main
for %%C in (query.txt) do call :checker %%C
goto :eof

:checker
call :datalist %1
goto :eof

:datalist
for /f %%D in (chekdata.txt) do find /i "%%D" < %1 > nul & if errorlevel 1 echo %1 is missing %%D >> missing.txt
goto :eof
:: DONE

The difference is the find /i which makes it a case-insensitive find.

Now I get 29 missing instaed of 116.


If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: AKL-MFCU
Date: November 17, 2005 at 14:48:23 Pacific
Reply:

Yeah, i didn't even think about looking at the capitalization. It works absolutely wonderful now. Thanks a ton!


0

Response Number 10
Name: Mechanix2Go
Date: November 17, 2005 at 14:52:02 Pacific
Reply:

Hi AKL,

Glad that did it.


If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch to read data and output

Unable to read data from the tran www.computing.net/answers/programming/unable-to-read-data-from-the-tran/14350.html

Batch to read USMT size results www.computing.net/answers/programming/batch-to-read-usmt-size-results/17701.html

batch to read a typed filename and www.computing.net/answers/programming/batch-to-read-a-typed-filename-and-/9196.html