全てをググりながらなんとかディレクトリ配下のファイル変更、空フォルダ削除を実装したのでメモ
拡張子
test.ps1
出力
write-host $tmp
比較演算子
カレントディレクトリの取得
$curDir = Convert-Path .
ディレクトリの指定
2番目のように書くと2つ上に戻れる。
Set-Location -Path C:\work\xxxx Set-Location ../../ Set-Location $folderId.Name
カレントディレクトリのファイル情報の取得
- フォルダ
$folderNames = Get-ChildItem -Directory
- ファイル
$fileNames = Get-ChildItem -File
for文
foreach($curFile in $fileNames) { # 処理 }
もしくは
$fileNames | foreach { write-host $_ }
$_
がforeachの1つずつのobjを指すらしい。
ファイル名取得
上記変数からName
プロパティにアクセスする
$file.Name
文字列系
- 文字数の確認
$curFile.Name.Length
$tmp1 = $curFile.Name.Substring(0, 4) $tmp2 = $curFile.Name.Substring($tmp1.Length, $curFile.Name.Length-$tmp1.Length)
- 特定文字列の検索
他言語と同じ。-1
ならNot Found
if($curFile.Name.indexOf("test") -ne -1) { # 処理 }
- 文字列の連結
$st = $a + "_" + $b
ファイル名の変更
Rename-Item $curFile.Name -NewName $newFileName
ファイルの移動
下記のように記述すると1つ上のディレクトリにファイルを移動
Move-Item $newFileName ../
ファイルの削除
Remove-Item $folderId.Name