cloud9_note

cloud9に限らないメモ

View on GitHub

コマンドプロンプト/PowerShell

PowerShell

PowerShellでこのシステムではスクリプトの実行が無効になっているため、ファイル hoge.ps1 を読み込むことができません。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

参考

uuidgen

[Guid]::NewGuid().toString()

toString()は無くても動くが、余計なものがついてくる。

tail

Get-Content -Path ${ファイルパス} -Tail 0 -Wait [-Encoding ${encode}]

参考

grep

Select-String -Path ${ファイルパス} -Pattern ${検索文字列}

再帰grep

Get-ChildItem -Filter ${ファイル条件} -Recurse | Select-String -Pattern ${検索文字列}

find

Get-ChildItem -r -Filter "条件" -Name
dir -Recurse | Get-ChildItem -Filter "条件" -Recurse

touch(新しいファイルを生成する)

New-Item -Path . -Name "test.txt" -ItemType "file" -Value "New-Item cmdlet test."

空のファイルを作る

New-Item -Type File ${ファイルパス}

ファイルを空にする

Clear-Content ${ファイルパス}

コンソール出力

Write-Host $str

ps1ファイルの起動引数の取得

Write-Host $Args[0]
Write-Host $Args[1]

文字列の置換

$str = '{hoge}'
Write-Host $str.replace($str, 'hoge')

ファイル読み込み

単一の文字列

$str = Get-Content $file_path -Raw

配列

$list = Get-Content $file_path

foreach ($str in $arr) {
    Write-Host $str
}

ファイル書き込み

# -Encodingを指定しない場合、ShiftJISになる。
Set-Content -Path $file_path -Value $str [-Encoding UTF8]

ファイルに追記

Add-Content -Path $file_path -Value $str

Map(ハッシュテーブル)

$hash_table = @{}
$hash_table.add('key','value')

foreach($key in $hash_table.Keys) {
    Write-Host $hash_table[$key]
}

Excel -> TSV

ChatGPTによる生成。まだ試せていない。

param (
    [string]$inputFilePath,
    [string]$outputFilePath,
    [string]$sheetName,
    [bool]$preserveNewLines
)

if (-not $inputFilePath -or -not $outputFilePath -or -not $sheetName -or ($preserveNewLines -eq $null)) {
    Write-Error "Usage: .\ExcelToTsv.ps1 -inputFilePath <InputExcelFilePath> -outputFilePath <OutputTsvFilePath> -sheetName <SheetName> -preserveNewLines <true|false>"
    exit 1
}

# Excel COMオブジェクトを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false

try {
    # Excelファイルを開く
    $workbook = $excel.Workbooks.Open($inputFilePath)
    $sheet = $workbook.Sheets.Item($sheetName)

    if (-not $sheet) {
        Write-Error "Sheet '$sheetName' not found in the workbook."
        exit 1
    }

    # 使用されている範囲を取得
    $range = $sheet.UsedRange
    $data = $range.Value2

    # データをTSV形式に変換
    $tsvContent = ""
    for ($i = 1; $i -le $data.GetLength(0); $i++) {
        $line = ""
        for ($j = 1; $j -le $data.GetLength(1); $j++) {
            $cellValue = $data[$i, $j]
            if ($preserveNewLines) {
                $cellValue = $cellValue -replace "\r\n|\n|\r", "`n"
            } else {
                $cellValue = $cellValue -replace "\r\n|\n|\r", " "
            }
            if ($j -eq 1) {
                $line += $cellValue
            } else {
                $line += "`t" + $cellValue
            }
        }
        $tsvContent += $line + "`r`n"
    }

    # TSVファイルに書き込み
    Set-Content -Path $outputFilePath -Value $tsvContent -Encoding UTF8

    Write-Output "TSVファイルへの出力が完了しました: $outputFilePath"
}
catch {
    Write-Error "エラーが発生しました: $_"
}
finally {
    # Excelを閉じてリソースを解放
    if ($workbook) { $workbook.Close($false) }
    if ($excel) { $excel.Quit() }
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
}

tsv -> Excel

ChatGPTによる生成。まだ試していない。

PowerShellを使って、TSVファイルの内容をExcelファイルに変換するスクリプトも作成することができます。このスクリプトでは、Excel COMオブジェクトを利用して、TSVファイルを読み込み、それを新しいExcelワークブックに書き込みます。

以下に、TSVからExcelに変換するPowerShellスクリプトの例を示します。

PowerShellスクリプト例:TSVからExcelへの変換

# 起動引数の確認
param (
    [string]$tsvFilePath,
    [string]$outputExcelPath,
    [string]$sheetName = "Sheet1"
)

if (-not $tsvFilePath -or -not $outputExcelPath) {
    Write-Error "Usage: .\TsvToExcel.ps1 -tsvFilePath <TsvFilePath> -outputExcelPath <OutputExcelPath> [-sheetName <SheetName>]"
    exit 1
}

# TSVファイルの内容を読み込む
$tsvContent = Get-Content -Path $tsvFilePath

# Excel COMオブジェクトを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false

# 新しいExcelワークブックを作成
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Sheets.Item(1)
$sheet.Name = $sheetName

# TSVデータをExcelシートに書き込み
$rowIndex = 1
foreach ($line in $tsvContent) {
    $columns = $line -split "`t"
    $columnIndex = 1
    foreach ($value in $columns) {
        $sheet.Cells.Item($rowIndex, $columnIndex).Value2 = $value
        $columnIndex++
    }
    $rowIndex++
}

# Excelファイルを保存
$workbook.SaveAs($outputExcelPath)

# Excelを閉じてリソースを解放
$workbook.Close($true)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

Write-Output "Excelファイルへの変換が完了しました: $outputExcelPath"

スクリプトの使い方

このスクリプトを実行するには、起動引数として以下の情報を渡す必要があります:

  1. TSVファイルのパス (-tsvFilePath)。
  2. 出力するExcelファイルのパス (-outputExcelPath)。
  3. シート名(任意) (-sheetName)。デフォルトは "Sheet1" です。

例えば、次のように実行します。

.\TsvToExcel.ps1 -tsvFilePath "C:\path\to\your\file.tsv" -outputExcelPath "C:\path\to\your\output.xlsx" -sheetName "MyDataSheet"

変更点の説明

  1. 起動引数の定義
    • param ブロックでスクリプトの起動引数を受け取るようにしました。
    • $tsvFilePath, $outputExcelPath, $sheetName の3つの引数を受け取ります($sheetName はオプションで、デフォルト値は "Sheet1")。
  2. TSVデータの読み込み
    • Get-Content コマンドを使ってTSVファイルの各行を読み込みます。
  3. Excelファイルへの書き込み
    • Excel COMオブジェクトを作成して、新しいExcelワークブックを生成します。
    • シートに対して、各行・列のデータを書き込みます。
  4. Excelファイルの保存
    • $workbook.SaveAs($outputExcelPath) でExcelファイルとして保存します。

注意点

このスクリプトを使って、TSVファイルのデータをExcelに変換する作業が簡単にできるはずです。何か追加の機能やカスタマイズが必要であれば教えてください。

tsv -> Excel : 追記

末尾行に引数のTSVファイルの内容を追記する。


# 起動引数の確認
param (
    [string]$tsvFilePath,
    [string]$outputExcelPath,
    [string]$sheetName = "Sheet1"
)

if (-not $tsvFilePath -or -not $outputExcelPath) {
    Write-Error "Usage: .\TsvToExcel.ps1 -tsvFilePath <TsvFilePath> -outputExcelPath <OutputExcelPath> [-sheetName <SheetName>]"
    exit 1
}

# TSVファイルの内容を読み込む
$tsvContent = Get-Content -Path $tsvFilePath

# Excel COMオブジェクトを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false

# Excelファイルの存在を確認
if (Test-Path $outputExcelPath) {
    # 既存のファイルを開く
    $workbook = $excel.Workbooks.Open($outputExcelPath)
    # 既存シートを開く
    $sheet = $workbook.Sheets.Item($sheetName)
    # 既存データの最後の行を取得
    $rowIndex = $sheet.UsedRange.Rows.Count + 1
} else {
    # 新しいワークブックを作成
    $workbook = $excel.Workbooks.Add()
    $sheet = $workbook.Sheets.Item(1)
    $sheet.Name = $sheetName
    $rowIndex = 1
}

# TSVデータをExcelシートに書き込み(書式を維持)
foreach ($line in $tsvContent) {
    $columns = $line -split "`t"
    $columnIndex = 1
    foreach ($value in $columns) {
        # 値を直接セルに割り当て、書式を保持
        $sheet.Cells.Item($rowIndex, $columnIndex).FormulaR1C1 = $value
        $columnIndex++
    }
    $rowIndex++
}

# Excelファイルを保存
$workbook.SaveAs($outputExcelPath)

# Excelを閉じてリソースを解放
$workbook.Close($true)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

コマンドプロンプト

ディレクトリごとコピーする(robocopy)

robocopy %ORIGIN% %TARGET% /s /e

オプション

再帰削除

rem rm -rfの代替
rm /s /q %対象ディレクトリ%
rmdir /s /q %対象ディレクトリ%

find(grep)

rem 通常の検索
find %word% %filepath%

rem 件数のみ
find /s %word% %filepath%

rem 指定した単語が引っかからない行
find /v %word% %filepath%

パイプ

findstr

@rem ダブルクォーテーションで囲まなければいけない。シングルクォーテーションはNG。
findstr /s /i /n "検索文字列" "検索対象パス"

findstr:オプション

オプション 効果
/s 再帰検索する。
/n 検索結果に行番号を表示する。
/i 大文字小文字の違いを無視する。

ファイルサイズを取得する(where)

where /R %検索対象パス% * /T > %実行結果出力パス%

参考

xcopy

xcopy /S /E /F /G /H /R /K /Y SOURCE DESTINATION

参考

サービスの停止/起動

@REM 停止
net stop %service_name%

@REM 起動
net start %service_name%

MD5ハッシュ値取得

certutil -hashfile %filepath% MD5

bashのsha1sumのような、ハッシュ値を使って、ファイルが改ざんされていないことを確認する機能はない。