Page 1 of 1

How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 26 Feb 2024 10:53
by goodywp
Hi all,
I have a file as attached as below. The purpose is to make it automatically (programmatically) to be ready for the tools.
Here is the original txt file as below: (let's name as source.txt)

-------------------------------------------------------
Schemes_to_Sign_for_852046
Tetra terminal:
From


\T503-08667-0106\Application_Signing\Signed_Schemes\MockupSigned_T3


500122010100.S3S.mockup
500199010200.S3S.mockup

\T503-08667-0109\Application_Signing\Signed_Schemes\MockupSigned_T3


500072010400.S3S.mockup
500083010300.S3S.mockup
500099010300.S3S.mockup


\T503-08939-0102\Application_Signing\Signed_Schemes\MockupSigned_T3


500278010400.S3S.mockup

------------------------------------------------------------
The expected output result is as below:

It will create a folder name as 852046 based upon the last 6 digits string of the first line from source.txt
Under this folder there will be some files, in this cases, will be three files, one is T503-08667-0108.txt, T503-08667-0109.txt, and T503-08939-0102.txt
For each file list fist 6 digits of the scheme under this file

Take an example from above
So for T503-08667-0108.txt, it should list these two strings based upon the info provided from source.txt as below:
500122
500199

T503-08667-0109.txt, it should list these three strings based upon the info provided from source.txt as below:
500072
500083
500099

T503-08939-0102 it should list these one string based upon the info provided from source.txt as below:
500278

Re: How to read a txt file to sort out the string then output to a txt file

Posted: 26 Feb 2024 11:03
by Squashman
Why haven't you attempted to do this task yourself?
You have certainly been given enough code examples over the past 7 years to accomplish this task.

Re: How to read a txt file to sort out the string then output to a txt file

Posted: 26 Feb 2024 11:06
by Squashman
goodywp wrote:
26 Feb 2024 10:53
It will create a folder name as 852046
under this folder there will be some files, in this cases, will be three files, one is T503-08667-0108.txt, T503-08667-0109.txt, and T503-08939-0102.txt
This is contradictory. You are asking to create a folder but then you go on to say that several files already exist in this folder.

Re: How to read a txt file to sort out the string then output to a txt file

Posted: 26 Feb 2024 13:02
by goodywp
Squashman wrote:
26 Feb 2024 11:06
goodywp wrote:
26 Feb 2024 10:53
It will create a folder name as 852046
under this folder there will be some files, in this cases, will be three files, one is T503-08667-0108.txt, T503-08667-0109.txt, and T503-08939-0102.txt
This is contradictory. You are asking to create a folder but then you go on to say that several files already exist in this folder.
Sorry I shall mentoned these files to be generated since they are not generated yet.

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 26 Feb 2024 18:07
by Aacini
Try this:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "folder="
for /F "delims=\" %%a in ('findstr "_ ." source.txt') do (
   set "line=%%a"
   if not defined folder (
      set "folder=!line:~-6!"
      md "!folder!"
      cd "!folder!"
   ) else if "!line:~0,1!" equ "T" (
      set "file=!line!.txt"
   ) else (
      >> "!file!" echo !line:~0,6!
   )
)
Antonio

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 27 Feb 2024 12:23
by goodywp
Thanks a lot! Antonio!
Yes works perfefectly!
Just a quick question for this line

Code: Select all

for /F "delims=\" %%a in ('findstr "_ ." source.txt') do (
So what is this 'findstr "_ ."?
the rest I can understand....

Thanks again!
Will

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 27 Feb 2024 13:44
by Squashman
goodywp wrote:
27 Feb 2024 12:23
Thanks a lot! Antonio!
Yes works perfefectly!
Just a quick question for this line

Code: Select all

for /F "delims=\" %%a in ('findstr "_ ." source.txt') do (
So what is this 'findstr "_ ."?
the rest I can understand....

Thanks again!
Will
You could help yourself by reading the help for the FINDSTR command.

Code: Select all

Use spaces to separate multiple search strings unless the argument is prefixed
with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
You have used FINDSTR in at least 10 of your forum topics. There isn't anything more basic with the findstr command then finding two strings. You will retain information better if you do a little homework yourself.

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 28 Feb 2024 17:10
by goodywp
OK I see now.

I just had another issue when the fist line string has one space at the end. the folder will become 5 digits

Schemes_to_Sign_for_829501
the folder will be 29501.

Anyway to cut off the space before set it as folder?

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 28 Feb 2024 17:47
by Squashman
goodywp wrote:
28 Feb 2024 17:10
OK I see now.

I just had another issue when the fist line string has one space at the end. the folder will become 5 digits

Schemes_to_Sign_for_829501
the folder will be 29501.

Anyway to cut off the space before set it as folder?
I bet if you use the sites search capability you will find all kinds of posts on trimming spaces.

You should also familiarize yourself with the main web page of the site which has all kinds of examples.
https://www.dostips.com/
https://www.dostips.com/DtTipsStringOperations.php

Re: How to read a txt file to sort out the string then output to a folder with some txt files

Posted: 01 Mar 2024 08:58
by goodywp
Thanks again and just found another way to do so to replace the string having space with the same string no space to the source file before the above steps.
Thanks