Powershell copy folder, attributes, and hidden files.

I would to create a powershell command to copy the  folder, attributes, and hidden files. After copying the  folder, attributes, and hidden files, next time the command run, it should only copy the new files, attributes, and hidden files for that folder. In addition, It should create logs progress info.

so far this is what I have.

$source = "C:\VeraSMART Data\Archives"
$filter = "*.arc"
$date = (get-date)
$destination = "\\file4\d$\eCASBackup\eCASBak"
$logProgress = 'c:\crmz3\logs\backupLog.txt'
$size = 0
$num = 0
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
$fileobj = $_
copy $fileobj.fullname -recurse $destination -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f yyyy-MM-dd)-Logfile.txt"
if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
$size += $fileobj.Length
$num += 1
echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
echo "Total size of files is:  $size" | out-file -append $logProgress
echo "Total num of folder is:  $num" | out-file -append $logProgress}
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you set on doing it in powershell?

I'm sure you could achieve what you want using Robocopy (http://technet.microsoft.com/en-us/library/cc733145%28v=ws.10%29.aspx).
tomfontanillaAuthor Commented:
I have xcopy already, here it is. What I need is something more robust.
xcopy D:\source Z:\destiation  /D /E /C /R /H /I /K /Y /J >C:\BackUpLog.txt

I need powershell script, since it's more powerful than xcopy. However, robocopy can be acceptable too. What switch would you recommended?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I, too, recommend using RoboCopy instead of a PS script, as it can do all what you request.
robocopy D:\Source Z:\Destination /Log+:C:\BackUpLog.txt /NP /E /Z /CopyAll /R:10 /W:1

Open in new window

I don't know what the /J in your xcopy line is for.
You can add several options, like /FP for logging full paths, /NDL and /NFL for no directory/file list, and many more.
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

The /J in xcopy is to copy unbuffered (to be used for very large files).

Depending on your requirements, you may want to investigate the /MIR option of robocopy. This stands for mirror.  It does what it says on the tin. A warning though, it will also remove files from the destination if the file is removed from the source.

It's interesitng that you say you wanted a more robust copy method, that's where robocopy got it's name: http://en.wikipedia.org/wiki/Robocopy
tomfontanillaAuthor Commented:
I agree to the both you. However, I was looking into robocopy switches, and I do not see an option to let me create another folder for each time I copy the file.

My goal is to set this through Task Scheduler every night. I want to copy the folder and create new folder every time to the destination. If this is not possible, at least overwrite the destination folder. Any thought?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That is something completely different. Your PS code does not use a new folder each time. And no, RoboCopy itself does not provide you an option for "archiving". It is mainly thought to provide exact copies, with or without following deletions.

But all you need is just to generate a new folder name each run, and use that one in RoboCopy, which will automatically create the folder. Something like
@echo off
set folder=%date:~-4%%date:~-10,2%%date:~-7,2%
robocopy C:\Source D:\Dest\%folder% ...

Open in new window

in a batch file, or using get-date in PS.
Agreed, you are not going to get that kind of functionality out of a standard copy utility like robocopy.  I've re-read the original question, can I check my understanding?

Day one
source folder has 3 file:  1.txt, 2.txt, 3.txt
Script runs, all file (and attributes) copied to destination folder.

day two (etc)
source folder now has 5 files: 1.txt, 2.txt, 4.txt, 5.txt, 6.txt
Script runs

What do you want to happen?

A subfolder of destination is created?  Or at same level?  With date appended?
Which files should be in the new folder?  4,5 and 6 or 1,2,4,5,6?
Should 3.txt be deleted from the original source folder?
tomfontanillaAuthor Commented:
I made some changes since you guys brought up logical thoughts.

So the goal is to copy the folder and each additional new files created to a new folder or overwrite existing folder. In addition, time stamps and sizes logs report for whatever was copied. I want to schedule a task scheduler to run every night at 8 P.M.
Any thought?
So you only want the "new" files in the source copied into a folder until the next time the script runs and then ditch the previous days "new" files?  

If so, not so easy.  Define a "new" file?  you would need to have a note off what files where there before so that you could see what was new.

You could do something involving the archive bit but that could be unreliable.

It seems like you do need a quite complex script.  If you want it in powershell, it's out of my league.
tomfontanillaAuthor Commented:
"If so, not so easy.  Define a "new" file?  you would need to have a note off what files where there before so that you could see what was new."

I wanted to copy the folder, including sub-folders, attributes, and hidden files. I want to overwirte the destanation folder or create new folder on the destanation.

I think this robocpoy is an Ideal, it's just that i'm not famliar of the switches. I'm working on a test script.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
"I want to overwirte the destanation folder or create new folder on the destanation."

That's a contradiction - you can only have one of both. For the latter you would need to rely either on time stamp (feasible with one day accuracy), archive bit or a reference folder.

Keeping a single backup folder is easy, RoboCopy provides all features for that already (with the switches I provided in my example). However, you will end with a single order, containing   updated copies of each file; deleted files of the source folder will be retained in the target folder, which can be intentionally or not.
tomfontanillaAuthor Commented:
"I want to overwirte the destanation folder or create new folder on the destanation."

What I meant to say is, as long as I get the current update, it does not matter about the destination folder (overwrite the new folder or simple update the folder with current files etc).

I tried your example, nut I do not see the time stamp.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If you use the same target folder each time, the only way to have a timestamp is either implicit, by looking at the timestamp of the newest file copied over, or explicit in a log file. The log file can either be created fresh each time (and get the timestamp in the name), or reused, and then having the timestamp logged in the file. Nothing of both is rocket science. E.g. using
as the logfile name (applied to the /LOG switch of robocopy in a cmd batch) would create one log per run.
Echoing that value into the logfile, and then using /LOG+:logfile.log with robocopy, would append.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomfontanillaAuthor Commented:
Make sense. I will give a shot tomorrow morning. Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.