Translating copy command from CMD to Powershell Copy-Item

Asked

Viewed 476 times

3

I’m trying to translate a command from to the but I’m not getting it.

I get multiple.txt files during the month, put them all in the same folder, some have specific terms in the name I use to select and concatenate the similar ones.

But at the end of the month, I concatenate all in a single file to work with all at once, this concatenation process in CMD I performed as follows:

'copy zucchini.txt Todasabobrinhas.txt'

Translating:

  • copy - command that makes the copy;

  • zucchini.txt - selects all files with the term "zucchini" in the name to concatenate; Todasabobrinhas.txt - final file with all concatenated files.

When trying to use this command on it executes but does not generate the expected file and also gives no error message.

Can anyone give me a hint to make this process on Powershell?

Thank you for your attention.

2 answers

4

Mostrando a utilização do script How your goal is to copy the contents of the files *abobrinha*.txt already concatenating in another file with name All the little things.txt, I believe the steps to achieving your goal are:

  1. Grabs the contents of the files by filtering TodasAbobrinhas.txt which will be generated with the contents of the first, so it will exist during execution, but is avoided:

    Get-ChildItem *abobrinha*.txt -Exclude TodasAbobrinhas.txt
    
  2. Take the contents of each item/file keeping the character codes:

    Foreach-Object {Get-Content $_ -Raw}
    
  3. Moves the respective contents of the files to the pointed output file:

    Out-File TodasAbobrinhas.txt
    

 Get-ChildItem *abobrinha*.txt -Exclude TodasAbobrinhas.txt|Foreach-Object {Get-Content $_ -Raw}| Out-File TodasAbobrinhas.txt
cat *abobrinha*.txt -E TodasAbobrinhas.txt | sc TodasAbobrinhas.txt
  • Obs1.: -E == -Exclude : is the same

  • Obs2.: Notes from Operator SO/EN @Gishu:

Note 1: The cat is a alias for Get-Content, and sc is a alias to the Set-Content.

Note 2: The output to a file with > does not preserve the character encoding! That’s why the use of Set-Content (sc) is recommended.

2


Try it like this in Powershell

copy-item *abobrinha*.txt -destination todasabobrinhas.txt
  • Does not work, it executes the command but no result appears...

  • but seeing here, I think the way is this, it generates the file but with the contents of only one file...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.