Reputation: 3545
I'm newb in powerShell and in cmd, sorry. I'm trying write script that can generate define like this
#define VERSION "a89aa153a054b865c0ef0a6eddf3dfd578eee9d2"
in VERSION i want to set the parameter from next source
.git\refs\heads\project
I'm tried next cmd script, but i have problems with " character, i can't escape it
echo #define VERSION \" > ver.h
type .git\refs\heads\project >> ver.h
echo \" >> ver.h
Also i'm tried use script from that post 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
but i have a problems when i'm trying to run it. i'm created file 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
i want to read template and insert VERSION between " characters and write that text to the "ver.h", but i have got an error
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 ""
Please, help me. Thanks!
Upvotes: 1
Views: 637
Reputation: 24071
The script sample is VBScript, not Powershell. Thus, you cannot execute it on Powershell. You can, however, execute it via invoking cscript
. There is also wscript
that executes VBScript, but it is used for graphics: popup windows and such. Rename the file as .vbs
and run it. Like so,
cscript d:\writeHeader.vbs
Cmd.exe uses the hat char as escape. Like so,
C:\temp>echo #define VERSION ^" > ver.h
C:\temp>type ver.h
#define VERSION "
Edit: As per how to create the header on a single line, some trickery must be invoked. This would be much more simple in Powershell, but here goes.
:: 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..."
... But I'd still do this in Powershell. Like so,
$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
Upvotes: 2