1.

Solve : Opening files from list of files?

Answer»

Gudday all
I wish to 1st create a list of known file names.
From that list, saved as file, open each file one by one to look at tokens 1 and 2 on the first line only for various patterns
I can create my file list ok
Code: [Select]dir D:\Archives\E20_120_AU\ArchiveCreator\Errors\*.spl /b > SPLfiles.txt
but I am having trouble opening each file in that list and doing the parsing.
I tried
Code: [Select]for /f "tokens=1,2 delims=" %%a in (SPLfiles.txt) do (
set full_line=\%%a\%%b
set file_name=%%b
echo Full line: !full_line!
echo File name: !file_name!
echo.
but that fails as it looks at the file names not the file contents.

Can anyone help me please?if you can use gawk for windows (see my sig), this would easy enough
Code: [Select]C:\test>gawk "FNR==1{print $1,$2}" *.spl
or, you can use vbscript
Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
If objFS.GetExtensionName(strFile) = "spl" Then
Set objFile = objFS.OpenTextFile(strFile)
strFirstLine = objFile.ReadLine
s = Split(strFirstLine," ")
objFile.Close
WScript.Echo "first token: "& s(0)
WScript.Echo "first token: "& s(1)
END If
Next
output
Code: [Select]C:\test>more file.spl
token1 token2 blah
word1 word2 word3

C:\test>cscript /nologo test.vbs
first token: token1
first token: token2
I have something similar to this written on my laptop, which I did not bring with me to work today. However when I get home I will post it for you to see if it helps.

My batch reads in a file (it's only 1 token but could easily changed to however many you need) and then runs some command.Just another way of doing this.

Code: [Select]# Script tokens12.txt
var str filelist, file, contents, line1, word1, word2
lf -r -n "*.spl" "D:/Archives/E20_120_AU/ArchiveCreator/Errors" > $filelist
while ($filelist <> "")
do
lex "1" $filelist > $file ; cat $file > $contents ; lex "1" $contents > $line1
wex -p "1" $line1 > $word1 ; wex -p "2" $line1 > $word2
echo $file "\t" $word1 "\t" $word2
done
Script is in biterscripting. Save the script in file C:/Scripts/tokens12.txt, start biterscripting, run the script by entering the following command

Code: [Select]script tokens12.txt

The documentation of the lex (line extractor) and wex (word/token extractor) commands is at http://www.biterscripting.com/helppages_editors.html .

Did a little more experimentation.
I can now get my script to grab the 1st 2 tokens off every line in every .spl file.
What I really want to do is just get the 1st 2 tokens off the 1st line of every file.

Code: [Select]@echo off

setlocal enabledelayedexpansion

dir D:\Archives\E20_120_AU\ArchiveCreator\Errors\*.spl /b > SPLfiles.txt

for /f %%a in (SPLfiles.txt) do (
echo The file is %%a
set file=%%a
for /f "tokens=1,2 delims= " %%i in (%%a) do (
echo variable i = %%i
echo variable j = %%j
rem goto :finishgettingline
)
rem :finishgettingline
echo left loop after 1st line of the file %file%
)

I thought that if I put a goto to leave the inner loop (getting the tokens) after the 1st line was parsed then it would PARSE the 1st line of every file. Instead it just parses the 1st line of the 1st file then stops. The output is

Quote

The file is 20090619153310.SPL
variable i = SEW_AU
variable j = DEL_NOTE
left loop after 1st line of the file 20090619153310.SPL

No other files are parsed. I have 'remed' out the seemingly offending lines in the code above.

Also is it possible to pass a variable, in this case %%a, from the inner loop to the outer loop? I wanted the progarm to tell me when it finished each file. The line ' left loop after 1st line of the file' is printed but not the current file name.

You can use set /p to get the first line of a file.

SET /P variable=
It works because by default SET with the /p switch waits for a CR terminated string from STDIN (the keyboard) however you can use the < redirection symbol to make it take its input from a file. The crlf at the end of the first line of a text file ensures that the command terminates.

so this should work...

Code: [Select]@echo off

setlocal enabledelayedexpansion

dir D:\Archives\E20_120_AU\ArchiveCreator\Errors\*.spl /b > SPLfiles.txt

for /f %%a in (SPLfiles.txt) do (
set file=%%a
echo the file is !file!
set /p firstline=<"%%a"
for /f "tokens=1,2 delims= " %%i in ("!firstline!") do (
echo variable i = %%i
echo variable j = %%j
)
)

notes...

(1) labels, GOTO etc

In many ways, NT command script language is a kludge, and it doesn't always work the way you would expect if you are used to "proper" programming or scripting languages and tools. One thing that is peculiar is the way it processes loops and other parenthetical structures. A loop is processed as if it was one logical line. Thus this

for [whatever] do (
some stuff
)

is equivalent logically to

for [whatever] do (some stuff)

(In fact if you only have one command in the loop you can use that format.)

Now you cannot have a label in the middle of a line, it has to be on a line by itself, so labels in loops break the code. Since your outer and inner loop is processed as one HUGE line, that is why it halted. Jumping out of loops is very tricky.

While I am on the topic, you may come across example scripts where the writer has used a double (or SINGLE) colon instead of REM to start a comment line

:: This is a comment

This is a carryover from MS-DOS where the practice did no harm (I think). It works because labels are disregarded (mostly!) and the line is a (broken) label. However such a line inside parentheses BREAKS THE CODE. It is an undocumented and unsupported "feature". The behaviour it CAUSES can give much puzzlement to coders, however it is often vociferously defended by people who ought to know better. My advice is, don't do it!


(2) Considering the FOR command,

for /f %%V in (dataset) do

With the /f switch, dataset (unquoted) is processed as a filename, whereas "dataset" is a string, which is why I used ("!firstline!") above.

(3)
In the other thread you asked about reference material for NT command scripting.

One good site I have found is

http://ss64.com/nt/

You may have the compiled help file ntcmds.chm on your system; it is not included on Vista & 7 I believe.

type

hh ntcmds.chm

at the prompt to find out.

If you don't have it, you can get if off an XP disk or installation, or download it from Microsoft (it is included in the Server 2003 Adminpak)

http://support.microsoft.com/kb/304718





Dear ST
Thank you once again.
After some fiddling the code works at last!
At least the boss is happy.


Discussion

No Comment Found