powershell - How does .Name work for variables? -
param( [parameter(mandatory=$true,position=1)] [string]$filename ) #$filename doesn't include directory or extension $dir = "c:\users\pb\desktop\source" $latest = get-childitem -path $dir | where-object {$_.name -like "*$filename*"} | sort-object lastwritetime -descending | select-object -first 1 -exclude "*import*" $filename = $latest.name $source = "c:\users\pb\desktop\source\${filename}"
basically, using .name
on $latest
make $latest
include directory name or include name of file, e.g. $filename
equal c:\users\pb\desktop\source\"filename"
or "filename"
?
in powershell, many other languages, dot-notation used accessing properties of object. in case $latest
contains fileinfo
object (or directoryinfo
object if *$filename*
matches folder), has number of properties, instance name
, fullname
, lastwritetime
, attributes
, etc.
you can display properties of object , respective values piping object format-list
cmdlet, while type can inspected instance via gettype()
method each object has:
ps c:\> get-item 'c:\temp\test.txt' ps c:\> $f.gettype().fullname system.io.fileinfo ps c:\> $f | format-list * pspath : microsoft.powershell.core\filesystem::c:\temp\test.txt psparentpath : microsoft.powershell.core\filesystem::c:\temp pschildname : test.txt psdrive : c psprovider : microsoft.powershell.core\filesystem psiscontainer : false versioninfo : file: c:\temp\test.txt internalname: originalfilename: fileversion: filedescription: product: productversion: debug: false patched: false prerelease: false privatebuild: false specialbuild: false language: basename : test mode : -a--- name : test.txt length : 8 directoryname : c:\temp directory : c:\temp isreadonly : false exists : true fullname : c:\temp\test.txt extension : .txt creationtime : 03.06.2015 00:11:07 creationtimeutc : 02.06.2015 22:11:07 lastaccesstime : 03.06.2015 00:11:07 lastaccesstimeutc : 02.06.2015 22:11:07 lastwritetime : 03.06.2015 00:11:17 lastwritetimeutc : 02.06.2015 22:11:17 attributes : archive
the properties , methods object has can inspected get-member
cmdlet:
ps c:> $f | get-member
typename: system.io.fileinfo
name membertype definition ---- ---------- ---------- mode codeproperty system.string mode{get=mode;} appendtext method system.io.streamwriter append... copyto method system.io.fileinfo copyto(str... create method system.io.filestream create() createobjref method system.runtime.remoting.objre... ... psiscontainer noteproperty system.boolean psiscontainer=... psparentpath noteproperty system.string psparentpath=mi... pspath noteproperty system.string pspath=microsof... psprovider noteproperty system.management.automation.... attributes property system.io.fileattributes attr... creationtime property datetime creationtime {get;set;} creationtimeutc property datetime creationtimeutc {get... directory property system.io.directoryinfo direc... ... basename scriptproperty system.object basename {get=i... versioninfo scriptproperty system.object versioninfo {ge...
in example, $latest.name
returns value of property name
, which, can see above, filename without path. $filename
contain value filename
, not c:\users\pb\desktop\source\filename
.
Comments
Post a Comment