PowerShell Kullanarak Dosyaya Veri Ekleme [ 4 Yol]


Metin Dosyasına Metin Eklemek

PowerShell kullanarak bir metin dosyasına içerik eklemek için çeşitli yaklaşımlarımız var. Örneğin, bilgileri aynı satıra, özel karakterli/özel karaktersiz yeni bir satıra, birden çok satıra ekleyebilir ve tablo içeriği gibi biçimlendirilmiş verileri ekleyebiliriz. Ancak olası çözümlere geçmeden önce bu bölümde aşağıda yer alan her yaklaşım için kullanacağımız dosya ve dosya içeriği hakkında bilgi sahibi olmak zorunludur. file.txt dosyası E:\Test dizininde bulunur.

file.txt Dosyasının içeriği:

This is the first line.

İmlecimizin aynı satırda, noktadan (.) hemen sonra olduğuna dikkat edin; bu, aynı satıra ekleme yapmamıza yardımcı olacaktır. Aşağıdaki çeşitli çözümleri keşfetmek için bu dosyayı kullanalım.

Out-File Cmdlet'ini kullanın

İçeriği file.txt dosyasında aynı satıra eklemek için Out-File cmdlet'ini -Append parametresiyle birlikte kullanın.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.

İçeriği file.txt dosyasındaki yeni satıra eklemek için Out-File cmdlet'ini -Append parametresiyle birlikte kullanın.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.

Yukarıdaki iki örnekte herhangi bir benzerlik buldunuz mu? Evet! Neden böyle? İlk örnekte -Append ilk kez kullanıldığından, imlecimiz aynı satırdayken içeriği aynı satıra ekledi ancak dosyanın sonuna da yeni bir satır karakteri ekledi. . Artık imleç yeni bir satırdadır; bu nedenle ikinci kod örneğinde -Append içeriği yeni bir satıra ekledi ancak aynı zamanda dosyanın sonuna yeni bir satır karakteri de ekledi. -Append parametresinin dosyanın sonuna otomatik olarak yeni bir satır karakteri eklediğini unutmayın; bu nedenle içerik ilk örnek dışında her seferinde yeni satıra eklenir.

file.txt dosyasına özel karakterler (sekme gibi) içeren içerik eklemek için -Append parametresiyle Out-File cmdlet'ini kullanın. > dosya.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

file.txt dosyasına birden fazla satır eklemek için Out-File cmdlet'ini -Append parametresiyle birlikte kullanın.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

File.txt dosyasına tablolu içerik (biçimlendirilmiş içerik) eklemek için -Append parametresiyle Out-File cmdlet'ini kullanın.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

file.txt dosyasına tek ve çift tırnak içeren bir satır eklemek için Out-File cmdlet'ini kullanın.

Dosya Dışı Cmdlet'i Kullan:

$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double quotes`"."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the eighth line having 'single quote' and "double quotes".

Out-File cmdlet'i çıktıyı belirli bir dosyaya göndermek için kullanılır. Bu cmdlet, bir dosyaya yazmak için PowerShell'in biçimlendirme sistemini örtülü olarak kullanır. Bu dosyanın, terminalde sahip olduğumuz tam ekran gösterimini alacağını unutmayın; bu, çıktının yalnızca, çıktıda yer alan her girdi nesnesinin bir dize olması durumunda programlı işleme için yeterince iyi olabileceği anlamına gelir. Bu cmdlet'i çıktıyı ($content) file.txt dosyasına göndermek için kullandık.

Yukarıdaki örneklerde $filePath ve $content adında iki değişken oluşturduk. $filePath, file.txt dosyasının yolunu içerirken, $content eklemek istediğimiz asıl içeriği içeriyordu. Get-Content cmdlet'i, file.txt dosyasının tüm verilerini (içerik) almak ve terminalde görüntülemek için kullanıldı; bu, güncellenmiş içeriği görmek için faydalı oldu file.txt dosyasını açmadan.

Tek fark, gereksinimlere göre Out-File cmdlet'ini nasıl kullandığımızdı. -FilePath, -InputObject ve -Append parametreleri tüm örneklerde aynıydı. Peki bu parametreler ne anlama geliyor ve bunları neden kullandık?

-FilePath $filePath'i belirtmek için kullanıldı, -InputObject istediğimiz $content'tan bahsetmek için kullanıldı eklemek için ve file.txt dosyasının sonuna $content eklemek için -Append kullanıldı. Hedef dosyanın kodlama türünü belirtmek için -Encoding parametresini de kullanabilirsiniz. Genellikle UTF8 kodlaması kullanılırken utf8NoBOMvarsayılan değeridir. İhtiyaçlarımıza göre kullanabileceğimiz başka kodlamalarımız da var.

Şimdi önemli olan içeriğin nasıl biçimlendirildiğidir. Her örneğin $content değişkeni başlatılırken yapılır. Birinci ve ikinci kod parçacıklarındaki $content değişkenini basit bir metinle ayarladık. Üçüncü örnekte, metni bir sekmesinden sonra eklemek için $content'i, önünde `t (özel karakter) bulunan bir metinle başlattık.

Benzer şekilde, dördüncü örnekte, her dizenin dosyaya aynı anda eklediğimiz bir metin satırı olduğu bir dize dizisi oluşturmak için @() ile temsil edilen bir dizi operatörünü kullandık. Son olarak, beşinci örnekte, verileri tablo biçiminde eklemek için özel karakterlerle birlikte @() kullandık; bu özel karakterler bir yeni satır karakteri ve bir sekme karakteri içerir. Aşağıda açıklamalarıyla birlikte farklı özel karakterleri bulabilirsiniz.

Special Character

Tanım

`0

Hükümsüz

`a

Uyarı

`b

Geri tuşu

`n

Yeni satır

`r

Taşıma İadesi

`t

Yatay Sekme

`'

Tek Alıntı

`"

Çift Alıntı

Bu bölümün yedinci ve son örneğinde $content değerini başlatırken tek ve çift tırnak kullandık. Yukarıdaki tabloda gösterildiği gibi, her bir alıntının (tekli ve çiftli) önünde bir geri tırnak işaretinin bulunduğunu unutmayın.

Bizim durumumuzda file.txt olan dosya mevcut değilse, Out-File cmdlet'i onu oluşturacak ve geri kalan işlemleri gerçekleştirecektir.

İçerik Ekle Cmdlet'ini kullanın

İçeriği file.txt dosyasında aynı satıra eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.

File.txt dosyasındaki yeni satıra içerik eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.

file.txt dosyasına özel karakterler (örneğin sekme) içeren içerik eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

file.txt dosyasına birden çok satır eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

file.txt dosyasına tablolu içerik (biçimlendirilmiş içerik) eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

file.txt dosyasına tek ve çift tırnak içeren bir satır eklemek için İçerik Ekle cmdlet'ini kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double qoutes`"."
Add-Content -Path $filePath -value $content
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the eighth line having 'single quote' and "double quotes".

Verilen dosyaya içerik eklemek için İçerik Ekle'yi kullandık. Bu cmdlet'i doğrudan komuttaki metni belirterek kullanabiliriz veya bir nesneye kaydedip Add-Content cmdlet'ini kullanırken o nesneyi kullanabiliriz. Örneğin içeriği $content değişkeninde sakladık ve bu cmdlet'i kullanırken o değişkeni kullandık; yukarıdaki kod çitlerine bakın.

Bu bölümün örnekleri Out-File cmdlet'ini kullananlara benzer. Yine, dosya yolunu ve içeriğini içerecek şekilde $filePath ve $content'ı tanımladık ve başlattık. Get-Content, file.txt dosyasından içerik almak ve onu terminalde göstermek için kullanıldı. $content değişkeninde saklanan içerik, önceki bölümde Out-File cmdlet'ini kullanırken yaptığımız gibi biçimlendirildi; buna başvurabilirsiniz.

Bu bölüme ilişkin tüm örneklerde, $filePath'i belirtmek için Add-Content cmdlet'iyle birlikte -Path ve -value parametrelerini kullandık ve $content.

İçerik Ekle, halihazırda mevcut değilse, belirtilen adla metin dosyası oluşturacaktır. Yol belirtilmezse metin dosyası geçerli dizinde oluşturulacaktır.

Yönlendirme (>>) Operatörünü Kullan

file.txt dosyasında aynı satıra içerik eklemek için >> operatörünü kullanın.

Yönlendirme Operatörünü Kullan:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.

file.txt dosyasındaki yeni satıra içerik eklemek için >> operatörünü kullanın.

Yönlendirme Operatörünü Kullan:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.

file.txt dosyasına özel karakterler (örneğin sekme) içeren içerik eklemek için >> operatörünü kullanın.

Yönlendirme Operatörünü Kullan:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

file.txt dosyasına birden çok satır eklemek için>> cmdlet'ini kullanın.

Yönlendirme Operatörünü Kullan:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

file.txt dosyasına tablo içeriği (biçimlendirilmiş içerik) eklemek için >> operatörünü kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

file.txt dosyasına tek ve çift tırnak içeren bir satır eklemek için >> operatörünü kullanın.

İçerik Ekleme Cmdlet'ini kullanın:

$filePath = "E:\Test\file.txt"
$content = "This is the seventh line having `'single quote`' and `"double quotes`"."
$content>>$filePath
Get-Content $filePath

ÇIKTI:

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the seventh line having 'single quote' and "double quotes".

Bu örnekler, cmdlet'ler ve değişkenler önceki bölümlerdekilerle aynıdır ancak file.txt dosyasını eklemek için yeniden yönlendirme operatörünü kullandık.

Yeniden yönlendirme operatörü, eğer mevcut değilse, metin dosyasını belirtilen dizinde (veya belirtilmemişse geçerli dizinde) oluşturacaktır.

Bir Metin Dosyasını Başka Bir Metin Dosyasına Ekleme

Bir metin dosyasının içeriğini PowerShell'deki başka bir metin dosyasına eklemek için İçerik Ekle'yi kullanın.

file1.txt Dosyasının içeriği:

This is the file1 line.

file2.txt Dosyasının içeriği:

This is the file2 line.

İçerik Ekleme Cmdlet'ini kullanın:

$sourceFile = "E:\Test\file2.txt"
$destinationFile = "E:\Test\file1.txt"
$sourceFileContent = Get-Content -Path $sourceFile
Add-Content -Path $destinationFile -value $sourceFileContent 
Get-Content $destinationFile

ÇIKIŞ:

This is the file1 line.This is the file2 line.

Bunların hepsi PowerShell kullanarak dosyaya nasıl veri ekleneceğiyle ilgili.

İlgili makaleler