Solved

Powershell copy folder, attributes, and hidden files.

Posted on 2012-03-15
14
2,510 Views
Last Modified: 2012-03-23
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}
0
Comment
Question by:tomfontanilla
  • 6
  • 4
  • 4
14 Comments
 
LVL 10

Expert Comment

by:Alan_White
ID: 37728268
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).
0
 

Author Comment

by:tomfontanilla
ID: 37731696
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?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 37734854
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.
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 37737123
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
0
 

Author Comment

by:tomfontanilla
ID: 37740283
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?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 37741353
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.
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 37741531
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?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:tomfontanilla
ID: 37742029
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?
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 37742084
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.
0
 

Author Comment

by:tomfontanilla
ID: 37742115
"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.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 37743634
"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.
0
 

Author Comment

by:tomfontanilla
ID: 37744926
"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.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 37745102
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
  %date:~-4%%date:~-10,2%%date:~-7,2%.log
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.
0
 

Author Comment

by:tomfontanilla
ID: 37745227
Make sense. I will give a shot tomorrow morning. Thanks again.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now