简体   繁体   中英

PS script copy new files

Pretty noob at writing PS scripts - wrote this up and have been actively using it although still requires some manual intervention trying to achieve my goal, which I would like to automate completely.

I will try my best to explain clearly;

I am trying to copy '.bak' files to a specific directory from a source folder that has files dropped in it on a daily basis. Problem is the way I created the script, every time it runs it creates a new folder with some of the same files as previously copied.

The files being copied all follow the same name structure in date sequence;

xxxx_2018_01_01_2131231.bak
xxxx_2018_01_02_2133212.bak  
xxxx_2018_01_03_2199531.bak

How could I write the script so that it copies newer files only and not what has already been copied previously?

It would also be nice to only create a new folder then a certain part of the file name changes.

Here is the script;

$basedir = "Path:\path"  
$today = (Get-Date).ToString('MM_dd_yy')  
$Filter = '*.bak'  
$location = New-Item -Path $basedir -Type Directory -Name $today  

Copy-Item -Path 'Path:\path' -Destination $location -Filter $Filter -Recurse

Any pointers are greatly appreciated! Thanks in advance for your help!

I'm not sure if there is an easy way to code this, but the general answer would be using the Get-ChildItem cmdlet.

"The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the -Recurse parameter to get items in all child containers and use the -Depth parameter to limit the number of levels to recurse."

By using the Get-ChildItem, you could get the listing of files that are in both directories, and then compare them to see if they have the same name. Then build an if() argument based on criteria you wish to use to compare them.

It's not the complete answer, but it is a good starting point.

Thanks everyone for pitching in, much appreciated!

I have switched over to the batch file route and have created the following to accomplish my goal;

@echo off
setlocal

set _source="C:\users\user1\desktop\source"
set _dest="C:\users\user1\desktop\dest"

robocopy %_source% %_dest% *.bak /mir /XC /XN /XO

Any opinion on this script is encouraged!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM