Creating .h file with definitions using powershell and cmd

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

  • Splitting a multi-line environment variable into lines
  • What does typing “cmd” in Git Bash do?
  • Adding cmdline (win7) as an Eclipse console view
  • Git Command Does not Work in Windows CMD
  • 'svn' is not recognized as an internal or external command
  • What's the Windows command shell equivalent of bash's `true` command?
  • .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

    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
            Wscript.Echo strText
        End If

    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 


    #define VERSION ""

    Please, help me. Thanks!

  • git: how to push to master an unrelated repo
  • How to set Git config options for all subdomains?
  • Git SourceTree:shows symbolic link files as unstaged file
  • git push with error message
  • Issue while trying to open a Maven project via Git in Eclipse Luna
  • Teamcity - Global VCS Root for all projects
  • One Solution collect form web for “Creating .h file with definitions using powershell and cmd”

    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
    Git Baby is a git and github fan, let's start git clone.