0

I am using mediainfo to pull metadata from an audio file and then use its information to generate a folder tree. Because this metadata allows for special characters such as astericks the folders cannot be generated. I can't seem to replace an asterisk using this script. I believe it might have to do with the fact that the * is a wildcard. Any thoughts or questions for me?

[mcve]

@echo off
setlocal enabledelayedexpansion
set "album_name=Make Sh*t Happen"
set "album_folder=!album_name:*=_!"
set album_folder
endlocal

I expect the following result:

album_folder=Make Sh_t Happen

I've tried a variety of different methods to try and replace the *, to no avail.

set "album_folder=!album_folder:^*=_!"

I'm definitely missing something.

Compo
  • 36,585
  • 5
  • 27
  • 39
Gatorman
  • 11
  • 2
  • This is an X-Y problem, you are asking about your failed solution. You should rather ask about the problem you are trying to solve with this solution. You are right that * is a wildcard, and for that reason not a valid character in a file path, so there can never be an * to replace. – Clifford Apr 23 '23 at 19:35
  • Thank you. I am pulling metadata with mediainfo from an audio file. This metadata is then used to create folders. However the metadata does allow for characters such as colons and asterisks. So when the script tries to generate the folders it is not able to. – Gatorman Apr 23 '23 at 19:38
  • Understood. That is the subject of https://stackoverflow.com/questions/15441422/replace-character-of-string-in-batch-script which has two answers that I imagine solve your problem. Bizarrely the most upvoted answer clearly doesn't. – Clifford Apr 23 '23 at 19:51
  • Thank you for the suggestions. I'll give them a try. – Gatorman Apr 23 '23 at 20:16

2 Answers2

1

My solution may not be pretty, but I finally got it to work. I export the mediainfo metadata to a txt file, then replace the asterisk with PowerShell. There is also a PowerShell command to Trim any spaces at the end of the line of the txt file.

for /f "delims=" %%b in ('%~dp0tools\mediainfo "--Inform=General;%%Performer%%" "%%a" 2^>nul') do (
        (echo %%b) > %~dp0tools\artist_name.txt && powershell -command "(get-content %~dp0tools\artist_name.txt) -replace '[\x2A]+', '_' | set-content %~dp0tools\artist_name.txt"
        set /p artist_name=<%~dp0tools\artist_name.txt
    )
Gatorman
  • 11
  • 2
  • `echo %%b > ...` adds a (not needed and undesired) space. Change to `(echo %%b) > ...` – Stephan Apr 25 '23 at 07:09
  • Nice! So I could remove the Trim command after using this fix? – Gatorman Apr 25 '23 at 13:01
  • I see no reason to trim spaces if there are none. Try it out - you can easily undo the little change in the (improbable) case it doesn't work as expected. – Stephan Apr 25 '23 at 13:18
0

Been asked a number of times. No universal solution afaiaa.

You could use something like this:

SETLOCAL

SET "string=hello*this&is*a*string"
:loop
FOR /f "tokens=1*delims=&*" %%b IN ("%string%") DO IF "%%c" neq "" SET "string=%%b_%%c"&GOTO loop

SET str

It's not perfect, but might suffice for your application.

OTT, if you're able to use 3rd-party utilities, then sed or (g)awk could be a solution.

Magoo
  • 77,302
  • 8
  • 62
  • 84