Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
congnt92

[Need help] - Merge text in 2 text files to another file.

Recommended Posts

Hi all,

EDIT: To avoid missmatch between Title and topic content, also to avoid a long post, I repost full my problem in post #9.

I have 2 small .cmd file. Let say test1.cmd and test2.cmd

test1.cmd creates text1.txt

H: Removable Disk 4.0 GB
I: Removable Disk 7.9 GB

 test2.cmd creates text2.cmd
 

USB1
USB2

My goal is merger text in text1.txt with texts in text2.txt to new file (text3.txt)

H: Removable Disk 4.0 GB USB1
I: Removable Disk 7.9 GB USB2

 

Here my way to do that but it repeat output many times
 

@echo off
for /f "tokens=*" %%a in ('type "text1.txt"') do (
for /f "tokens=*" %%b in ('type "text2.txt"') do echo %%a %%b)
pause

Can you help me correct my code? Tks :)

Edited by congnt92

Share this post


Link to post
Share on other sites

You may want to ask the actual question and not a small part of it.

(full disclosure, maybe I know what the actual question is, because we talked about it via PM).

Remember, you want to reach a goal, express it, DO NOT divide the way you believe you can reach it in n little steps each disjointed from the previous one, there are several reasons for this, among them the fact that there may be "better" or "easier" ways to reach your goal, people doesn't know WHAT you are actually wanting to do, you may make an example for what you believe is the right approach which is either inaccurate or more simply "wrong" , so as soon as an answer will be given to you, you will come up with another question (because the answer is not suitable to your unknown goal) and then another question, and another question :ph34r:.

You have a test1.cmd that creates text1.txt and a test2.cmd that creates text2.txt, yet, your code deals with two other files outputfile.txt and volname.txt, and there is no trace of text3.txt.

So I have to assume that text1.txt is actually  outputfile.txt and that text2.txt is volname.txt.

I can provide you a perfectly valid answer to your question but that won't help you a little bit in getting near your GOAL, (this answers your question and example):

@ECHO OFF
SETLOCAL
SET skip=
SET /A Counter=0
FOR /F %%B IN ('TYPE test2.txt') DO CALL :merge %%B
GOTO :EOF

:merge
FOR /F "%skip% tokens=*" %%A IN ('TYPE test1.txt') DO (
ECHO %%A %1&GOTO :out
)
:out
SET /A Counter+=1
SET skip=skip=%Counter%
GOTO :EOF

jaclaz
 

  • Upvote 1

Share this post


Link to post
Share on other sites

Written ad-hoc in Powershell 2.0 (should work on any newer version, which means every Windows system from XP up to today)
I'm not a Powershell professional, so it might not be written with respect to all best practices, but it surely works.
Remember to set your current working directory with cd or set-location before using.

$1 = Get-Content text1.txt
$2 = Get-Content text2.txt
foreach($line in $(0..($text1.length - 1))){
$1[$line] + " " + $2[$line] >> text3.txt} 

EDIT: Getting curious what the real goal is...

  • Upvote 1

Share this post


Link to post
Share on other sites

Here is the VBS way of doing it with checks

'-> Runtime Varibles
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Runtime Object
Dim C1, C2, Tx1, Tx2,Ts
'-> Check For Text File One
 If Fso.FileExists("Test1.txt") Then
  C1 = True 
  Set Ts =  Fso.OpenTextFile("Test1.txt")
   Tx1 = Ts.ReadAll
   Ts.Close 
 End If
'-> Check For Text File Two
 If Fso.FileExists("Test2.txt") Then
  C2 = True 
  Set Ts =  Fso.OpenTextFile("Test2.txt")
   Tx2 = Ts.ReadAll
   Ts.Close 
 End If
'-> If C1 And C2 Are true Than Make The Third Text File
 If C1 = True And C2 = True Then
  Set Ts = Fso.CreateTextFile("Test3.txt")
  Ts.WriteLine Tx1 & vbCrLf & Tx2 
  Ts.Close
  CreateObject("Wscript.Shell").run("Test3.txt")
 End If 

 

  • Upvote 1

Share this post


Link to post
Share on other sites
8 hours ago, jaclaz said:

You may want to ask the actual question and not a small part of it.

(full disclosure, maybe I know what the actual question is, because we talked about it via PM).

Remember, you want to reach a goal, express it, DO NOT divide the way you believe you can reach it in n little steps each disjointed from the previous one, there are several reasons for this, among them the fact that there may be "better" or "easier" ways to reach your goal, people doesn't know WHAT you are actually wanting to do, you may make an example for what you believe is the right approach which is either inaccurate or more simply "wrong" , so as soon as an answer will be given to you, you will come up with another question (because the answer is not suitable to your unknown goal) and then another question, and another question :ph34r:.

You have a test1.cmd that creates text1.txt and a test2.cmd that creates text2.txt, yet, your code deals with two other files outputfile.txt and volname.txt, and there is no trace of text3.txt.

So I have to assume that text1.txt is actually  outputfile.txt and that text2.txt is volname.txt.

I can provide you a perfectly valid answer to your question but that won't help you a little bit in getting near your GOAL, (this answers your question and example):

jaclaz
 

Hi Jaclaz,

First i'm sorry. I just want to split my problem because if i post full my goal, then may be there are too much texts, and may be it make the reader quite difficult to know what extractly i need help.

I tried your code and it work perfect. Also, as your advice, i edit #1 to full post and hope that if there is someone has same problem with me, it can help him/her.

Again, thank you very much, Jaclaz.

Share this post


Link to post
Share on other sites
8 hours ago, Mcinwwl said:

Written ad-hoc in Powershell 2.0 (should work on any newer version, which means every Windows system from XP up to today)
I'm not a Powershell professional, so it might not be written with respect to all best practices, but it surely works.
Remember to set your current working directory with cd or set-location before using.


$1 = Get-Content text1.txt
$2 = Get-Content text2.txt
foreach($line in $(0..($text1.length - 1))){
$1[$line] + " " + $2[$line] >> text3.txt} 

EDIT: Getting curious what the real goal is...

Tks for your reply. I do not know anything about powershell. I never use it before. i know that there are many ways that better than batch but I'm not good at coding then i always try to write some simple batch file to archive my goal.

I tried with your method and it work very well. After I have text3.txt, i can display it in batch by type command. This method is "mixed" method: powershell and batch.

I just have one thing make me worry that i do not know if powershell is always exists in all PC Windows or maybe winPE.

Again, thank you very much.

Share this post


Link to post
Share on other sites
6 hours ago, gunsmokingman said:

Here is the VBS way of doing it with checks

Thank you, gunsmokingman

I tried your .vbs code but get this output. If you have a free time, can you recheck your code and see if there is any typo in it? Tks
 

H: Removable Disk 4.0 GB
I: Removable Disk 7.9 GB

ÿþU S B 1                 U S B 2   
 

Also, just me make sure i'm copy your code completely. I upload your .vbs, too

test_vbs.zip

Share this post


Link to post
Share on other sites

Your problem is that one of the files is a UNICODE .txt, while the other is a plain ASCII .txt, and, BTW, that vbs expects both to be plain ASCII. :w00t:

  • Upvote 1

Share this post


Link to post
Share on other sites

I want to create a batch file with some wmic command to display USB driveletter, description, size, volume name.

@echo off
WMIC LOGICALDISK where drivetype=2 get caption,size,description,volumename > text.txt
for /f "tokens=1-5 skip=1" %%a in ('type "text.txt"') do call :display %%a %%b %%c %%d %%e
exit /b

:display
set drive=%1
set total=%4
set desc1=%2
set desc2=%3

set volname=%5

call :convertbytes total

echo %drive% %desc1% %desc2% %total%>> text1.txt
goto :eof

: convertbytes

REM run some command here to convert bytes to Gib

...

But i have problems when using above code to display vol name. Because in some case vol name contain space then i do not know how to use for /f token (tokens can up to 6, 7, 8 even more).

In case vol name contain space, then if we just using token 1-5, volname cannot fully display. Such as, FULL Vol name is : KINGSTON USB, then it only display KINGSTON.

So to solved my problem, i split to output vol name to text2.txt, all the rest will output to text1.txt. Then i will merge text in 2 file (text1.txt, text2.txt) to one file (text3.txt). Finally i use TYPE command to display text3.txt. This is exactly i want to create this topic to get help from you.

--------

My problem was solved by Jaclaz's batch code, by Mcinwwl's Powershell code, and of course Gunsmokingman's VBS code (but now I'm waiting his reply to edit *something* wrong may come from his typo).

Tks all of you, Jaclaz,  Mcinwwl, Gunsmokingman help me solve my problem.

God bless you :)

Edited by congnt92

Share this post


Link to post
Share on other sites
4 minutes ago, dencorso said:

Your problem is that one of the files is a UNICODE .txt, while the other is a plain ASCII .txt, and, BTW, that vbs expects both to be plain ASCII. :w00t:

Thank you for your clarification, dencorso. You're right.

Text2.txt is output from batch code, i do not know why it is unicode .txt.

But after your hint, i save it as ANSI to make sure again. Rerun .vbs code, it still return
 

H: Removable Disk 4.0 GB
I: Removable Disk 7.9 GB

USB1        
USB2

Why i need to outptut as

H: Removable Disk 4.0 GB USB1        
I: Removable Disk 7.9 GB USB2 

Tks.

Share this post


Link to post
Share on other sites

Here is a simple VBS script that list Removable drives if they are connected. It will display the Drive Size, Free Space, Used Spaced.

'-> Constant For GB Size
Const GB = 1073741824
'-> Object For Runtime
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles For Run Time
Dim Drv, Dsk, Usb, vB :vB = vbTab 
'-> Loop Threw All Drives 
  For Each Dsk In Fso.Drives   
   If Dsk.IsReady Then
'-> Drive Type Removable Or USB 
    If Dsk.DriveType = 1 Then    
     Usb = Usb &_
     Dsk.DriveLetter & ":\ USB" & vB & "Size = " & Num(Dsk.TotalSize) & ", Free = " &_
     Num(Dsk.FreeSpace) & ", Used = " & Num(Dsk.TotalSize - Dsk.FreeSpace) & vbCrLf 
    End If   
   End If   
  Next
'-> Function To Convert Numbers To GB
  Function Num(N)    
   Num = Round(N/GB,2)
   If Num <= 10 Then Num = "0" & Num
   If Num > 0 And Num < 1 Then Num = "0" & Num
  End Function
'-> Show The Results
  If Chk = True Then 
   MsgBox vbTab & "Usb Drive Results" & vbCrLf & Usb, 4128,"Usb Querry"
  Else
   MsgBox "No Removable Drives Were Found",4128,"Usb Querry"
  End If
  • Upvote 1

Share this post


Link to post
Share on other sites

A batch was posted and deleted by accident, here it is it back:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET Caption=
ECHO Replace §'s with spaces
ECHO.
ECHO Drive§§Description§§§§§§§Size§§§§Volume Label

FOR /F "tokens=1,2 delims==:" %%A in ('WMIC LOGICALDISK where "drivetype=2" get caption^,size^,volumename /format:list') DO (
IF NOT %%B.==. SET %%A=%%B
IF DEFINED Caption IF %%A==VolumeName CALL :checkdata
)
GOTO :EOF

:checkdata
SET %Caption%_Caption=§%Caption%:§§
SET %Caption%_Size=%Size%
SET %Caption%_Volume=%Volumename%§§§§§§§§§§§
IF DEFINED %Caption%_Size (
call :truncatebytes %Caption%_Size
) ELSE (
SET %Caption%_Size=§§§§§§§§
)
ECHO !%Caption%_Caption!§§Removable disk§§!%Caption%_Size:~-8,8!§§!%Caption%_Volume:~0,11!
GOTO :EOF

:truncatebytes
SET thisvar=!%Caption%_Size!
SET thissize=
SET units=TB GB MB KB
FOR /L %%A IN (11,-3,2) DO (
SET thissize=!thisvar:~0,-%%A!
SET unit=!units:~-%%A,2!
IF "!thissize!"=="!thissize:~0,1!" SET thissize=
IF DEFINED thissize SET %Caption%_Size=§§!thissize:~0,-1!.!thissize:~-1,1! !unit!&&GOTO :EOF
)
GOTO :EOF

jaclaz
 

  • Upvote 1

Share this post


Link to post
Share on other sites

And again, I tried to do my best with Powershell. tested on PS 2.0/WinXP, both in PS console and Powershell ISE.
Script outputs drive letter, free space, total space and label for each active removable drive (pendrives, portable HDDs, memory cards... and floppy discs :P)

$drives = [System.IO.DriveInfo]::getdrives() | ?{$_.Drivetype -eq "Removable" -and $_.IsReady -eq $true}
foreach ($d in $drives) {Write-host ("$($d.RootDirectory.name.TrimEnd("\"))" + " " + [math]::round(($d.AvailableFreeSpace)/1GB,2) + " " +  [math]::round(($d.TotalSize)/1GB,2) + " " + "$($d.VolumeLabel)")}

That was my output with 4 connected pendrives:  

G: 1.78 1.87 
H: 14.87 29.28 
I: 3.73 3.73 
J: 7.16 7.31 KINGSTON

Hope that was expected.


 

  • Upvote 1

Share this post


Link to post
Share on other sites
10 hours ago, gunsmokingman said:

Here is a simple VBS script that list Removable drives if they are connected. It will display the Drive Size, Free Space, Used Spaced.

Hi, Gunsmokingman

First, Thank you for your enthusiasm.

I try your vbs code with 2 USB devices plug on my PC but still .vbs say that no removable drives found.

I do not know why i get this error. May come from copy paste issue. So can you please attach .vbs file for me.

Tks :)

Share this post


Link to post
Share on other sites
9 hours ago, jaclaz said:

A batch was posted and deleted by accident, here it is it back:

jaclaz
 

Try your code and work perfect. No temporarily file needed. Even your code is shorter. Tks Jaclaz :thumbup

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...