Search

2017/06/17

Powershellでフォルダ内のファイル名のリスト取得:Select-Obejct basename

Powershellでフォルダ内のファイル名の取得
結果はコピペでexcelに貼り付けできます。
get-childitem は、lsでもdirでも同じく使えるよ。
select-objectのエイリアスは%。


拡張子あり
get-childitem | select-object name

拡張子なし
get-childitem | select-object basename

拡張子のみ
get-childitem | select-object extension


エイリアスで
ls | % name
ls | % basename
ls | % extension


ls | % name は
For-Eachを使って ls | %{ $_.name }にもなる。

CSVに出力も。
get-childitem | select-object basename | export-csv c:\tmp\aaa.csv

ls | % basename | export-csv c:\tmp\aaa.csv


ついでにファイル名の一括変更

使い方
Get-ChildItem <対象ファイル> | Rename-Item -NewName { $_.Name -replace '旧文字列','新文字列' }
エイリアスで、
ls *.jpg | ren -newname { $_.name -replace '旧文字列', '新文字列'  }

拡張子を変更する
dir | rename-item -newname {$_.name -replace '\.jpg$','.jpeg'}


-whatif 付きでテスト
dir | rename-item -newname {$_.name -replace '\.jpg$','.jpg'} -whatif


参照)Windows PowerShellを使って複雑なパターンのファイル名変更を行う
http://www.atmarkit.co.jp/ait/articles/1411/07/news133.html





変数はこれ
プロパティはselect-objectできるはず。

TypeName: System.IO.FileInfo

Name                      MemberType     Definition
----                      ----------     ----------
Mode                      CodeProperty   System.String Mode{get=Mode;}
AppendText                Method         System.IO.StreamWriter AppendText()
CopyTo                    Method         System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(s...
Create                    Method         System.IO.FileStream Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateText                Method         System.IO.StreamWriter CreateText()
Decrypt                   Method         void Decrypt()
Delete                    Method         void Delete()
Encrypt                   Method         void Encrypt()
Equals                    Method         bool Equals(System.Object obj)
GetAccessControl          Method         System.Security.AccessControl.FileSecurity GetAccessControl(), System.Secur...
GetHashCode               Method         int GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys...
GetType                   Method         type GetType()
InitializeLifetimeService Method         System.Object InitializeLifetimeService()
MoveTo                    Method         void MoveTo(string destFileName)
Open                      Method         System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Op...
OpenRead                  Method         System.IO.FileStream OpenRead()
OpenText                  Method         System.IO.StreamReader OpenText()
OpenWrite                 Method         System.IO.FileStream OpenWrite()
Refresh                   Method         void Refresh()
Replace                   Method         System.IO.FileInfo Replace(string destinationFileName, string destinationBa...
SetAccessControl          Method         void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity)
ToString                  Method         string ToString()
PSChildName               NoteProperty   System.String PSChildName=field-1.jpg
PSDrive                   NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=C
PSIsContainer             NoteProperty   System.Boolean PSIsContainer=False
PSParentPath              NoteProperty   System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\tmp
PSPath                    NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\tmp\field-1.jpg
PSProvider                NoteProperty   System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.C...
Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
CreationTime              Property       datetime CreationTime {get;set;}
CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}
Directory                 Property       System.IO.DirectoryInfo Directory {get;}
DirectoryName             Property       string DirectoryName {get;}
Exists                    Property       bool Exists {get;}
Extension                 Property       string Extension {get;}
FullName                  Property       string FullName {get;}
IsReadOnly                Property       bool IsReadOnly {get;set;}
LastAccessTime            Property       datetime LastAccessTime {get;set;}
LastAccessTimeUtc         Property       datetime LastAccessTimeUtc {get;set;}
LastWriteTime             Property       datetime LastWriteTime {get;set;}
LastWriteTimeUtc          Property       datetime LastWriteTimeUtc {get;set;}
Length                    Property       long Length {get;}
Name                      Property       string Name {get;}
BaseName                  ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re...
VersionInfo               ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVer...



0 件のコメント:

コメントを投稿