【问题标题】:Creating .h file with definitions using powershell and cmd使用 powershell 和 cmd 创建带有定义的 .h 文件
【发布时间】:2012-11-19 07:06:16
【问题描述】:

我是 powerShell 和 cmd 的新手,抱歉。 我正在尝试编写可以像这样生成定义的脚本

#define VERSION "a89aa153a054b865c0ef0a6eddf3dfd578eee9d2"

在 VERSION 中我想从下一个来源设置参数

.git\refs\heads\project

我尝试了下一个 cmd 脚本,但我遇到了 " 字符的问题,我无法转义它

echo #define VERSION \" > ver.h
type .git\refs\heads\project >> ver.h
echo \" >> ver.h

我也尝试使用该帖子中的脚本http://blogs.technet.com/b/heyscriptingguy/archive/2008/04/29/how-can-i-read-a-text-file-and-extract-all-the-text-enclosed-in-double-quote-marks.aspx

但是当我尝试运行它时遇到了问题。 我创建了文件 writeDefine.ps1

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Scripts\DefineTemplate.txt", ForReading)

Do Until objFile.AtEndOfStream
    strText = ""
    strCharacter = objFile.Read(1)
    If strCharacter = Chr(34) Then
        Do Until objFile.AtEndOfStream
           strNewCharacter = objFile.Read(1)
           If strNewCharacter = Chr(34) Then
               Exit Do
           End If
           If strNewCharacter <> "" Then
               strText = strText & strNewCharacter
           End If
        Loop
        Wscript.Echo strText
    End If
Loop

objFile.Close

我想读取模板并在 " 字符之间插入 VERSION 并将该文本写入 "ver.h",但是 我有一个错误

D:\writeHeader.ps1:4 symbol:67
+ Set objFile = objFSO.OpenTextFile("D:\Scripts\DefineTemplate.txt", <<<<  ForReading)
    + CategoryInfo          : ParserError: (,:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken 

定义模板.txt

#define VERSION ""

请帮帮我。谢谢!

【问题讨论】:

  • 你的代码是vbscript,没有powershell。将其重命名为 writeDefine.vbs。

标签: windows git file powershell cmd


【解决方案1】:

脚本示例是 VBScript,而不是 Powershell。因此,您不能在 Powershell 上执行它。但是,您可以通过调用 cscript 来执行它。还有wscript 执行VBScript,但它用于图形:弹出窗口等。将文件重命名为.vbs 并运行它。像这样,

cscript d:\writeHeader.vbs

Cmd.exe 使用帽子字符作为转义。像这样,

C:\temp>echo #define VERSION ^" > ver.h
C:\temp>type ver.h
#define VERSION "

编辑:根据如何在单行上创建标题,必须调用一些技巧。这在 Powershell 中会简单得多,但在这里。

:: Assume git data is in git.dat and it doesn't contain a newline
echo ^"> quote.txt
<nul set /p d=#define VERSION ^"> ver.h
copy /y ver.h+git.dat+quote.txt ver2.h
type ver2.h
#define VERSION "0x000..."

...但我仍然会在 Powershell 中执行此操作。像这样,

$git = cat .git\refs\heads\project # Read the git project file
$str = $("#define VERSION `"{0}`"" -f $git) # Build a formatted string
set-content -LiteralPath ver.h -value $str # Write the string to a file

【讨论】:

  • 非常感谢!关于cmd。该脚本生成 3 行代码。你能告诉我,我怎样才能把它写成一行?
猜你喜欢
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
相关资源
最近更新 更多