0

I am trying to use a batch file to generate linux library symbolic links (Yes I'm trying to do this on windows, it needs to be cross platform). I have 3 files

  • libnum1.so.1.0
  • libnum2.so.1
  • libnum3.so

I am trying to loop through the files, and only generate links for the first 2 files (the 3rd file doesn't need a link generated). I am using the following command to loop through the files

for %%F in (lib*.so.*) do (
    echo %%F
)

However, instead of just grabbing the first 2 files, its also grabbing the 3rd file, which is breaking my script. How do I make the for loop ignore any file that ends in just .so?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Thad House
  • 133
  • 1
  • 7

3 Answers3

1
for %%F in (lib*.so.*) do IF /i "%%~xF" NEQ ".so" (

If the extension part of the filename is not equal to .so, regardless of case...

see for/?|more or `if/? from the prompt for docco

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

I totally forgot about the undocumented wildcards.

for %%G IN ("lib*.so.<*") do echo %%G

You can read about it over on Dostips. http://www.dostips.com/forum/viewtopic.php?f=3&t=6207#p39390 But here are the tests that were run by Dave.

                |                         |      GREEDY NATURE                                                          
file            | "??.??.??" | ">>.>>.>>" | "?a?.??.??" | ">a>.??.??"
----------------+------------+------------+-------------+-------------
a               |  match     |  no match  |  no match   |  no match
ab              |  match     |  no match  |  no match   |  no match
abc             |  no match  |  no match  |  no match   |  no match
a.1             |  match     |  no match  |  no match   |  no match
ab.12           |  match     |  no match  |  no match   |  no match
abc.123         |  no match  |  no match  |  no match   |  no match
a.1.x           |  match     |  match     |  no match   |  no match
ab.12.xy        |  match     |  match     |  no match   |  no match
abc.123.xyz     |  no match  |  no match  |  no match   |  no match
a.1.x.7         |  no match  |  no match  |  no match   |  no match
ab.12.xy.78     |  no match  |  no match  |  no match   |  no match
abc.123.xyz.789 |  no match  |  no match  |  no match   |  no match

                                                         | NON-GREEDY
file            | "*.*.*"  | "*."     | "**." | "abc.*." | "*a*"
----------------+----------+----------+-------+----------+-----------
abc             |  match   | match    | match | match    | match
abc.123         |  match   | no match | match | match    | match
abc.123.xyz     |  match   | no match | match | match    | match
abc.123.xyz.789 |  match   | no match | match | match    | match

                                                         |      NON-GREEDY
file            | "<.<.<"  | "<"      | "<<"  | "abc.<"  | "<a<"    | "<a<<"
----------------+----------+----------+-------+----------+----------+--------
abc             | no match | match    | match | no match | match    | match
abc.123         | no match | no match | match | match    | no match | match
abc.123.xyz     | match    | no match | match | no match | no match | match
abc.123.xyz.789 | match    | no match | match | no match | no match | match
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Good table. Will need to be printed out - I'm never going to recall all those combinations... – Magoo Nov 04 '16 at 19:29
0

Magoo's answer is the best solution for your problem but here is a way to solve it using a regular expression with the FINDSTR command.

FOR /F "delim=" %%G IN ('dir /B lib*.so.* ^|findstr /R /C:"lib.*\.so\..*"') DO ECHO %%G
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Um, what happens with filename `libnum3.so.something.so` ? – Magoo Nov 04 '16 at 19:05
  • @magoo, well that makes it a bit easier using findstr `dir /B lib*.so.* |findstr /I /E /V "so"` But as I said your solution is the easiest. Just wanted to show different ways to do it. – Squashman Nov 04 '16 at 19:23
  • 1
    Many ways - but what about `|findstr /I /E /L /V ".so"`. Interesting how other ideas pop up after you've got one to work. Even more interesting that batch generates a culture of co-operation on SO whereas some other subject areas can become mighty hostile... – Magoo Nov 04 '16 at 19:28
  • @Magoo, yes, that would be better as it would match the extension and not just something like **I_am_a_fatso**. – Squashman Nov 04 '16 at 19:39
  • What about `findstr /R /C:"^lib[^.][^.]*\.so\.[^.]$" /C:"^lib[^.][^.]*\.so\.[^.]\.[^.]$"`? (The `/I` switch is mandatory anyway as Windows treats file paths case-insensitively...) – aschipfl Nov 04 '16 at 21:23