Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3034
  • Last Modified:

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}
0
tomfontanilla
Asked:
tomfontanilla
  • 6
  • 4
  • 4
1 Solution
 
Alan_WhiteCommented:
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
 
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?
0
 
QlemoC++ DeveloperCommented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Alan_WhiteCommented:
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
 
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?
0
 
QlemoC++ DeveloperCommented:
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
 
Alan_WhiteCommented:
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
 
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?
0
 
Alan_WhiteCommented:
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
 
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.
0
 
QlemoC++ DeveloperCommented:
"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
 
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.
0
 
QlemoC++ DeveloperCommented:
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
 
tomfontanillaAuthor Commented:
Make sense. I will give a shot tomorrow morning. Thanks again.
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now