0

I am trying to get the list of files having specific extension along with their version and store it in a text file. However, I wont get the desired output. The text file will show all the file's list in the folder.

@echo off
for %%f in (*.drw) do (
    if "%%~xf"==".drw" echo %%f
)
dir /b > files.txt
echo. & echo.

I am trying to extract the list of all files which will have extension in the form of:

test.drw.1
flange.drw.1
block.drw.2
hinge.drw.3
door.drw.1

The text file will show all the lists of the file (.pdf, .pro,.dwg,.asm). Is there something I am missing ? Or this is occurring because the programs tries to scan .drw extension stringently, and considers .1 and .2 as extensions and skips them ? Any help is appreciated.

Additionally, I would be grateful, if we are able to echo the file having the extension with highest version. E.g.

flange.drw.1
flange.drw.12

It should echo flange.drw.12 in the txt file and ignore the one with .drw.1

DaSnipeKid
  • 31
  • 6
  • The extensions of those files are `.1`, `.2`, and `.3`. Your code is looping through all non hidden files carrying a case insensitive extension beginning with `.drw` and then isolating from those only ones which case sensitvely match exactly `.drw`. – Compo Jun 16 '23 at 18:31
  • Exactly, but how can I club .1 with .drw and present result in text file. Also, I was thinking to eliminate the lower versions and consider the highest version of same file. It means, if there are two 'flange.drw.1' and 'flange.drw.2', it should list 'flange.drw.2' in the txt file. – DaSnipeKid Jun 16 '23 at 18:42
  • May be I need to compare the same name (string) of file and echo the highest version. – DaSnipeKid Jun 16 '23 at 18:44
  • Does `dir /b *.?` work for you? (technically it's too broad: listing files with no extension or a one-char extension, but might work good enough for your use case). In case it doesn't: `dir /b *.drw.? |findstr /ierc:"\.drw\.[0123456789]"` (for one-digit extensions) or `dir /b *.drw.? |findstr /ierc:"\.drw\.[0123456789][0123456789]*"` (for numeric extensions with one or more digits). ok, void after your above comments. – Stephan Jun 16 '23 at 18:49
  • @Stephan For two-digit extensions, it still shows the names of files with one-digit versioned extension. – DaSnipeKid Jun 16 '23 at 19:04
  • Correct @DaSnipeKid. Unfortunately Stephan made a rare mistake, ```... findstr /ierc:"\.drw\.[0123456789][0123456789]*"``` isolates those with `1` or more integer digits. As many people often forget that the `*` wildcard usually matches `0` or more characters. Effectively, in this case, "match one digit then zero or more digits". – Compo Jun 16 '23 at 19:08
  • No mistake. I said `with one or more digits`. If you want *exactly* two digits, remove the wildcard (`*`) – Stephan Jun 17 '23 at 08:16

1 Answers1

1

Hmm... First, let's try to adhere to a certain nomenclature...

The extension of a filename is the last part of the name separated by a point. In your example files above all files have digits as extensions...

If you want to list the files that ends in .drw.<anything>, then this should be enough:

dir /B *.drw.*

If you want the files having the same name part and the highest extension, then this should do it:

@echo off
setlocal EnableDelayedExpansion

for %%a in (*.drw.*) do (
   if not defined file[%%~Na] (
      set "file[%%~Na]=%%~Xa"
   ) else (
      set "ext=%%~Xa"
      if !ext:~1! gtr !file[%%~Na]:~1! set "file[%%~Na]=%%~Xa"
   )
)

echo Files with last "versions" (extensions really):
for /F "tokens=2,3 delims=[]=" %%a in ('set file[') do echo %%a%%b

For example, with these input files:

test.drw.1
flange.drw.1
block.drw.2
hinge.drw.3
door.drw.1
test.drw.3
flange.drw.4
block.drw.5
hinge.drw.2
door.drw.2
flange.drw.12

... this is the output:

Files with last "versions" (extensions really):
block.drw.5
door.drw.2
flange.drw.12
hinge.drw.3
test.drw.3
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • The solution works. However, I am not able to get the contents (echoes) into txt file. Apologize, i am a novice, and trying to learn the batch scripts. Tried > output.txt, but it returned a single file name result. – DaSnipeKid Jun 16 '23 at 20:12
  • Enclose in parens _the whole_ `for` command: `(for /F "tokens=2,3 delims=[]=" %%a in ('set file[') do echo %%a%%b) > output.txt` – Aacini Jun 17 '23 at 01:10