{"id":155,"date":"2023-10-02T16:20:50","date_gmt":"2023-10-02T19:20:50","guid":{"rendered":"https:\/\/difusao.tech\/blog\/?p=155"},"modified":"2023-11-06T19:15:33","modified_gmt":"2023-11-06T22:15:33","slug":"comandos-powershell-para-criacao-e-manipulacao-de-arquivos","status":"publish","type":"post","link":"https:\/\/difusao.tech\/blog\/comandos-powershell-para-criacao-e-manipulacao-de-arquivos\/","title":{"rendered":"Comandos PowerShell para Cria\u00e7\u00e3o e Manipula\u00e7\u00e3o de arquivos"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"380\" src=\"https:\/\/difusao.tech\/blog\/wp-content\/uploads\/2023\/10\/image-1024x380.png\" alt=\"\" class=\"wp-image-156\" srcset=\"https:\/\/difusao.tech\/blog\/wp-content\/uploads\/2023\/10\/image-1024x380.png 1024w, https:\/\/difusao.tech\/blog\/wp-content\/uploads\/2023\/10\/image-300x111.png 300w, https:\/\/difusao.tech\/blog\/wp-content\/uploads\/2023\/10\/image-768x285.png 768w, https:\/\/difusao.tech\/blog\/wp-content\/uploads\/2023\/10\/image.png 1252w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>O PowerShell \u00e9 uma solu\u00e7\u00e3o de automa\u00e7\u00e3o de tarefas multiplataforma que consiste em um shell de linha de comando, em uma linguagem de script e uma estrutura de gerenciamento de configura\u00e7\u00e3o. O PowerShell pode ser executado no Windows, Linux e macOS.<\/p>\n\n\n\n<p>O PowerShell \u00e9 um shell de comando moderno que inclui os melhores recursos de outros shells populares. Ao contr\u00e1rio da maioria dos shells que s\u00f3 aceita e retorna texto, o PowerShell aceita e retorna objetos .NET. O shell inclui os seguintes recursos:<\/p>\n\n\n\n<p>Hist\u00f3rico de linha de comando robusto<br>Previs\u00e3o de comando e conclus\u00e3o da guia (confira about_PSReadLine)<br>Suporte a aliases de par\u00e2metro e de comando<br>Pipeline para comandos de encadeamento<br>Sistema de ajuda no console, semelhante a p\u00e1ginas man UNIX<\/p>\n\n\n\n<p>Como uma linguagem de script, o PowerShell \u00e9 normalmente usado para automatizar o gerenciamento de sistemas. Ele tamb\u00e9m \u00e9 usado para compilar, testar e implantar solu\u00e7\u00f5es, geralmente em ambientes de CI\/CD.<\/p>\n\n\n\n<p>Defini\u00e7\u00e3o acima e mais informa\u00e7\u00f5es: https:\/\/learn.microsoft.com\/pt-br\/powershell\/scripting\/overview?view=powershell-7.3<\/p>\n\n\n\n<p>Agora vamos ao que interessa.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Criar arquivo:<\/strong><\/p>\n\n\n\n<p><mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">New-Item -Path &#8220;c:\\temp\\test.txt&#8221; -ItemType File<\/mark><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Verificar se o arquivo existe:<\/strong><\/p>\n\n\n\n<p><mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">Test-Path -Path &#8220;c:\\temp\\test.txt&#8221;<\/mark><\/p>\n\n\n\n<p>O resultado do teste \u00e9 um boleano (True se o arquivo existir e False caso n\u00e3o exista).<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Criar outro arquivo caso o arquivo j\u00e1 exista. Utilizaremos o if.<\/strong><\/p>\n\n\n\n<p><mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">if (-not (Test-Path -Path &#8220;c:\\temp\\test.txt&#8221;)){<br>New-Item -Path &#8220;c:\\temp\\test.txt&#8221; -ItemType File<br>}else{<br>New-Item -Path &#8220;c:\\temp\\test2.txt&#8221; -ItemType File<br>}<\/mark><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Escrevendo no documento criado:<\/strong><\/p>\n\n\n\n<p><mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">$file = &#8220;c:\\temp\\test.txt&#8221; <br>$contenFile = &#8220;Meu conteudo&#8221; <br>Set-Content -Path $file -Value $contentFile -Force <\/mark><\/p>\n\n\n\n<p>Explica\u00e7\u00e3o das linhas acima:<\/p>\n\n\n\n<p><strong>$file = &#8220;c:\\temp\\test.txt&#8221;<\/strong> # Coloca o caminho do arquivo na vari\u00e1vel file.<br><strong>$contenFile = &#8220;Meu conteudo&#8221;<\/strong> # Coloca o conte\u00fado a ser inserido no arquivo dentro da vari\u00e1vel contentFile<br><strong>Set-Content -Path $file -Value $contentFile -Force<\/strong> # Seta o conte\u00fado da vari\u00e1vel contentFile dentro do caminho indicado pela variavel file<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Ler o conte\u00fado do arquivo e mostrar na sa\u00edda padr\u00e3o (na linha de comando do powershell):<\/strong><\/p>\n\n\n\n<p><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">Get-Content &#8220;c:\\temp\\test.txt&#8221;<\/mark><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Um dia vou editar este post e colocar uns prints. rs<\/p>\n\n\n\n<p>At\u00e9 a pr\u00f3xima.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>O PowerShell \u00e9 uma solu\u00e7\u00e3o de automa\u00e7\u00e3o de tarefas multiplataforma que consiste em um shell de linha de<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,188,30],"tags":[67,190],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-aplicativos-windows","category-powershell","category-programacao-e-desenvolvimento","tag-cmd","tag-windows-powershell"],"_links":{"self":[{"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":2,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/posts\/155\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/difusao.tech\/blog\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}