1

私は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 ""

私を助けてください。ありがとう!

4

1 に答える 1

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 "

編集:1行にヘッダーを作成する方法に従って、いくつかのトリックを呼び出す必要があります。これは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
于 2012-11-19T07:53:32.670 に答える