Home > Software engineering >  DIR but select specific parts of file names
DIR but select specific parts of file names

Time:12-30

I have the habit for years to use this very easy and convenient .BAT file to create a simple TXT file of all my files in a directory:

dir *.* > LIST.TXT

but the result includes many pieces of information that I don't need

Now I have many files in a directory in this way:

file 01 [shYddYJQIGfffY].xyz
second file 00008 [shYddYJQIfGf11dfzrffY].exe
filex [shGffY].sys
file that i don't need.txt
many other files that i don't need.bat

1/ which command line can I use with DIR (or anything else) to have in the final LIST.txt files only those information? only the value between [ and ]

Expected result:

shYddYJQIGfffY
shYddYJQIfGf11dfzrffY
shGffY

2/ how to edit the .BAT file to add the string MYRESULT before each of those results, like:

MYRESULT shYddYJQIGfffY
MYRESULT shYddYJQIfGf11dfzrffY
MYRESULT shGffY

CodePudding user response:

Use a for loop (see for /?):

for /f "tokens=2 delims=[]" %%a in ('dir /b *[*].*') do echo MYRESULT %%a

This for /f loop takes the output of dir /b *[*].* (filtering for relevant files, bare format) line per line and splits it into three parts, first token the part before the first [, token2 the part between [ and ]. and a third part after that. You need the second token only. Then just echo the string together with the extracted substring, done.

  • Related