Preview the file for the entire .cs file in a directory and subdirectory using PowerShell

How can I pre-delay (insert at the beginning of a file) a file to all type files in a folder and subfolders using Powershell?

I need to add a standard header file to all .cs and try to use Powershell for this, but so far I have been able to add it to several lines of code, I got stuck trying to postpone it beforehand.

+3
source share
3 answers

Here is a very simple example to show you one way to do it.

$Content = "This is your content`n"
Get-ChildItem *.cs | foreach-object { 
$FileContents = Get-Content -Path $_
Set-Content -Path $_ -Value ($Content + $FileContents)
}
+9
source

I don’t know, but if you have code to add, just do it the other way around. Sort of

  • rename an existing file,
  • create an empty file with the name above
  • ,
  • ,
+1

, :

1) read the contents of the file you need to change

2) change the content (as a string, if you have content in a variable called content), for example: content = header + content

3) tend to the beginning of the file, each language has a search method or equivalent search

4) write new content

5) crop the file at the position returned by the file pointer

There you have it, there is no temporary file. :)

+1
source

Source: https://habr.com/ru/post/1696975/


All Articles