Home > Enterprise >  extract content from a txt file and making txt files with that content using batch
extract content from a txt file and making txt files with that content using batch

Time:11-10

I have never tried this, and I have been searching to see the best way to make a script that can extract content from a txt file into their own txt files

Now I managed this and it extracts all content into their own txt files, but not sure how to make it use the extracted content as the txt filename

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('type "a.txt"') do (
    cd > %%a.txt
)
endlocal

so I have this

a.txt Content of my txt file 
  All_type_2020
  All_Foods_2021

and this is what I get

All_type_2020All_Foods_2021.txt

I have tested many different scripts, and the closes I have gotten was with the script above

my goal is to have this results

All_type_2020.txt

All_Foods_2021.txt

CodePudding user response:

I'm not entirely sure of what it is you're seeking, however I might have something that nevertheless might help you.

The code I wrote below will check for each sentence in "a.txt" if the first word is equal to "blue" if so it will output it into a txt file.

for /f "delims=" %%a in (a.txt) do if "%%a" == "blue" (
echo - %%a is blue >blue.txt
) else (
if "%%a" == "red" echo - %%a is red >red.txt)

pause 

This is an oversimplified version of what I think you're trying to do, but hopefully it can help. To change how the document filters look around the website and you will surely find ways for it to help.

CodePudding user response:

So, contents of a.txt

All_type_2020
All_Foods_2021
All_type_2021
All_Foods_2022
All_type_2022

Contents of my CMD file:

@echo off
for /f "delims=*" %%a in (a.txt) do (
echo. >%%a.txt
)

In the directory, I then have these empty files:

11/10/2022  10:22 AM    <DIR>          .
11/10/2022  10:22 AM    <DIR>          ..
11/10/2022  10:22 AM                77 a.txt
11/10/2022  10:22 AM                 3 All_Foods_2021.txt
11/10/2022  10:22 AM                 3 All_Foods_2022.txt
11/10/2022  10:22 AM                 3 All_type_2020.txt
11/10/2022  10:22 AM                 3 All_type_2021.txt
11/10/2022  10:22 AM                 3 All_type_2022.txt
           6 File(s)             92 bytes
  • Related