2016-09-23 55 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