I use the Get-Childitem already. gci is a short hand for that. I don't have an issue with getting the child folders, I have an issue getting the script to copy the new files in each directory. Thanks.
Backup Script
I am trying to write a script that will go through each folder in a directory and copy only the newest files that have been created since the last day the backup was ran. I am able to make this happen with the following script:
$newfile = (get-date).AddDays(-4)
$ChildFolders = @('path1','path2','etc')
for($i= 0; $i -lt $ChildFolders.Count; $i++){
$folderPath = "\SampleNetworkPath"
$DestinationPath = "\SampleDestinationPath"
gci -Path $folderPath -File |Where-Object { !$_.PSIsContainer -and $_.CreationTime -gt $newfile} |%($_){
$_.FullName
Copy-Item $_.FullName -Destination $DestinationPath}
}
This issue is the $ChildFolders variable. I have to add the path that I want the files to be pulled from. I would like to be able to have it automatically go through each folder and sub-folder, find any files that have a newer creation time, and copy them over to the same path they came from. The only difference between the original path and destination path are the ip address of the machine they are stored on. I have a few place I need to back up that have thousands of sub-folders, so listing them individually isn't really viable. Also, sometimes new sub-folders are added. While its not an issue for me to add the sub-folder to the code, other people will occasionally run the script, and they don't know how to do that.
Windows for home | Other | Apps
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
2 answers
Sort by: Most helpful
-
Anonymous
2018-05-01T23:40:35+00:00 -
Anonymous
2018-05-01T23:24:06+00:00 You could get the folders at current location for childfolder variable with Get-ChildItem.