Controlling copies

I was recently asked about ‘forcing’ a copy so that only the files you want are copied

Consider a folder with lots of files. I want to copy those files that start with the letter t and have a txt extension

PS> Get-ChildItem -Path C:\test2\t*.txt

    Directory: C:\test2

Mode                LastWriteTime         Length Name
—-                ————-         —— —-
-a—-       08/08/2015     14:14              0 test.txt
-a—-       08/08/2015     14:16              0 test1.txt
-a—-       08/08/2015     14:17              0 test2.txt
-a—-       08/08/2015     14:17              0 test3.txt
-a—-       08/08/2015     14:17              0 test4.txt

I want to copy to c:\test1 which is empty

PS> Get-ChildItem -Path C:\test1\
PS>

Defining the copy is simply a matter of setting the path to files you want

PS> Copy-Item -Path C:\Test2\t*.txt -Destination C:\test1\ -Verbose
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test.txt Destination: C:\test1\test.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test1.txt Destination: C:\test1\test1.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test2.txt Destination: C:\test1\test2.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test3.txt Destination: C:\test1\test3.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test4.txt Destination: C:\test1\test4.txt”.
PS> Get-ChildItem -Path C:\test1\

    Directory: C:\test1

Mode                LastWriteTime         Length Name
—-                ————-         —— —-
-a—-       08/08/2015     14:14              0 test.txt
-a—-       08/08/2015     14:16              0 test1.txt
-a—-       08/08/2015     14:17              0 test2.txt
-a—-       08/08/2015     14:17              0 test3.txt
-a—-       08/08/2015     14:17              0 test4.txt

Advertisements
This entry was posted in Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s