Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to change the format of all files in a folder from Unicode to ANSI

Tags:

powershell

Can someone give me a Powershell script that will change the format of all files in a folder from Unicode to ANSI? Here is a sample folder structure:

c:\DBObjects
  +StoredProcs
      - sp1.sql
      - sp2.sql
      - sp3.sql
  +Views
     - v1.sql
     - v2.sql
  .Functions
     - fn1.sql
     - fn2.sql

I tried the following script for one file and it worked. However it will create another file:

Get-Content sp1.sql -encoding Unicode | Set-Content sp1_Ansi.sql -encoding ASCII

Thank you!

like image 330
rk1962 Avatar asked Jan 17 '23 08:01

rk1962


1 Answers

Try

Set-Content sp1.sql -Encoding ASCII -Value (Get-Content sp1.sql)

Note that the entire file will be read into memory temporarily. If that's not acceptable, you'll need to use the pipeline method to write to a new file, then delete the old file, and rename the new file to the old file's name.

like image 192
Josh Avatar answered May 10 '23 06:05

Josh