Computing.Net > Forums > Programming > VBScript help parsing a text file

VBScript help parsing a text file

Reply to Message Icon

Original Message
Name: humerick
Date: May 15, 2006 at 10:34:05 Pacific
Subject: VBScript help parsing a text file
OS: WinXP
CPU/Ram: Pentium
Model/Manufacturer: Dell
Comment:

We are switching over from our Unix version of software to a windows version. I only know korn shell scripting, and now need to learn VBScript. Can someone help me get started with rewriting a shell script in VBScript? What I need to do is loop through a directory looking for all files that begin with FPACTAPE.C*. I need to check and see if the first char of the first line is a "1", and the first character of the second line is a "9". If they meet that criteria, then I skip the file. Otherwise I need to concatenate each file into a file called CAAPACTP.DAT. Any help with getting me started on this will be greatly appreciated.

Thanks,
Scott



Report Offensive Message For Removal

Response Number 1
Name: Michael J (by mjdamato)
Date: May 15, 2006 at 14:56:50 Pacific
Subject: VBScript help parsing a text file
Reply: (edit)

It would probably be easier for me to write it for you than to walk you though it, but then you wouldn't learn anything!

Start by reading through this page: http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/vbscriptpr_0201.html

There is an example towards the bottom (source code provided) which searches through an entire drive for batch files. The program has a function that recursively calls itself in order to go into sub directories. If you are only needing to look in one directory (and not subdirectories) you will not need to do that.

but the example should be enough for you to accomplish the first part of your problem of looping through files in a directory and finding the ones that begin with "FPACTAPE.C*"

So start with that and post back if you have problem or question. Once that is done we can move on to reading the contents of the files.

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: May 16, 2006 at 08:21:25 Pacific
Subject: VBScript help parsing a text file
Reply: (edit)

Hi Scott,

I don't envy you having to give up ksh and learn something else; but there we are.

Like Michael J., it's easier to do it than teach it.

VBS and perl both have more horsepower than BATCH scripts.

For now, to get you going, you can try the BAT below. You will need to modify the drive/directory/filespec in () in the first 'for' line to suit your files to check.

:: == cat-19.bat
:: concat all matching files UNLESS:
:: 1st char of 1st line is 1 AND
:: 1st char of 2nd line is 9

@echo off > CAAPACTP.DAT

:main
setlocal
for /f "tokens=* delims=" %%F in ('dir /b/s/a-d c:\temp\??.bat') do (
call :sub1 %%F)
endlocal
goto :eof

:sub1
set currFILE=%*
set lineNUM=0
for /f %%L in (%1) do call :sub2 %%L
if %first%%second% neq 19 type %currFILE% >> CAAPACTP.DAT
goto :eof

:sub2
set currLINE=%*
set /a lineNUM+=1
if %lineNUM% gtr 2 goto :eof
if %lineNUM% equ 1 set first=%currLINE:~0,1%
if %lineNUM% equ 2 set second=%currLINE:~0,1%
goto :eof
:: DONE



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

M2



Report Offensive Follow Up For Removal

Response Number 3
Name: humerick
Date: May 17, 2006 at 13:12:31 Pacific
Subject: VBScript help parsing a text file
Reply: (edit)

Thanks Michael J and M2. I hope to look into your solutions tomorrow. I've been working on some other issues here at work. I'll get back to you if I have any questions.
Thanks again,
Scott


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: VBScript help parsing a text file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software