powershell filter to remove .pdf extension in the name of a file -
i trying use powershell child elements in folder code using is
get-childitem -recurse -path c:\clntfiles
this code gives output like
mode lastwritetime length name ---- ------------- ------ ---- -a--- 4/29/2015 9:11 6919044 hd 100616 dec2014.pdf -a--- 5/1/2015 11:42 7091019 hd 101642 jan2015.pdf
i don't want mode lastwritetime length , name of file without .pdf extension
the output should
dec2014 jan2015
i not sure how filter that. please advise
i'll start posting similar leptonator's answer, simplified using select-object
command (alias select
used in code because it's habit, , i'm lazy).
$files = get-childitem -recurse -path c:\clntfiles | select -expandproperty basename
now gets file names without extension. but, asked part of file names, first file name "hd 100616 dec2014.pdf" , specified want "dec2014" returned. can couple different ways, favorite of them regex match (because regex awesome, , think lastindexof
/substring
combo overly complicated imho).
so, regex match of "\w+$"
want. broken down this:
\w
means letter or number
+
means 1 or more of them
$
means end of string/line
so that's 1 or more alpha-numeric characters @ end of string. pipe our array of file names foreach-object
loop (alias foreach
used out of habit), , have:
$files | foreach{ [regex]::matches($_,"\w+$")}
now, outputs [system.text.regularexpressions.match]
object, more want, have property value
asked for! use select -expand
again property , output precisely asked for:
$files = get-childitem -recurse -path c:\clntfiles | select -expandproperty basename $files | foreach{[regex]::matches($_,"\w+$")} | select -expand value
regex matches handy, , if learn them can simplify quite bit more this:
gci c:\clntfiles -rec | ?{$_.basename -match "(\w+)$"} | %{$matches[1]}
that 1 line, 2 line code above both should output:
dec2014 jan2015
Comments
Post a Comment