Denis
Denis

Reputation: 79

Remove selected lines from TXT file with .bat (CMD)

This is my TXT file:

 Volume in drive N is ABSdrive
 Volume Serial Number is BLA - BLA

 Directory of n:\ggg\gggg\ggg\ggggg

25/10/2016  09:46           138,813 gggggg.csv
               1 File(s)        00000 bytes
               0 Dir(s)  00000000 bytes free

What I need is .txt file with ONLY this line

25/10/2016  09:46           138,813 gggggg.csv

No empty lines on the top or bottom just text file with 1 single line with this information.

Is it any way of make it by using .bat file?

Upvotes: 3

Views: 2380

Answers (4)

aschipfl
aschipfl

Reputation: 34919

Use a for /F loop to parse text:

@echo off
for /F "usebackq skip=5 delims=" %%E in ("\path\to\your\text\file.txt") do (
    set "LINE=%%E"
    goto :EXIT_FOR
)
:EXIT_FOR
echo %LINE%

The skip=5 option tells for /F to ignore the first five lines. The goto command leaves the loop after having read the first non-skipped line. So the variable LINE will hold the line of interest finally.

Instead of using goto, you can also check whether the variable has already been defined, to pick one certain line of text, like this:

@echo off
set "LINE="
for /F "usebackq skip=5 delims=" %%E in ("\path\to\your\text\file.txt") do (
    if not defined LINE set "LINE=%%E"
)
echo %LINE%

The text file content looks like the output of a dir command (something like dir "n:\ggg\gggg\ggg\ggggg\gggggg.csv"). You can parse such command output directly by for /F without having to store it into a text file first -- see this:

@echo off
for /F "skip=5 delims=" %%E in ('dir "n:\ggg\gggg\ggg\ggggg\gggggg.csv"') do (
    set "LINE=%%E"
    goto :EXIT_FOR
)
:EXIT_FOR
echo %LINE%

Or:

@echo off
set "LINE="
for /F "skip=5 delims=" %%E in ('dir "n:\ggg\gggg\ggg\ggggg\gggggg.csv"') do (
    if not defined LINE set "LINE=%%E"
)
echo %LINE%

Upvotes: 0

Compo
Compo

Reputation: 38622

Drag and drop your folder onto this batch file.

@Echo Off
Set "SrcDir=%~1"
If Not Defined SrcDir Set "SrcDir=%CD%"
(For /F EOL^=^ Delims^=^^ %%A In ('Dir/A-D "%SrcDir%"') Do Echo=%%A)>output.txt

It should output the output.txt file to that folder. (if no input folder is given then it should provide the output.txt to the current directory).

If you are only wanting to parse the data from a file then adapt it like this:

@Echo Off
Set "SrcFile=%~1"
If Not Defined SrcFile Exit/B
(For /F UseBackQEOL^=^ Delims^=^^ %%A In ("%SrcFile%") Do Echo=%%A)>output.txt

Just drag/drop your file onto it or adapt it to replace %~1 with your file path/name on line two.

Upvotes: 0

aring
aring

Reputation: 3602

findstr /r /c:"^[0-9]" yourfile.txt > targetfile.txt

This getting lines which begins with digit, since that is what you need in your question.

Upvotes: 5

Dunny
Dunny

Reputation: 170

You could just use the findstr command and the write the output to a new textfile, outfile.txt Then you can search for that exact string in your file. However this will only write the output to a file if the string searched for matches the line exactly.

findstr /x "25/10/2016  09:46           138,813 gggggg.csv" infile.txt > outfile.txt

For more information on the findstr command I would recommend opening up a new cmd and typing in

findstr /?

Hope this helps

Upvotes: 0

Related Questions