Avatar of tomfontanilla
tomfontanillaFlag for United States of America asked on

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}
else
{
$size += $fileobj.Length
$num += 1
echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
$size
$num
echo "Total size of files is:  $size" | out-file -append $logProgress
echo "Total num of folder is:  $num" | out-file -append $logProgress}
PowershellVB ScriptScripting Languages

Avatar of undefined
Last Comment
tomfontanilla

8/22/2022 - Mon
Alan_White

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).
ASKER
tomfontanilla

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

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Alan_White

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
ASKER
tomfontanilla

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

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alan_White

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?
ASKER
tomfontanilla

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?
Alan_White

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
tomfontanilla

"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

"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.
ASKER
tomfontanilla

"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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
tomfontanilla

Make sense. I will give a shot tomorrow morning. Thanks again.