How to read a txt file to sort out the string then output to a folder with some txt files
Moderator: DosItHelp
How to read a txt file to sort out the string then output to a folder with some txt files
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
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.
Re: How to read a txt file to sort out the string then output to a txt file
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.
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
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
Re: How to read a txt file to sort out the string then output to a folder with some txt files
Try this:
Antonio
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!
)
)
Re: How to read a txt file to sort out the string then output to a folder with some txt files
Thanks a lot! Antonio!
Yes works perfefectly!
Just a quick question for this line
So what is this 'findstr "_ ."?
the rest I can understand....
Thanks again!
Will
Yes works perfefectly!
Just a quick question for this line
Code: Select all
for /F "delims=\" %%a in ('findstr "_ ." source.txt') do (
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
You could help yourself by reading the help for the FINDSTR command.goodywp wrote: ↑27 Feb 2024 12:23Thanks a lot! Antonio!
Yes works perfefectly!
Just a quick question for this lineSo what is this 'findstr "_ ."?Code: Select all
for /F "delims=\" %%a in ('findstr "_ ." source.txt') do (
the rest I can understand....
Thanks again!
Will
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.
Re: How to read a txt file to sort out the string then output to a folder with some txt files
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 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
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
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
Thanks