Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

How to merge two text files?

- - - - -

  • Please log in to reply
237 replies to this topic

#176
willsmannar

willsmannar
  • Member
  • 1 posts
  • OS:Vista Ultimate x86
  • Country: Country Flag

There are two files:

1.txt

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568

2.txt
[SourceFileInfo]
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

If I do "copy 1.txt+2.txt 3.txt" I get this:
[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
[SourceFileInfo]
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

but I would like to get something similar to this:
[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

Is it possible?



In C# you can do it programmatically . Read those file one by one and store it ino a string and finally write the string to a single file.

string line = null;
System.IO.TextReader readFile = new StreamReader("filename.txt");
line = line + readFile.ReadToEnd();

read three files

and write file

write file

System.IO.TextWriter writeFile = new StreamWriter("filename.txt");
writeFile.WriteLine("line");

check this link.

http://csharp.net-in...-textwriter.htm

wills.


How to remove advertisement from MSFN

#177
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag

Using COPY /B serves no purpose when used in that manner, in fact it is the default mode when copying single files.

Something is different between 2K and XP in this matter.

When I do:

COPY/B NUL 1.txt
an empty file gets created when using cmd.exe from Windowx XP but when doing the same with Windows 2000's cmd.exe:

The parameter is incorrect
and no file is created.

In Windows 2000 it's necessary to use:

COPY NUL 1.txt
to create an empty file while in XP the switch doesn't matter.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#178
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag
For the record:
http://www.robvander...com/useless.php

Create Empty Files

to create an empty (zero bytes) file I always used:
TYPE NUL > new_empty_file_name

Robert Van Etta showed me an even shorter command:
CD.> new_empty_file_name


jaclaz

#179
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
Thanks :)

The short one

CD.> new_empty_file_name
is really nice.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#180
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
I'm trying to divide and then relink registry entries like this one...

1.txt
HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink",,0x10000, "MSGOTHIC.TTC,MS UI Gothic"
into

HKLM
"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink"

0x10000
"MSGOTHIC.TTC,MS UI Gothic"

I've come up with this script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set tokens=
CD.>temp.txt
:TEST
SET/A tokens+=1
FOR /F "delims=" %%A IN (1.txt) DO (
	FOR /F "tokens=%tokens% delims=," %%B IN ("%%A") DO (
		FOR /F "tokens=1* delims= " %%C IN ("a %%B") DO (
			SET STRING=
			ECHO %%D|FINDSTR/V ^"^"^" >NUL && SET STRING=%%D
			IF NOT DEFINED STRING ECHO %%D|FINDSTR/B ^"^"^"|FINDSTR/E ^"^"^" >NUL && SET STRING=%%D
			IF NOT DEFINED STRING ECHO %%D|FINDSTR/B ^"^"^" >NUL && SET STRING1=%%D
			IF NOT DEFINED STRING ECHO %%D|FINDSTR/E ^"^"^" >NUL && SET STRING2=%%D
			IF DEFINED STRING (
				ECHO>>temp.txt !STRING!
			) ELSE (
				IF DEFINED STRING1 IF DEFINED STRING2 (
					ECHO>>temp.txt !STRING1!,!STRING2!
					SET STRING1=
					SET STRING2=
				)
			)
			GOTO :TEST
		)
	)
	FOR /F "delims=" %%A IN (temp.txt) DO <NUL SET/P="%%A,">>2.txt
)

The result is:

2.txt
HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink",0x10000,"MSGOTHIC.TTC,MS UI Gothic",

I know it's not pretty and I need to work on it more but the biggest problem here is that the FOR /F loop:

FOR /F "tokens=%tokens% delims=," %%B IN ("%%A") DO (
done on

HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink",,0x10000, "MSGOTHIC.TTC,MS UI Gothic"
makes the empty token before "0x10000" disappear. I've done some Googling but couldn't really find any simple solution to this default behaviour of FOR /F. Is there simple way to preserve it other than using gsar before running the script to replace ",," with something else like ","","? :rolleyes:

Edited by tomasz86, 25 March 2013 - 06:05 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#181
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag

I've done some Googling but couldn't really find any simple solution to this default behaviour of FOR /F. Is there simple way to preserve it other than using gsar before running the script to replace ",," with something else like ","","? :rolleyes:

No :(, "delims" interprets consecutive separators as a single one.
There is more than one way to skin a cat (the cat won't be happy anyway about it ;)), but the "real" issue as I see it is the comma inside the last field. :unsure:
You dont' need to use gsar for doing something *like*:
FOR /F "tokens=*" %%A IN ('TYPE 1.txt') DO SET Line=%%A
SET Line=%Line:,,=,"",%
and then go on with processing "Line" instead of "1.txt", but the script you posted (at first sight) seem to me a rather complex one.
If I get it right the idea should be to "ignore" the comma when it is inside a double quoted field....
I'll see which trick I can find to get that result in a simpler way (if I can find one) :blink: .

jaclaz

#182
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
That's right. "Normally" it would be easy to just divide everything by commas but these kind of fields:

"MSGOTHIC.TTC,MS UI Gothic"
are problematic. As for the script you suggested, isn't it going to be the same situation as there? That's why I'm thinking of gsar because the script just has to work in Windows 2000 with its default cmd.exe.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#183
Yzöwl

Yzöwl

    Wise Owl

  • Member
  • PipPipPipPipPipPipPipPipPip
  • 4,531 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Here is an idea you may be able to use to help you.
@ECHO OFF

SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

FOR /F "TOKENS=*" %%# IN (1.TXT) DO CALL :SUB %%#

IF DEFINED _OUT (

	>TEMP.TXT (

		ECHO/%FIRST%

		ECHO/%SECOND%

		ECHO/%THIRD%

		ECHO/%FOURTH%

		ECHO/%FIFTH%

   )

)

GOTO :EOF



:SUB

	SET LINE=%*

	SET _FIRST=%LINE:*,=%

	CALL :OUT %%LINE:%_FIRST%=%%

	SET FIRST=%_OUT:~,-1%

	SET _SECOND=%_FIRST:*,=%

	CALL :OUT %%_FIRST:%_SECOND%=%%

	SET SECOND=%_OUT:~,-1%

	SET _THIRD=%_SECOND:*,=%

	CALL :OUT %%_SECOND:%_THIRD%=%%

	SET THIRD=%_OUT:~,-1%

	SET _FOURTH=%_THIRD:*,=%

	CALL :OUT %%_THIRD:%_FOURTH%=%%

	SET FOURTH=%_OUT:~,-1%

	CALL :OUT %%LINE:*%FOURTH%,=%%

	SET FIFTH=%_OUT%

	GOTO :EOF



	:OUT

		SET _OUT=%*
I would be interested in your reasons for deconstructing .inf files line by line before reconstructing them.

#184
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
I want to remove duplicates, ex.

HKCR,"TypeLib\{662901fc-6951-4854-9eb2-d9a2570f2b2e}\5.1","",0x00000002,"Microsoft WinHTTP Services, version 5.1"
HKCR,TypeLib\{662901fc-6951-4854-9eb2-d9a2570f2b2e}\5.1,"",0x00000002,"Microsoft WinHTTP Services, version 5.1"
HKCR,TypeLib\{662901fc-6951-4854-9eb2-d9a2570f2b2e}\5.1,,0x00000002,"Microsoft WinHTTP Services, version 5.1"
HKCR,TypeLib\{662901fc-6951-4854-9eb2-d9a2570f2b2e}\5.1,,0x00000002,             "Microsoft WinHTTP Services, version 5.1"

etc.
so I'd like to deconstruct and then reconstruct such lines so that they are exactly the same and can be deduped by the yanklines script.

Edited by tomasz86, 26 March 2013 - 05:27 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#185
Yzöwl

Yzöwl

    Wise Owl

  • Member
  • PipPipPipPipPipPipPipPipPip
  • 4,531 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Would that be done manually?

HKCR,	TypeLib\{662901fc-6951-4854-9eb2-d9a2570f2b2e}\5.1,	,	0x2,	"Microsoft WinHTTP Services, version 5.1"

How do you deal with string variables since they use percent characters, do you have to convert them all to another character, then convert them back again later. What happens if a string variable is used from the strings section, and another key contains the same data but without implementing the string variable. How does a non manual method know that?

#186
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
All of the merging is done 100% automatically. As for strings, they have been replaced with their original variables using this script:

FOR /F "skip=1 delims=" %%B IN ([Strings].inf) DO (
	FOR /F tokens^=1-2^ delims^=^=^" %%C IN ("%%B") DO (
		FOR /F "delims=" %%E IN ('FINDSTR/ILM "%%%%C%%" "*.inf"') DO (
			TOOLS\gsar.exe -i -o -s"%%%%C%%" -r"%%D" "%%E" >NUL
		)
	)
)
so there are no string variables present at this point.

[Strings].inf uses this format:

[Strings]
LangTypeValue=9
WSEDIR="1033"
TSCLIENTDIR="Terminal Services Client"

etc.


Edit: @Yzöwl I'm not sure what you're exactly asking about above... Do you mean the spaces? I was thinking about lines like the one below which Microsoft somehow managed to produce.

Taken from Windows 2000 SP4's update.inf:
HKLM, "SYSTEM\CurrentControlSet\Services\RASMAN\PPP\EAP\25",                    Path,                   0x00020000, "%%SystemRoot%%\System32\rastls.dll"

Edited by tomasz86, 26 March 2013 - 08:21 PM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#187
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
I've got a different problem now...

Basically speaking I'm searching for something like this:

FINDSTR/IR "sp.qfe" 1.inf
and the output is:

?"%sourcepath%\\SP2QFE\\bitsinst.exe /setupservice /resourcedll:%windir%\\system32\\xpob2res.dll"
I need to know that the string found is specifically "SP2QFE" so that I can use gsar to remove it like this:

gsar -o -s"SP2QFE\\" -r 1.inf
It's not possible to get such information directly from FINDSTR, or is it?

Edited by tomasz86, 30 March 2013 - 07:13 PM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#188
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag
Pipe it into FIND.

FINDSTR/IR "sp.qfe" 1.inf | FIND /i "SP2QFE"

jaclaz

#189
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
The problem is that I don't know that it's "SP2QFE" before running the script.

I'm also struggling with another problem :}

1.inf
;abc, def
When

FOR /F "tokens=1,2 delims=, " %%a in (1.inf) do echo %%a,%%b
the line is ignored as it starts with

;
and the default EOL is set to it.

I want to disable EOL but also set delims to both comma and space at the same time. Is doing it this way

FOR /F tokens^=1-2^ eol^=^

delims^=^,^  %%a in (1.inf) do echo %%a,%%b
the only possible solution? I know about the other method of setting EOL to an uncommon character but I'd like to avoid it.

The desired output should be:

;abc,def

Edited by tomasz86, 31 March 2013 - 02:40 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#190
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts

1.inf

;abc, def
When

FOR /F "tokens=1,2 delims=, " %%a in (1.inf) do echo %%a,%%b
the line is ignored as it starts with

;
and the default EOL is set to it.

I want to disable EOL but also set delims to both comma and space at the same time. Is doing it this way

FOR /F tokens^=1-2^ eol^=^

delims^=^,^  %%a in (1.inf) do echo %%a,%%b
the only possible solution? I know about the other method of setting EOL to an uncommon character but I'd like to avoid it.

The desired output should be:

;abc,def

This should work:
FOR /F "tokens=1,2 delims=, eol=" %%a in (1.inf) do echo %%a,%%b

Edited by allen2, 31 March 2013 - 03:55 AM.


#191
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag

This should work:

FOR /F "tokens=1,2 delims=, eol=" %%a in (1.inf) do echo %%a,%%b

I'd like to use both comma and space as delims. In your example it's only the former :( And

"eol="
actually sets EOL to double quotes so lines like this

"abc, def
will be ignored.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#192
Yzöwl

Yzöwl

    Wise Owl

  • Member
  • PipPipPipPipPipPipPipPipPip
  • 4,531 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Edit: @Yzöwl I'm not sure what you're exactly asking about above... Do you mean the spaces?

No not just spaces. Tabs are often used too, but the main thing was the hex string you used.

Your automatic routine would need to know how, for instance, the following relate when checking for duplicates
  • 0x00000000
  • 0x0
  • 0
  • ,,
  • ,<one or more spaces>,
  • ,<one or more tabs>,
  • ,<one or more spaces with one or more tabs>,

As for your query about findstr…

I've got a different problem now...

Basically speaking I'm searching for something like this:

FINDSTR/IR "sp.qfe" 1.inf
and the output is:

?"%sourcepath%\\SP2QFE\\bitsinst.exe /setupservice /resourcedll:%windir%\\system32\\xpob2res.dll"
I need to know that the string found is specifically "SP2QFE" so that I can use gsar to remove it like this:

gsar -o -s"SP2QFE\\" -r 1.inf
It's not possible to get such information directly from FINDSTR, or is it?

It's not findstr that's causing you the problem, its thinking through what your goal is before structuring your code.
you are not looking for sp.qfe, you are looking for example for sp1qfe\\, sp2qfe\\, or sp3qfe\\.
anyhow regardless of that, here is an example script using your search and findstr strings:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
(SET VAR=)
FOR /F "TOKENS=*" %%# IN ('FINDSTR/I \\SP.QFE\\ 1.INF') DO SET VAR=%%#
IF NOT DEFINED VAR GOTO :EOF
CALL :SUB
ECHO/gsar -o -s"%VAR%" -r 1.inf
PAUSE
GOTO :EOF
:SUB
SET VAR=%VAR:*\\SP=%
SET VAR=SP%VAR:~,6%
GOTO :EOF
Remove the ECHO on line seven to try it when happy with the output!

Edited by Yzöwl, 31 March 2013 - 12:33 PM.
example script added


#193
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag
Try with a "plain":

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

jaclaz

#194
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
@Yzöwl Thank you for the script. I've done some tests and it does work for "\\SP.QFE\\" but I think I need to take a different approach. The script will just check and save a list of SP*QFE folders which exist and than basing on their actual names from the list replace the strings. It's much easier for me to write and should be more efficient since there will be no need to check for non-existing folders.

No not just spaces. Tabs are often used too, but the main thing was the hex string you used.

Your automatic routine would need to know how, for instance, the following relate when checking for duplicates

  • 0x00000000
  • 0x0
  • 0
  • ,,
  • ,<one or more spaces>,
  • ,<one or more tabs>,
  • ,<one or more spaces with one or more tabs>,

I know about it :) I'll have to add all those exceptions. At the moment it was just a basic structure.


@jaclaz Thanks. It works fine. Are there any precautions when using TYPE like that?
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#195
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag

@jaclaz Thanks. It works fine. Are there any precautions when using TYPE like that?

In what sense precautions? :unsure:
The file TYPEd must be "plain text", that's all.

jaclaz

#196
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
I was thinking about files which contain some language specific characters, etc.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#197
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag

I was thinking about files which contain some language specific characters, etc.

You mean UNICODE? :unsure:

jaclaz

#198
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag
Never mind :P I'll let you know if I encounter any problems.

By the way, is

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b
any different from

FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b
?
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#199
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,205 posts
  • OS:none specified
  • Country: Country Flag

Never mind :P I'll let you know if I encounter any problems.

By the way, is

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b
any different from

FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b
?

Yes it is, one uses TYPE, the other uses FINDSTR :whistle: ;)

More seriously, try them on a UNICODE .inf and check the differences.
See here:
http://www.robvanderwoude.com/type.php
Convert Unicode to ASCII vv

TYPE is UNICODE compliant (and the result will normally be "plain text"), FINDSTR is not.

jaclaz

#200
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • OS:XP Pro x86
  • Country: Country Flag

Try with a "plain":

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

I was wrong. It still doesn't ECHO lines starting with ";" when EOL isn't defined :(

I'll probably use:

  • FOR /F delims^=^ eol^= %%A IN (1.inf) DO ECHO %%A
    when I want to display full lines and
  • FOR /F tokens^=1-2^ eol^=^
    
    delims^=^,^  %%A IN (1.inf) DO ECHO %%A,%%B
    when I need to specify delims, one of which is space.

Edited by tomasz86, 02 April 2013 - 03:22 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN