PowerShell: Read id3v2 from MP3, specifically ISRC - powershell

I'm ultimately aiming to get a hashtable of the path and ISRC of all the MP3 files in my music library for use in organising my library. Right now, I am having trouble getting the ISRC information out of the files. I have checked it is there using other software, but I particularly need to read it using powershell.
I've tried using a few Get-FileMetaData functions, but I think I was looking in the wrong place with that attempt.
In place of reading it the 'proper' way, I attempted to just read the file as plain text with Get-Content and manipulate the string to isolate the ISRC, which I can find when viewing the file in Notepad. The difficulty I ran into is managing the way the text is encoded (if that is the right word). There are whitespace characters inbetween the characters when viewed in notepad, which don't show up in PowerShell but still seem to count toward string length.
I would try to provide some code, but all I've had are dead ends, and I think the issue is in my understanding of what I'm working with. If I've skipped over any important information, please let me know. Tagged with unicode on a vague hunch that the string manipulation involves unicode.
So, how can I properly read the id3v2 tags using powershell (By properly I mean without bodgy string manipulation), or how can I interpret the raw file contents using powershell, i.e. deal with the special characters and whitespaces.
Thanks very much.
Raw content example: (Where the piece of interest is the text following 'TSRC')
ID3 >1TCON ) ÿþS i n g e r & S o n g w r i t r TRCK 1 TPOS 1 TIT2 ÿþv a l e n t i n e TPE1
ÿþD a f n a TXXX ÿþA R T I S T S ÿþD a f n a TALB ÿþv a l e n t i n e TPE2
ÿþD a f n a TLEN 151000TPUB # ÿþM a r g a l i t R e c o r d s TSRC ÿþQ Z 8 L D 1 9 8 6 2 3 3 TXXX - ÿþB A R C O D E ÿþ1 9 3 6 6 4 6 1 1 6 0 3 TYER 2019TDAT 0702APIC ‰ image/jpeg cover ÿØÿà JFIF H H ÿÛ C

Maybe this
Access Music File Metadata in Powershell
answer - using taglib.dll can help you too.

Get-Content has a parameter for -encoding.
If you can work out the encoding of those files, just put it in that parameter.
It's also worth checking your powershell version. I believe this behaviour changed between 5 and 6.

Related

Need to explain the kdb/q script to save partitioned table

I'm trying to understand this snippet code from:
https://code.kx.com/q/kb/loading-from-large-files/
to customize it by myself (e.x partition by hours, minutes, number of ticks,...):
$ cat fs.q
\d .Q
/ extension of .Q.dpft to separate table name & data
/ and allow append or overwrite
/ pass table data in t, table name in n, : or , in g
k)dpfgnt:{[d;p;f;g;n;t]if[~&/qm'r:+en[d]t;'`unmappable];
{[d;g;t;i;x]#[d;x;g;t[x]i]}[d:par[d;p;n];g;r;<r f]'!r;
#[;f;`p#]#[d;`.d;:;f,r#&~f=r:!r];n}
/ generalization of .Q.dpfnt to auto-partition and save a multi-partition table
/ pass table data in t, table name in n, name of column to partition on in c
k)dcfgnt:{[d;c;f;g;n;t]*p dpfgnt[d;;f;g;n]'?[t;;0b;()]',:'(=;c;)'p:?[;();();c]?[t;();1b;(,c)!,c]}
\d .
r:flip`date`open`high`low`close`volume`sym!("DFFFFIS";",")0:
w:.Q.dcfgnt[`:db;`date;`sym;,;`stats]
.Q.fs[w r#]`:file.csv
But I couldn't find any resources to give me detail explain. For example:
if[~&/qm'r:+en[d]t;'`unmappable];
what does it do with the parameter d?
(Promoting this to an answer as I believe it helps answer the question).
Following on from the comment chain: in order to translate the k code into q code (or simply to understand the k code) you have a few options, none of which are particularly well documented as it defeats the purpose of the q language - to be the wrapper which obscures the k language.
Option 1 is to inspect the built-in functions in the .q namespace
q).q
| ::
neg | -:
not | ~:
null | ^:
string | $:
reciprocal| %:
floor | _:
...
Option 2 is to inspect the q.k script which creates the above namespace (be careful not to edit/change this):
vi $QHOME/q.k
Option 3 is to lookup some of the nuggets of documentation on the code.kx website, for example https://code.kx.com/q/wp/parse-trees/#k4-q-and-qk and https://code.kx.com/q/basics/exposed-infrastructure/#unary-forms
Options 4 is to google search for reference material for other/similar versions of k, for example k2/k3. They tend to be similar-ish.
Final point to note is that in most of these example you'll see a colon (:) after the primitives....this colon is required in q/kdb to use the monadic form of the primitive (most are heavily overloaded) while in k it is not required to explicitly force the monadic form. This is why where will show as &: in the q reference but will usually just be & in actual k code

Trouble Importing Microsoft CSV(Historical-Search) into powershell

When I try to load the result of an Historical Search (through EAC) into my Powershell Script, I get whitespaces in the result between every letter. So for Example what looked in the original csv like
Header1, Header2, Header3,
Content1, Content2, Content3
Now Looks like
" H e a d e r 1 ", " H e a d e r 2 ", " H e a d e r 3 ",
" C o n t en t 1", ...
I already tried re-downloading the files and creating a datatable etc but nothing works because the data is just wrong.
When I open the csv in Editor, the whitespaces aren't there.
Select Statements also don't work.
BTW the same Thing works for the traditional message trace csv
$trace = Import-Csv W:\Path.csv
If anybody knows what might cause this, i would love to know the fix since it's driving me crazy
Update: I checked the csv on Notepad++ and These are not whtitespaces, but \0 values.
Any Ideas how they got there and why they are there?

Will it be possible for PowerShell to passing value across to Lua

I'm new in PowerShell and Lua, just wandering will it be possible for PowerShell to passing value across to Lua ?
Example, I have a PowerShell will read through a csv file records and I would like to pass the id from the csv to Lua. Will it be possible and how?
Lua scripts can use command line arguments in the form of the top level vararg operator .... Here's an example:
test.lua
local a, b, c = ...
print('a', a)
print('b', b)
print('c', c)
On command line:
lua test.lua 1 2 3
Output:
a 1
b 2
c 3
So, in your powershell script, replace test.lua with your script, and the 1 2 3 with the CSV ID.
I've tried looking for a good resource for more info on how ... works, but I can't seem to find any. If anyone knows of one, let me know, or suggest an edit, thanks.

use perl to extract specific output lines

I'm endeavoring to create a system to generalize rules from input text. I'm using reVerb to create my initial set of rules. Using the following command[*], for instance:
$ echo "Bananas are an excellent source of potassium." | ./reverb -q | tr '\t' '\n' | cat -n
To generate output of the form:
1 stdin
2 1
3 Bananas
4 are an excellent source of
5 potassium
6 0
7 1
8 1
9 6
10 6
11 7
12 0.9999999997341693
13 Bananas are an excellent source of potassium .
14 NNS VBP DT JJ NN IN NN .
15 B-NP B-VP B-NP I-NP I-NP I-NP I-NP O
16 bananas
17 be source of
18 potassium
I'm currently piping the output to a file, which includes the preceding white space and numbers as depicted above.
What I'm really after is just the simple rule at the end, i.e. lines 16, 17 & 18. I've been trying to create a script to extract just that component and put it to a new file in the form of a Prolog clause, i.e. be source of(banans, potassium).
Is that feasible? Can Prolog rules contain white space like that?
I think I'm locked into getting all that output from reVerb so, what would be the best way to extract the desirable component? With a Perl script? Or maybe sed?
*Later I plan to replace this with a larger input file as opposed to just single sentences.
This seems wasteful. Why not leave the tabs as they are, and use:
$ echo "Bananas are an excellent source of potassium." \
| ./reverb -q | cut --fields=16,17,18
And yes, you can have rules like this in Prolog. See the answer by #mat. You need to know a bit of Prolog before you move on, I guess.
It is easier, however, to just make the string a a valid name for a predicate:
be_source_of with underscores instead of spaces
or 'be source of' with spaces, and enclosed in single quotes.
You can use probably awk to do what you want with the three fields. See for example the printf command in awk. Or, you can parse it again from Prolog directly. Both are beyond the scope of your current question, I feel.
sed -n 'N;N
:cycle
$!{N
D
b cycle
}
s/\(.*\)\n\(.*\)\n\(.*\)/\2 (\1,\3)/p' YourFile
if number are in output and not jsut for the reference, change last sed action by
s/\^ *[0-9]\{1,\} \{1,\}\(.*\)\n *[0-9]\{1,\} \{1,\}\(.*\)\n *[0-9]\{1,\} \{1,\}\(.*\)/\2 (\1,\3)/p
assuming the last 3 lines are the source of your "rules"
Regarding the Prolog part of the question:
Yes, Prolog facts can contain whitespace like this, with suitable operator declarations present.
For example:
:- op(700, fx, be).
:- op(650, fx, source).
:- op(600, fx, of).
Example query and its result, to let you see the shape of terms that are created with this syntax:
?- write_canonical(be source of(a, b)).
be(source(of(a,b))).
Therefore, with these operator declarations, a fact like:
be source of(a, b).
is exactly the same as stating:
be(source(of(a,b)).
Depending on use cases and other definitions, it may even be an advantage to create this kind of facts (i.e., facts of the form be/1 instead of source_of/2). If this is the only kind of facts you need, you can simply write:
source_of(a, b).
This creates no redundant wrappers and is easier to use.
Or, as Boris suggested, you can use single quotes as in 'be source of'/2.

Scala file reading adding spaces

I'm reading a file in scala using
def fileToString(that:String):String= {
var x:String=""
for(line <- Source.fromFile(that).getLines){
x += line + "\n"
}
x
}
This works fine for a scala file. But on a txt file it adds spaces between every character. For example. I read in a .txt file and get this:
C a l l E v e n t L o g ( E r r o r $ , E r r N u m , E r r O b j )
' E n d E r r o r h a n d l i n g b l o c k .
E n d S u b
and I read in the scala file for the program and it comes out normally
EDIT: It seems to be something to do with Encoding. When I change it to UTF-16, it reads the .txt file, but not the scala file. Is there a way to make it universally work?
No it can't work for all files. To read/interpret a file/data you need to know the format/encoding unless you're treating it as a binary blob.
Either save all files in the usual unicode format (UTF-8) or specify the encoding when reading the file.
FromFile takes an implicit codec, you can pass it explicitly.
io.Source.fromFile("123.txt")(io.Codec("UTF-16"))
In general, if you read from a file you need to know its encoding in order to correctly read the characters. I am not sure what the default encoding is that Scala assumes, probably UTF8, but you can either pass a Codec to fromFile, or specify the encoding as a string:
io.Source.fromFile("file.txt", "utf-8")
It's hard to be sure, but it sounds like the two files were written with different encodings. On any Unix system (including Mac) you can use the command od to look at the actual bytes in the file.
UTF-8 is the standard for ordinary text files on most systems, but if you have a mix of UTF-8 and UTF-16, you'll have to know which encoding to use for which files and correctly specify the encoding.
Or be more careful when you create the files to insure that they are all in the same format.