1
votes

I have several files generated by a build script that look like this:

sfml-audio-s-d.lib
sfml-graphics-s-d.lib
sfml-system-s-d.lib
...

I need to rename them to remove the -s-d part (which in certain cases can be just -s). After searching around SO for an hour I thought it would be simple to do using the rename command:

Ren ".\sfml-*-*.lib" "sfml-*.lib"

Hoverever it isn't changing any of the files in the directory.

What is the correct (and ideally one-liner) approach to renaming these files?

1

1 Answers

0
votes

Here are a few issue's you're having with your command. Firstly, it looks like you're attempting to rename a file with only -s. The problem is that Ren "sfml-*-*.lib" "" cannot assume the wildcard from the file path. Meaning that the REN does not know what to put for the * in "sfml-*.lib".

To get around this, you will need to address what the * is. To do this, a FOR statement can be used. The first thing the FOR statement will need is the filenames. Using the command For /f %%A In ('dir /b *.lib') Do (Echo %%A) will list all the file names. The issue now is that we only want audio in sfml-audio-s.lib.

To grab only certain objects using a FOR statement, you can add /f "tokens=2" %%A. This will tell the statement to only grab the second word in phrase. The problem in this scenario is that this only works if sfml-audio-s.lib was spaced sfml audio s.lib. By adding delims=- the statement will ignore all - in the parse. More info on that here.

From CMD: (Be sure to CD to directory location)

Remove "-s" from *.lib file names

For /f "tokens=2 delims=-" %A In ('dir /b *.lib') Do (Ren "sfml-*-*.lib" "sfml-%A.lib" 2>NUL)

Remove "-s-d" from *.lib file names

For /f "tokens=2 delims=-" %A In ('dir /b *.lib') Do (Ren "sfml-*-*-*.lib" "sfml-%A.lib" 2>NUL)

From BATCH: (Add CD C:\folder location to the script)

Remove "-s" from *.lib file names

For /f "tokens=2 delims=-" %%A In ('dir /b *.lib') Do (Ren "sfml-*-*.lib" "sfml-%%A.lib" 2>NUL)

Remove "-s-d" from *.lib file names

For /f "tokens=2 delims=-" %%A In ('dir /b *.lib') Do (Ren "sfml-*-*-*.lib" "sfml-%%A.lib" 2>NUL)