question

IvayloStefanov avatar image
0 Votes"
IvayloStefanov asked IanXue-MSFT commented

Copy Some File from Directory

Hallo,

i need a script that muss copy some file from one directory to another directory. All files in the directory have a *.txt extension. I only have to choose one of them. For example, I have the following files:
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
etc.
I have defined the directory where the files are located as a variable and also the directory where I need to save the files.
In my script I only need to select some files to be copied (not all of them). For example:
2.txt, 4.txt, 6.txt
Below is some of the code I wrote:

 Function CopyTxt {
         $pathtxt = "C:\Users\admin\Desktop\update\"
            
         $newtxt = "E:\User\txt\"
            
          
         Copy-Item -Path $pathtxt -Destination $newtxt -Verbose
            
            
 }

I need to select in "-Path $pathtxt " only some txt files. In $pathtxt are located all txt files.

Thank you advance for help.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered IanXue-MSFT commented

Hi IanXue-MSFT,

Thank you for answer. I need to test and if work, I "Accept Answer".

Best Regards,
Ivaylo

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please help to accept the right answer so that it could be found more easily.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

You can try something like below.

 Function CopyTxt {
     $pathtxt = "C:\Users\admin\Desktop\update\"            
     $newtxt = "E:\User\txt\"
     $files = @("2.txt","4.txt","6.txt")  
     Get-ChildItem -Path $pathtxt | Where-Object {$_.Name -in $files} | Copy-Item -Destination $newtxt -Verbose
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.