2012-11-19 74 views
1

我福利局在PowerShell和在cmd中,遗憾的定义.h文件中。 我试着写脚本,它可以产生这样定义创建使用PowerShell和CMD

#define VERSION "a89aa153a054b865c0ef0a6eddf3dfd578eee9d2" 

在版我想从一个源设置参数

.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 

DefineTemplate.txt

#define VERSION "" 

请帮帮我,谢谢!

+1

您拥有的代码是vbscript,没有powershell。将其重命名为writeDefine.vbs。 –

+0

哦,谢谢)对不起) – BergP

回答

2

脚本样本VBScript中,不PowerShell的。因此,您无法在Powershell上执行它。你可以,但是,通过调用cscript执行它。还有执行VBScript的wscript,但它用于图形:弹出式窗口等。该文件重命名为.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 
+0

非常感谢!关于cmd。该脚本生成3行代码。你能告诉我,我怎么把它写在一行中? – BergP