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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#1 Post by goodywp » 26 Feb 2024 10:53

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
Last edited by goodywp on 26 Feb 2024 11:11, edited 4 times in total.

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

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

#2 Post by Squashman » 26 Feb 2024 11:03

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.

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

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

#3 Post by Squashman » 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.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#4 Post by goodywp » 26 Feb 2024 13:02

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.

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

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

#5 Post by Aacini » 26 Feb 2024 18:07

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

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#6 Post by goodywp » 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

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

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

#7 Post by Squashman » 27 Feb 2024 13:44

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.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#8 Post by goodywp » 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?

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

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

#9 Post by Squashman » 28 Feb 2024 17:47

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

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#10 Post by goodywp » 01 Mar 2024 08:58

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

Post Reply