powershell - Find character position and update file name -


what function might use find character position in string using powershell 2.0.

i.e use charindex or patindex if using sql server.

i looked @ using select-string cmdlet doesn't seem need do.

ultimately i'm looking find "_" character in file name , strip off following "." .

example file name 237801_201011221155.xml

final solution, below strips out characters , including <_> <.> .xml files in current directory

get-childitem *.xml | rename-item -newname `   { $_.name -replace $_.name.substring($_.name.indexof("_"), `        $_.name.lastindexof(".") - $_.name.indexof("_") ),''} 

will end 237801.xml

the string .net string can use .net methods. in case:

$index = "the string".indexof(" ") 

will return 3, first occurrence of space in string. more information see: http://msdn.microsoft.com/en-us/library/system.string.aspx

for need try like:

$s.substring($s.indexof("_") + 1, $s.lastindexof(".") - $s.indexof("_") - 1) 

or use regexps:

if ($s -match '(_)(.*)(\.)[^.]*$') {  $matches[2] } 

(has adjusted depending on need).


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -