2016-09-23 60 views
0

我有一個文本文件,其中我有一個其他文件的引用。我想要做的是運行一個腳本,它將在運行時用文件的內容替換文件名,然後將其捲曲到服務器。用文件內的文件內容替換文件名在windows中

任何人都可以推薦一個快速的方法來使用PowerShell或類似的東西在Windows命令行上做到這一點?

例如

"responses": [ 
    { 
     "is": { 
     "statusCode": 200, 
     "headers": { 
      "Location": "http://localhost:4545/myresponse", 
      "Content-Type": "application/xml" 
     }, 
     "body": "@file:myresponse.xml" 
     } 
    }] 

將成爲:

$template = Get-Content template.txt -Raw 
$template = [regex]::Replace($template, '(@file:.*?)(?=")', {param($f) Get-Content ($f -replace '@file:') -Raw })) 
Invoke-WebRequest -Uri 'http://example.org/' -ContentType 'application/json' -Method Post -Body $template 

....根據您的設置的所有細節:

"responses": [ 
    { 
     "is": { 
     "statusCode": 200, 
     "headers": { 
      "Location": "http://localhost:4545/myresponse", 
      "Content-Type": "application/xml" 
     }, 
     "body": "<xml> contents of myresponse.xml within same directory.. </xml>" 
     } 
    }] 
+0

你是否知道PoSh與「windows命令行」無關? - powershell.exe aint PoSh;) –

+0

是的..我不是一個有經驗的Windows腳本編寫者,但是我認爲使用普通命令行腳本相比PowerShell非常困難,但希望保持我的選項開放:) – Pete

回答

1

你可以像做在PowerShell中。

正則表達式替換選取@file:位到下面的"並刪除前導位。這些匹配被輸入到scriptblock中,該腳本用於加載文件內容,並將其替換回來代替文件名。

如果你想要實際上curl它,你需要一個Windows的內置curl.exe。相同名稱的PowerShell別名不會嘗試以相同的方式運行。

+0

謝謝,很好的回答! :) – Pete