0

I've a program (an IDL compiler) that creates a bunch of header files. These header files have a header guard with a random part at the end, like this:

#ifndef AUTOMATEDGENERATEDCODEIMPL_H_U1CBCG
#define AUTOMATEDGENERATEDCODEIMPL_H_U1CBCG

// My code here

#endif /* AUTOMATEDGENERATEDCODEIMPL_H_U1CBCG */

These files are created every time that I build my solution and the random part changes even if the rest of the code is not changed. So in every commit that I do in git I commit these files that causes a lot of useless conflicts (I build the code, another one build the same code obtaining other headers and then in rebase/merge we have conflicts on these header guards).

So I want to create a PowerShell script that runs in the post-build event in Visual Studio, that removes the random part of all header guards in all header files in that project.

How can I replace in PowerShell the string *IMPL_H_(random part) with *IMPL_H_ in all .h files?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jepessen
  • 11,744
  • 14
  • 82
  • 149

1 Answers1

2

Use a regular expression replacement:

Get-ChildItem -Recurse -Filter *.h | ForEach-Object {
  $file = $_.FullName
  (Get-Content $file) -creplace '(_IMPL_H_)[A-Z0-9]*', '$1' | Set-Content $file
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328