Cut text in line after / before seperator in powershell

So here is what I am trying to do: I want to make a small powershell script that takes every file in my music library and then makes a hash amount and writes it to a file like this:

test.txt; 131 136 80 89 119 17 17 60 123 210 121 188 42 136 200 131 198

Now, when I run the script, I need to first compare my music library with existing values, but for this I just want to cut everything off; so that it can compare the file name with the file name (or file) ... but I'm at a dead end how to do this, tried to replace $ name = $ name -replace "; *", "" do not go ...

tried to filter ... don't know how D:

I am very grateful for the help.

Also, if you think that I am using the wrong coding language, tell me, and what will be better, I just used C and powershell

+8
string powershell
source share
5 answers
$pos = $name.IndexOf(";") $leftPart = $name.Substring(0, $pos) $rightPart = $name.Substring($pos+1) 

PowerShell uses the String class internally .

+33
source share
 $text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198" $text.split(';')[1].split(' ') 
+4
source share

You can use Split :

 $text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198" $separator = ";" # you can put many separator like this "; : ," $parts = $text.split($separator) echo $parts[0] # return test.txt echo $parts[1] # return the part after the separator 
+2
source share

This works for a specific separator for a certain number of characters between the separator. I had a lot of problems trying to use this in for every cycle where the position changed but the separator was the same. For example, I used a backslash as a separator and wanted to use everything that is to the right of the backslash. The problem was that after determining the position (71 characters from the beginning), it would use $ pos as 71 every time, regardless of where the delimiter was actually in the script. I found another method of using a separator and .split to break things up, and then used the split variable to call partitions. For example, the first section was the variable $ [0], and the second section was the variable $ [1].

+1
source share

I had a complete directory of files, including some that were called invoice no-product no.pdf, and I wanted to sort them by product number, so ...

 get-childitem *.pdf | sort-object -property @{expression={$\_.name.substring($\_.name.indexof("-")+1)}} 

Note that if not - this sorts by $_.name

0
source share

All Articles