use new line as delimiter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dispro
Posts: 7
Joined: 13 Nov 2014 06:18

use new line as delimiter

#1 Post by dispro » 13 Nov 2014 06:30

Hello,
I have a code that that shows information from other file:

Code: Select all

set /a index_soft=%index_soft%+3
set /a index_key=%index_key%+3
set /a index_versiune=%index_versiune%+3

    set curent_name=
    set curent_Type=
    set curent_version=


for /f "tokens=%index_soft%-%index_versiune% delims=/" %%1 IN (Database_version.txt) DO (
   set Soft=%%1
   set Soft_key=%%2
   set Soft_V=%%3

)
if %Soft%==END goto :end


as you can see i use "/" as a delimiter.
the Database_version.txt looks like these:

Code: Select all

Customization_McoLte_Metrocell/{379CA25D-9F17-4435-B9BD-94B30699FCB0}/2.0.0/Customization_McoLte_Metrocell_ALPID/{52B2140C-D20A-4159-B88D-22263C55D4DF}/2.2.0/System_McoLte_Metrocell/{A4060419-1D13-468B-8E01-AFE508D319D9}/4.2.0/System_McoLte_Metrocell_ALPID/{8D685F81-04E6-49AB-B206-8064D3FADDBC}/3.3.0/Onetool//{BE0CC86F-F9C3-45C7-9343-43D663935464}/1.6.25/Module_MroLte_CubeDock/{C9C1A7D9-ADE3-4771-B9F7-3E6F617EC74F}/4.2.0/Module_MroLte_CubeDock_ALPID/{D4CA152F-6186-4F4B-85A0-6F5587CC506E}/4.0.0/System_MCOLte_Metrocell_User/{8BA0661C-6D08-4506-96C5-8D81DC909E8A}/1.2.0/END/END/END


I would like to use new line delimiter instead of "/" but i dont know how to do it.

Thanks for your help.

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

Re: use new line as delimiter

#2 Post by Squashman » 13 Nov 2014 07:52

That really doesn't make much sense nor will it work if you read the help for the FOR command. It will read the file line by line.

dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#3 Post by dispro » 13 Nov 2014 08:02

The code works fine, but the data in "Database_version.txt" is organized in a single line.
I would like to separate the data in this file to new line for every part (foe aesthetics), but without effecting the final outcome.

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

Re: use new line as delimiter

#4 Post by Squashman » 13 Nov 2014 08:06

What I am saying is that you can't USE a new line character as a delimiter in the FOR command. But if you want to change all the forward slashes to a new line so that the text wraps to a new line then yes that is possible. But please clarify if that is what you want to do.

dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#5 Post by dispro » 13 Nov 2014 08:13

Yes that was exactly what I meant

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: use new line as delimiter

#6 Post by npocmaka_ » 13 Nov 2014 08:30


dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#7 Post by dispro » 13 Nov 2014 08:36

I'm sorry i didn't understand that.

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

Re: use new line as delimiter

#8 Post by Squashman » 13 Nov 2014 08:53

This is a very basic way to do it. This only works because the line length does not exceed the SET commands line length limitation and you also have no spaces in your data. There are more dynamic ways to do this if you have longer lines and spaces in your data.

Code: Select all

echo off

set /p line=<Database_version.txt

set "line=%line:/= %"

FOR %%G in (%line%) do echo %%G

pause


output

Code: Select all

Customization_McoLte_Metrocell
{379CA25D-9F17-4435-B9BD-94B30699FCB0}
2.0.0
Customization_McoLte_Metrocell_ALPID
{52B2140C-D20A-4159-B88D-22263C55D4DF}
2.2.0
System_McoLte_Metrocell
{A4060419-1D13-468B-8E01-AFE508D319D9}
4.2.0
System_McoLte_Metrocell_ALPID
{8D685F81-04E6-49AB-B206-8064D3FADDBC}
3.3.0
Onetool
{BE0CC86F-F9C3-45C7-9343-43D663935464}
1.6.25
Module_MroLte_CubeDock
{C9C1A7D9-ADE3-4771-B9F7-3E6F617EC74F}
4.2.0
Module_MroLte_CubeDock_ALPID
{D4CA152F-6186-4F4B-85A0-6F5587CC506E}
4.0.0
System_MCOLte_Metrocell_User
{8BA0661C-6D08-4506-96C5-8D81DC909E8A}
1.2.0
END
END
END

dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#9 Post by dispro » 14 Nov 2014 00:55

Where should I implement the code in my program?

dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#10 Post by dispro » 14 Nov 2014 01:54

Let me clarify my question:

I have *.txt file that looks like these:
111/222/333/444/555

I have a code that shows the data from these file like these:
111
222
333
444
555

Here is the full code:

Code: Select all

@echo off

REM ---------------------------------------------------------------------------
REM Cream fisierul unde vom scrie raportul, si adaugam data actuala a computerului
REM ---------------------------------------------------------------------------

echo >>Check_Result\raport_%COMPUTERNAME%.txt
echo ----------------------------------------->> Check_Result\raport_%COMPUTERNAME%.txt
echo %date% %time%>> Check_Result\raport_%COMPUTERNAME%.txt
echo ----------------------------------------->> Check_Result\raport_%COMPUTERNAME%.txt


REM -------------------------------Se citeste baza de date(pe rand fiecare soft)--------------------------------------------

set index_soft=-2
set index_key=-1
set index_versiune=0

:nextsoft

set /a index_soft=%index_soft%+3
set /a index_key=%index_key%+3
set /a index_versiune=%index_versiune%+3

    set curent_name=
    set curent_Type=
    set curent_version=


for /f "tokens=%index_soft%-%index_versiune% delims=/" %%1 IN (Database_version.txt) DO (
   set Soft=%%1
   set Soft_key=%%2
   set Soft_V=%%3

)
if %Soft%==END goto :end

echo -/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
   echo Soft %Soft%
   echo Key %Soft_key%
   echo Versiune %Soft_v%
echo -/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/


REM ------------------------------------Se merge la adresa registrilor(pe rand fiecare soft)----------------------------------

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\%Soft_key%"
set VALUE_NAME=DisplayVersion

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set current_name=%%A
    set current_Type=%%B
    set current_version=%%C
)


if not defined current_version (
   goto :endcheck
)
   IF %current_version% == %Soft_v% (
            echo Softul %Soft% versiunea %Soft_v% este ok
            echo Softul %Soft% versiunea %Soft_v% este ok >> Check_Result\raport_%COMPUTERNAME%.txt
            goto :endcheck
               )
   
   ELSE (
            echo Softul %Soft% trebuie actualizat de la %current_version% la %Soft_v%
            echo Softul %Soft% trebuie actualizat de la %current_version% la %Soft_v%  >> Check_Result\raport_%COMPUTERNAME%.txt
         )

)

:endcheck
goto :nextsoft


:end
pause


The problem is that i would like that my *.txt file will look like these (so it would be easier to read):
111
222
333
444
555


What changes i should do un my code so it will be able to read the *.txt file and show the data in the same way?

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: use new line as delimiter

#11 Post by foxidrive » 14 Nov 2014 05:41

dispro wrote:Let me clarify my question:

I have *.txt file that looks like these:
111/222/333/444/555

I have a code that shows the data from these file like these:
111
222
333
444
555


That is what Squashman's example showed with your database data, isn't it?
What was wrong with that solution?

dispro
Posts: 7
Joined: 13 Nov 2014 06:18

Re: use new line as delimiter

#12 Post by dispro » 14 Nov 2014 06:10

Squashman's example shows the database as i want.
My problem is that whet I arrange my database like these:
111
222
333
444
555

my code don't work!
It only works when i arrange the database like these:
111/222/333/444/555

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

Re: use new line as delimiter

#13 Post by Squashman » 14 Nov 2014 07:35

Now you are just contradicting yourself. You can't have two different types of input and expect to have the same output.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: use new line as delimiter

#14 Post by Aacini » 14 Nov 2014 11:25

dispro wrote:Let me clarify my question:

I have *.txt file that looks like these:
111/222/333/444/555

[unuseful stuff removed from this place...]

The problem is that i would like that my *.txt file will look like these (so it would be easier to read):
111
222
333
444
555


What changes i should do un my code so it will be able to read the *.txt file and show the data in the same way?

Thanks



Delete all the "index_..." variables stuff from your code, change this line:

Code: Select all

for /f "tokens=%index_soft%-%index_versiune% delims=/" %%1 IN (Database_version.txt) DO (

... by this one:

Code: Select all

for /f "tokens=1-3 delims=/" %%1 IN (Database_version.txt) DO (

... and move ALL THE CODE delimited by ":nextsoft" and "goto :nextsoft" inside previous FOR loop, that is:

Code: Select all

for /f "tokens=1-3 delims=/" %%1 IN (Database_version.txt) DO (
   set Soft=%%1
   set Soft_key=%%2
   set Soft_V=%%3

   MOVE ALL THE CODE HERE

)

However, you must also replace all %variable% inside the FOR loop by !variable! and insert "setlocal EnableDelayedExpansion" command at beggining...

Another easier solution is to move all that code into a subroutine and then use "call :theSubroutineName" instead of "MOVE ALL THE CODE HERE" above.

Ah! And also change your Database_version.txt file from this:

Code: Select all

Customization_McoLte_Metrocell/{379CA25D-9F17-4435-B9BD-94B30699FCB0}/2.0.0/Customization_McoLte_Metrocell_ALPID/{52B2140C-D20A-4159-B88D-22263C55D4DF}/2.2.0/System_McoLte_Metrocell/{A4060419-1D13-468B-8E01-AFE508D319D9}/4.2.0/System_McoLte_Metrocell_ALPID/{8D685F81-04E6-49AB-B206-8064D3FADDBC}/3.3.0/Onetool//{BE0CC86F-F9C3-45C7-9343-43D663935464}/1.6.25/Module_MroLte_CubeDock/{C9C1A7D9-ADE3-4771-B9F7-3E6F617EC74F}/4.2.0/Module_MroLte_CubeDock_ALPID/{D4CA152F-6186-4F4B-85A0-6F5587CC506E}/4.0.0/System_MCOLte_Metrocell_User/{8BA0661C-6D08-4506-96C5-8D81DC909E8A}/1.2.0/END/END/END

... to this:

Code: Select all

Customization_McoLte_Metrocell/{379CA25D-9F17-4435-B9BD-94B30699FCB0}/2.0.0
Customization_McoLte_Metrocell_ALPID/{52B2140C-D20A-4159-B88D-22263C55D4DF}/2.2.0
System_McoLte_Metrocell/{A4060419-1D13-468B-8E01-AFE508D319D9}/4.2.0
System_McoLte_Metrocell_ALPID/{8D685F81-04E6-49AB-B206-8064D3FADDBC}/3.3.0
Onetool//{BE0CC86F-F9C3-45C7-9343-43D663935464}/1.6.25
Module_MroLte_CubeDock/{C9C1A7D9-ADE3-4771-B9F7-3E6F617EC74F}/4.2.0
Module_MroLte_CubeDock_ALPID/{D4CA152F-6186-4F4B-85A0-6F5587CC506E}/4.0.0
System_MCOLte_Metrocell_User/{8BA0661C-6D08-4506-96C5-8D81DC909E8A}/1.2.0


Antonio

Post Reply