Can I get individual "unix" lines read by set /P "LN="

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Can I get individual "unix" lines read by set /P "LN="

#1 Post by alan_b » 23 Dec 2011 17:07

This code reads multiple unix lines (no CR=0x0D) from Test2.ini into a single SET /P
but works perfectly if I convert Test2.ini into DOS format (0x0D, 0x0A) as DOS_Test2.ini

Creating a very large DOS "duplicate" of a UNIX original seem clumsy.
Is there a more elegant way please.

This is a representative sample of Test2.ini from many contributors,
almost all are DOS format, but one bit in the middle is UNIX

Code: Select all

DetectFile=%ProgramFiles%\Corel\Corel PaintShop Pro X4\Corel PaintShop Pro.exe
Default=False
FileKey1=%LocalAppData%\Corel PaintShop Pro\14.0\Database|*.db

[Corel VideoStudio Pro X4*]
LangSecRef=3023
DetectFile=%ProgramFiles%\Corel\Corel VideoStudio Pro X4\vstudio.exe
Default=False
FileKey1=%AppData%\Corel\Messages|*.*|RECURSE
FileKey2=%AppData%\Ulead Systems\Corel VideoStudio Pro\14.0\en-US|VS_PRO.log
Excludekey1=FILE|%AppData%\Corel\Messages|*.policy

Detect=HKLM\SOFTWARE\Sunbelt Software\CounterSpy
Default=False
FileKey1=%CommonAppData%\Sunbelt Software\CounterSpy\Logs|*.*


This is my code which extracts into one indexed variable all 7 off UNIX lines commencing [Corel VideoStudio Pro X4*]
but has no problem using the "DOS" variant

Code: Select all

@echo off & setlocal EnableDelayedExpansion & CLS
echo %DATE% %TIME%

SET "file=Test2.ini"

CALL :DO_FILE
MORE %file% > DOS_%file%
SET "file=DOS_%file%"
ECHO(
CALL :DO_FILE
PAUSE
GOTO :EOF

:DO_FILE
ECHO USING %file%
<%file% (
  for /f %%n in ('type %file%^|find /c /v ""') do (
    for /L %%i in (1 1 %%n) do (
      set "LN=" & set /P "LN="
      SET "SS_%%i=!LN!"
      ECHO @ %%i [!SS_%%i!]
    )
  )
)
GOTO :EOF


EDIT - JUST AS FEARED Pasting Unix text into my post resulted in a conversion to DOS.

This shows the result on my DOS Window :-

Code: Select all

23/12/2011 23:10:31.34
USING Test2.ini
@ 1 [DetectFile=%ProgramFiles%\Corel\Corel PaintShop Pro X4\Corel PaintShop Pro.exe]
@ 2 [Default=False]
@ 3 [FileKey1=%LocalAppData%\Corel PaintShop Pro\14.0\Database|*.db]
@ 4 []
@ 5 [[Corel VideoStudio Pro X4*]
LangSecRef=3023
DetectFile=%ProgramFiles%\Corel\Corel VideoStudio Pro X4\vstudio.exe
Default=False
FileKey1=%AppData%\Corel\Messages|*.*|RECURSE
FileKey2=%AppData%\Ulead Systems\Corel VideoStudio Pro\14.0\en-US|VS_PRO.log
Excludekey1=FILE|%AppData%\Corel\Messages|*.policy]
@ 6 []
@ 7 [Detect=HKLM\SOFTWARE\Sunbelt Software\CounterSpy]
@ 8 [Default=False]
@ 9 [FileKey1=%CommonAppData%\Sunbelt Software\CounterSpy\Logs|*.*]
@ 10 []
@ 11 []
@ 12 []
@ 13 []
@ 14 []
@ 15 []

USING DOS_Test2.ini
@ 1 [DetectFile=%ProgramFiles%\Corel\Corel PaintShop Pro X4\Corel PaintShop Pro.exe]
@ 2 [Default=False]
@ 3 [FileKey1=%LocalAppData%\Corel PaintShop Pro\14.0\Database|*.db]
@ 4 []
@ 5 [[Corel VideoStudio Pro X4*]]
@ 6 [LangSecRef=3023]
@ 7 [DetectFile=%ProgramFiles%\Corel\Corel VideoStudio Pro X4\vstudio.exe]
@ 8 [Default=False]
@ 9 [FileKey1=%AppData%\Corel\Messages|*.*|RECURSE]
@ 10 [FileKey2=%AppData%\Ulead Systems\Corel VideoStudio Pro\14.0\en-US|VS_PRO.log]
@ 11 [Excludekey1=FILE|%AppData%\Corel\Messages|*.policy]
@ 12 []
@ 13 [Detect=HKLM\SOFTWARE\Sunbelt Software\CounterSpy]
@ 14 [Default=False]
@ 15 [FileKey1=%CommonAppData%\Sunbelt Software\CounterSpy\Logs|*.*]
Press any key to continue . . .

E:\T\CCleaner\v313\WinApp>


As you can see, variable SS_5 has grabbed seven UNIX lines
and SS_6 through to SS_9 receive the last three DOS lines
and Test2.ini has nothing to put into SS_10 through to SS_15.

The file DOS_Test2.ini gives me what I want, apart from a subsequently redundant duplicate file (perhaps 6000 lines and 200 KB)
a simple fix to the code to get the same result without needing a duplicate would be appreciated.

Regards
Alan

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Can I get individual "unix" lines read by set /P "LN="

#2 Post by aGerman » 24 Dec 2011 08:08

Hi Alan.

Obviously SET /P needs CrLf to separate the lines. I'm virtually certain you can't change that behaviour (except you would develop your own cmd.exe :wink: ). You could return to FOR loops, like ...

Code: Select all

@echo off & setlocal DisableDelayedExpansion
SET "file=Test2.ini"

set /a n=0
for /f "tokens=* delims=1234567890" %%A in ('type "%file%"^|findstr /n "^"') do (
  set /a n+=1
  set "LN=%%A"
  setlocal EnableDelayedExpansion
  for %%i in (!n!) do (
    SET "SS_%%i=!LN:~1!"
    ECHO @ %%i [!SS_%%i!]
  )
  endlocal
)

PAUSE

... but I'm afraid that the "normalization" via MORE and the use of SET /P will be umpteen times faster and safer than those FOR constructs could ever be.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Can I get individual "unix" lines read by set /P "LN="

#3 Post by alan_b » 24 Dec 2011 08:55

Thanks, you confirmed my fears.

Then I choose to be clumsy and fast using SET /P on a temporary file created by MORE.

Regards
Alan

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Can I get individual "unix" lines read by set /P "LN="

#4 Post by Squashman » 24 Dec 2011 14:34

Other option would be to run a UNIX2DOS command on the input file first. I have Win32 versions of several Unix commands on my pc just for situations like this.

I knew about the set /p limitation with LF terminated text files. I have a batch file where I pull the first record from each file into another file to see if the file structure is the same for all the files.

Because the files I work with are several hundred megabytes and sometimes gigabytes in size I end up using a port of the HEAD command instead of making them into crlf text files.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Can I get individual "unix" lines read by set /P "LN="

#5 Post by alan_b » 24 Dec 2011 16:19

Thanks, but I see no benefit.
The only disadvantage of MORE is the redundant %InputFile% after using the command
MORE %InputFile% > %OutputFile%
That is easily fixed by
DEL %InputFile%.
BUT AT LEAST I get to choose to delete %InputFile% after testing and confirming I will never need it again

UNIX2DOS similarly leaves a redundant %InputFile% if I use the option "-destdir"
but has the inconvenience of creating a new folder for the variant.
By omitting the option "-destdir" then the %InputFile% and there are no "take backs".

Regards
Alan

Post Reply