Solved extract rows of a text file

August 16, 2011 at 14:51:28
Specs: Windows XP
I have 2 things:
1. a picture file (ie. picture1.jpg)
2. an associated world file - 6 rows of text (ie. picture1.jgw)

typical world file looks like this:

2.00
0.0
0.0
-2.00
50000
75000

I need to extract hundreds of picture information and the world file information to a single text file with all info for each picture/WF on a single row. The end product will look like this (WF_row=World File Row):
---------------------------------------
picturename, width, height, WF_row1, WF_row2, WF_row3, WF_row4, WF_row5, WF_row6
picture1.jpg, 1200, 800, 2.00, 0.0, 0.0, -2.0, 50000, 75000
picture2.jpg, 1600, 1200, 3.00, 0.0, 0.0, -3.0, 100000, 75000
---------------------------------------

I am having trouble extracting the world file. so far I have the image information but cannot associate the world file with the same line in the text file. this is what I have (I stole and modified other peoples code):

-----------------------------------------------------------------
@echo on
cls
setlocal
set info=%temp%\info.txt
set dims=C:\Temp\dimensions\PictureInformation.txt
set iview=C:\Program Files\IrfanView\i_view32.exe
set filext=*.jpg
for /r %1 %%a in (%filext%) do call :EXTRACT "%%a"
goto :END

:EXTRACT
"%iview%" %1 /info="%info%"

for /f "tokens=4,6" %%a in ('type %info% ^| find.exe /i "Image dimensions"') do (set /a width=%%a) & (set /a height=%%b)
echo %~n1, %~nx1, %width%, %height% >> "%dims%"
goto :EOF

:END
if exist %info% del %info%
endlocal
---------------------------------------------------------------------------

this gives me:
picture1.jpg, 1200, 800
picture2.jpg, 1600, 1200

can someone help me add the world file info to the text file. thanks.


See More: extract rows of a text file

Report •

✔ Best Answer
August 18, 2011 at 10:29:40
Longer than what I'd have liked, but that's how it goes some times.

VBScript:

Const searchPath = "C:\somePath"
Const outPath = "C:\somePath\out.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outPath, 2, True)
Set explorer = CreateObject("Shell.Application")
set folder = explorer.NameSpace(searchPath)

For Each file In folder.Items
  If fso.FileExists(file.Path) And LCase(fso.GetExtensionName(file.Path)) = "jpg" Then
    line = file.Name & "," & GetDimentions(folder, file)
    With GetTextStream(fso, file.Path)
      Do Until .AtEndOfStream
        line = line & "," & .ReadLine
      Loop
    End With
    outFile.WriteLine line
  End If
Next 'file
WScript.Echo "Done"
WScript.Quit

Function GetDimentions(folder, folderItem)
  'Note: Uses undocumented Explorer behavior.
  '      Function can and probably will break eventually.
  'There are better ways to sanitize the string, but I'm lazy.
  ret = folder.GetDetailsOf(folderItem, 31)
  ret = Replace(ret, "x", ",")
  For i = 1 To Len(ret)
    char = Mid(ret, i, 1)
    If (char < "0" Or char > "9") And char <> "," Then _
      ret = Replace(ret, char, " ")
  Next 'i
  GetDimentions = Replace(ret, " ", "")
End Function

Function GetTextStream(fso, picPath)
  path = Left(picPath, InStrRev(picPath, ".")) & "jgw"
  Set GetTextStream = fso.OpenTextFile(path, 1, True)
End Function

How To Ask Questions The Smart Way



#1
August 17, 2011 at 09:32:41
I have a simplified question posted here:
http://www.computing.net/answers/pr...

An answer to the whole thing would be nice, but if I can get this part solved I will be in a much better position than I am in now. Thanks.


Report •

#2
August 18, 2011 at 08:26:25
Are all of these picture files .jpg's?

EDIT: Oh, and while I applicate you making my job easier by linking the duplicate, don't double post like that.

How To Ask Questions The Smart Way


Report •

#3
August 18, 2011 at 08:30:05
They are all jpg's. The part of extracting the jpg info I have down just fine, it is getting the associated test world file (jgw) information that I am having trouble with.

Report •

Related Solutions

#4
August 18, 2011 at 08:54:17
I have now been able to get a single 6 row, 1 column file to come out as a single 1 row 6 columnfile. Now how to I extrapolate this to a 100 files at once? I use a temp file to store and then pull it back. Once again I have stolen the concept from others and applied to my situation.

One thing though, I wasn't able to get the file name into this? I think I could make it work without, but it would be helpful.

------------------------------------
@Echo Off
SetLocal EnableDelayedExpansion


::This outputs a temp file A.tmp creating the delimiter "]"
::based on the row in the text file
Type Nul > A.tmp
Set n=1
For /F "tokens=1" %%a in (InputWorldFile.jgw) Do (
Echo.[1,!n!]%%a>> A.tmp
Set /A n+=1
)

:: Sorts the the temp file
Sort < A.tmp > At.tmp

::outputs in needed format
Type Nul > At.txt
Set row=
Set n=0
For /F "tokens=2 delims=]" %%j in (At.tmp) Do (
Set row=!row! %%j
Set /A n+=1
If !n! equ 6 (
Echo.!row:~1!>> outputfile.txt
Set row=
Set n=0)
)
---------------------------------------------


Report •

#5
August 18, 2011 at 10:29:40
✔ Best Answer
Longer than what I'd have liked, but that's how it goes some times.

VBScript:

Const searchPath = "C:\somePath"
Const outPath = "C:\somePath\out.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outPath, 2, True)
Set explorer = CreateObject("Shell.Application")
set folder = explorer.NameSpace(searchPath)

For Each file In folder.Items
  If fso.FileExists(file.Path) And LCase(fso.GetExtensionName(file.Path)) = "jpg" Then
    line = file.Name & "," & GetDimentions(folder, file)
    With GetTextStream(fso, file.Path)
      Do Until .AtEndOfStream
        line = line & "," & .ReadLine
      Loop
    End With
    outFile.WriteLine line
  End If
Next 'file
WScript.Echo "Done"
WScript.Quit

Function GetDimentions(folder, folderItem)
  'Note: Uses undocumented Explorer behavior.
  '      Function can and probably will break eventually.
  'There are better ways to sanitize the string, but I'm lazy.
  ret = folder.GetDetailsOf(folderItem, 31)
  ret = Replace(ret, "x", ",")
  For i = 1 To Len(ret)
    char = Mid(ret, i, 1)
    If (char < "0" Or char > "9") And char <> "," Then _
      ret = Replace(ret, char, " ")
  Next 'i
  GetDimentions = Replace(ret, " ", "")
End Function

Function GetTextStream(fso, picPath)
  path = Left(picPath, InStrRev(picPath, ".")) & "jgw"
  Set GetTextStream = fso.OpenTextFile(path, 1, True)
End Function

How To Ask Questions The Smart Way


Report •

#6
August 18, 2011 at 13:07:48
I haven't done much VB before. It extracts the text file information perfectly, but didn't seem to bring in the picture dimensions, but I am able to get that information with what I had done previously so this works perfectly for me thanks.

Report •

#7
August 18, 2011 at 13:20:30
didn't seem to bring in the picture dimensions
I wondered about that. It works on my Win7 box, but I don't have access to WinXP here. Thanks for letting me know.

How To Ask Questions The Smart Way


Report •

#8
August 18, 2011 at 13:23:29
Thanks again for your help.

Report •

Ask Question