【问题标题】:Replace filename with file contents within a file in windows用windows中文件中的文件内容替换文件名
【发布时间】:2017-01-31 19:47:51
【问题描述】:

我有一个文本文件,其中包含对其他文件的引用。我想要做的是运行一个脚本,它将在运行时用文件的内容替换文件名,然后将其卷曲到服务器。

谁能推荐一种在 windows 命令行上使用 powershell 或类似工具的快速方法?

例如

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

会变成:

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

【问题讨论】:

  • 你知道PoSh与“windows命令行”无关吗? -- powershell.exe 不是 PoSh ;)
  • 是的.. 我不是一个经验丰富的 Windows 脚本编写者,但是我认为与 powershell 相比,使用常规命令行脚本编写这将是相当困难的,但我想保持我的选项开放 :)跨度>

标签: windows command-line scripting text-files


【解决方案1】:

您可以在 PowerShell 中使用类似的方法:

$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

....取决于您的设置的所有细节。

正则表达式替换将位 @file: 挑选到以下 " 并删除前导位。匹配项被输入到脚本块中,该脚本块负责加载文件内容并替换文件名。

如果你想实际上 curl 它,你需要一个由curl.exe 构建的Windows。同名的 PowerShell 别名不会尝试以相同的方式运行。

【讨论】:

  • 谢谢,很好的回答! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多