Change / Save encoding How to convert several txt files UTF ...
文章推薦指數: 80 %
Hello. I need to convert several txt files , located in C:\Folder1 from UTF-8 to UTF-8-BOM. I had try these two variants, but non of them ... Home Anonymous Signintopost Post Explore Tags Questions Sitefeedback Articles Users Skiptomaincontent Findthreads,tags,andusers... SearchSearch Explore Tags Questions Sitefeedback Articles Users Signintopost Home question Clicktovote 0Votes"0 Clicktodownvote SuzanaEree-2102 asked • May22,'21 | RichMatheisen-8856 edited • Aug9,'22 Powershell:Change/SaveencodingHowtoconvertseveraltxtfilesUTF-8toUTF-8-BOM Hello.Ineedtoconvertseveraltxtfiles,locatedinC:\Folder1fromUTF-8toUTF-8-BOM. Ihadtrythesetwovariants,butnonofthemworks: get-itemC:\Folder1*.*|foreach-object{get-content-Encodingutf8BOM$_|out-file("C:\Folder1"+$_.Name)-encodingdefault} OR $MyPath="C:\Folder1" Get-ChildItem-Path$sourcedir-Filter*.txt|ForEach-Object{ #Thisvariablecanbereused $utf8=New-ObjectSystem.Text.UTF8Encoding$false $MyFile=Get-Content$MyPath-Raw Set-Content-Value$utf8.GetBytes($MyFile)-EncodingByte-Path$MyPath } Doesanyoneknowthesolution? windows-server-powershell Comment Comment Show 0 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. 6Answers SortbyCreatedCreated SortbyOldestOldest SortbyVotesVotes Clicktovote 0Votes"0 Clicktodownvote SuzanaEree-2102 answered • May23,'21 | SuzanaEree-2102 edited • May23,'21 yes,thejobisdone.ButifthereisasolutionalsoinPoweshell,Itiswelcome. also,youcanfindHEREanothersolutionsforconvertingUTF-8toUTF-8-BOM.Seemsthatyoucandothisjobusingnotepad++andREGEX,ItestalsothissolutionandWORKS! Forexample,inNotepad++,opentheFindinFilesdialog(Ctrl+Shift+F).AnduseRegularExpression: Search:\A ReplaceBy\x{FEFF} FILTERS:*.html ForothergreatsolutionsforconvertingUTF-8inUTF-8-BOMcheckthislinkbelow: https://community.notepad-plus-plus.org/topic/21200/change-save-encoding-how-to-convert-800-txt-files-utf-8-to-utf-8-bom image.png (26.1KiB) Comment Comment Show 0 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. Clicktovote 2Votes"2 Clicktodownvote AndreasBaumgarten answered • May22,'21 Hi@SuzanaEree-2102, Pleasetrythis: $a="./Junk/testfile-utf8.txt" $b="./Junk/testfile-utf8-2bom.txt" (Get-Content-path$a)|Set-Content-EncodingUTF8BOM-Path$b (Ifthereplywashelpfulpleasedon'tforgettoupvoteand/oracceptasanswer,thankyou) RegardsAndreasBaumgarten Comment Comment Show 0 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. Clicktovote 0Votes"0 Clicktodownvote SuzanaEree-2102 answered • May22,'21 | DRytsk-0361 commented • Aug7,'22 hellosir@AndreasBaumgarten Itestyourcode,Ihaveanerror.Seethis: Also,Imustconvertmorethan600txtfiles,notjustone.txtfile:( Comment Comment· Show 3 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. sdwheeler ·May25,2021at05:49PM WindowsPowerShell5.1doesnotsupporttheencodingvalueutf8NoBOM.ThatwasintroducedinPowerShell6.SoyouneedusePowerShell6orhigher. 0Votes0 · SuzanaEree-2102 sdwheeler ·May25,2021at07:18PM yes,rightnowIhavePowerShellversion5.1.19041.906.Iwillinstallanewestversionfromhere:https://www.itechtics.com/powershell-7-1-3/ 0Votes0 · DRytsk-0361 ·Aug07at10:44AM toscanmultiplefilesGet-Contentfile?.txt where'?'isanycharacterlike1,2,3,a,b,c 0Votes0 · Clicktovote 0Votes"0 Clicktodownvote AndreasBaumgarten answered • May22,'21 | SuzanaEree-2102 commented • May23,'21 Hi@SuzanaEree-2102, pleasetrythis: $a="./Junk/testfile-utf8.txt" $b="./Junk/testfile-utf8-2bom.txt" (Get-Content-path$a)|Set-Content-EncodingUTF8-Path$b Evenwith-EncodingUTF8itcreatesanUTF8-BOMencodedfile.AtleastNotepad++isshowingtheEncodingUTF8-BOMforthefile. Justtakealookinyourquestionsandanswersofthepastandyouwillfindhowtodothisforeachfileinafolder.https://docs.microsoft.com/en-us/answers/questions/396128/powershell-copy-a-stringword-from-a-bottom-line-to.html ----------(Ifthereplywashelpfulpleasedon'tforgettoupvoteand/oracceptasanswer,thankyou) RegardsAndreasBaumgarten image.png (130.7KiB) Comment Comment· Show 3 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. SuzanaEree-2102 ·May23,2021at05:00AM goodmorning@AndreasBaumgartenItestyourlastcode,itisworking.ButItconvertstoUTF-8.MyfilesarealreadyUTF-8.AndIdon'tknowwhyisnotworkingyourfirstcode,becauseonlythisisdifferentUTF8BOM 0Votes0 · AndreasBaumgarten SuzanaEree-2102 ·May23,2021at08:29AM Hi@SuzanaEree-2102, howdoyouverifytheencoding?IopenedaconvertedtxtfileinNotepad++anditshowsUTF-8-BOM(screenshotabove). (Ifthereplywashelpfulpleasedon'tforgettoupvoteand/oracceptasanswer,thankyou) RegardsAndreasBaumgarten 0Votes0 · SuzanaEree-2102 AndreasBaumgarten ·May23,2021at10:00AM @AndreasBaumgarten ItestbyrefreshingthehtmlpageafterrunningthePythonScriptonnotepad++.Seethisprintscreen. 0Votes0 · pytho.png (176.7KiB) Clicktovote 0Votes"0 Clicktodownvote SuzanaEree-2102 answered • May23,'21 | SuzanaEree-2102 published • May24,'21 Ifindoninternetagreatandsimplesolutionthatworksbyusingnotepad++.YoucanuseaPyhonscript: Firstinstallthepluginnamed"PythonScript".GotoMenu->Plugins->PluginsAdmin(selectPythonScript).AfterinstallthispluginselectNewScriptandsavethecodebelow.Therunthescript. YoucanfindthePythoncodehere:https://pastebin.com/KkZbqJWA Afteryourunthecode,youwillgetapopupwindowthatitpromptsyouthusly.JustwriteyourPathoverthere,thelocationofyourfolderfiles,forexample`c:\Folder1` Comment Comment· Show 1 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. AndreasBaumgarten ·May23,2021at09:42AM Hi@SuzanaEree-2102, sotheworkisdoneandnoPowerShellscriptisrequiredanymore? (Ifthereplywashelpfulpleasedon'tforgettoupvoteand/oracceptasanswer,thankyou) RegardsAndreasBaumgarten 0Votes0 · Clicktovote 0Votes"0 Clicktodownvote RichMatheisen-8856 answered • Aug9,'22 | RichMatheisen-8856 edited • Aug9,'22 Iknowthisisalateanswertothisquestion.Imeanttoansweritmuchearlier,butIlosttrackofthetopic. PowerShell5.1,byitself,can'taccomplishthisbutit'seasilydoneusingafewlinesofcodeusingthe.NetSystem.IO.StreamWriterandtheSystem.Text.UTF8Encodingclasses. Hereanexample: $in=Get-Content-PathC:\junk\123a.txt-Raw#getthewholefileinonegulp $Utf8NoBomEncoding=New-ObjectSystem.Text.UTF8Encoding$False $sw=[System.IO.StreamWriter]::new("c:\Junk\New123a.txt",$Utf8NoBomEncoding)#NoBOM! $sw.Write($in) $sw.Flush() $sw.Close() Theinputfilelookslikethis(notethe0xFFFEatthebeginning): TheOutputfilelookslikethis(thefilebeginswiththe1stbyteofdata--thefilewasn'topenedinawaythatwouldclobberanexistingfile,sothedata'stheretwice;onceforeachtimeIranthescript): 123atxt.jpg (23.6KiB) new123a.jpg (23.5KiB) Comment Comment Show 0 Comment 5|1600charactersneededcharactersleftcharactersexceeded ▼ Visibletoallusers Visibletotheoriginalposter&Microsoft Viewablebymoderators Viewablebymoderatorsandtheoriginalposter Advancedvisibility ToggleCommentvisibility.CurrentVisibility:Visibletoallusers Attachments:Upto10attachments(includingimages)canbeusedwithamaximumof3.0MiBeachand30.0MiBtotal. questiondetails 13peoplearefollowingthisquestion. AnswersSubscribetoAnswers AnswersandCommentsSubscribetoCommentsandAnswers RelatedQuestions New-Itemfailson"Policies"hivewithaccessdenied "Denythisuserpermissionstologontoremotedesktopsessionhostserver"-Powershellcommand FTPDownloadPowerShellScript Get-ADUser-FilterforblankAttribute howdoIgetpowershellpythontostopopeningwindowsstore? About · FAQ · Privacy · Copyright©2010-22DZone,Inc.
延伸文章資訊
- 1Get-Content - PowerShell Command - PDQ
UTF8: Encodes in UTF-8 format. ... Therefore, by default, when reading a text file, Get-Content r...
- 2PowerShell Studio tip: View and change file encoding
- 3UTF-8 - MDN Web Docs Glossary: Definitions of Web-related terms
- 4Changing source files encoding and some fun with PowerShell
Changing source files encoding and some fun with PowerShell ... At least it can correctly read te...
- 5Read UTF-8 files correctly with PowerShell - Stack Overflow
I need a function that can read any file with UTF-8 encoding, ignore and delete the BOM and not m...