如何用各種方式快速地產生執行批次作業腳本 ⚡ (Create Script file by coding)


  1. 說明
    1. Powershell ISE
    2. Python

筆記如何用各種方式( Powershell, python, batch ? babsh ?) 快速地產生執行腳本 (Scripts)。

logo

說明

本次筆記的重點在於羅列各種通用性且最低門檻的編輯方式,以處理再不熟悉作業環境下如何能夠產生批次作業的腳本 (scripts)。

Powershell ISE

$var = @(1, 2, 3)
$fileName = "tmp.txt"
write "" | out-file $fileName

foreach($file in $var)
{
    write "cd $file" | out-file $fileName -Append
    write "copy $file.config $file.config.backup" | out-file $fileName -Append
    write "aspnet_regiis.exe -pef `"connectionStrings`" `".`"" | out-file $fileName -Append
}
  • 執行指令碼 F5
  • 回到編輯介面 Ctrl + I

Python

template = """
cd {0}
copy {0}.config {0}.config.backup
aspnet_regiis.exe -pef \"connectionStrings\" \".\"
"""

with open(r'c:\temp\tmp.txt', 'w') as f:
    for file in ('a', 'b', 'c'):
        print(template.format(file), file=f)

Python String Formatting Best Practices