Solved

Powershell copy script

Posted on 2014-10-22
8
376 Views
Last Modified: 2014-11-09
Hi

Can someone help me, I am trying to write a Powershell script that will do the following:-

1. Check certain files e.g bacs1.txt, bacs2.txt, bacs3.txt exist in a file location \\cslwinapp06\bacs, check that three files exist if not report, log error 'files don't exist'
2. if these three files exist in \\cslwinapp06\bacs copy these files to \\cslwinapp10\bacs
3. Check that these files have been copied to \\cslwinapp10\bacs and report a log to confirm 'files copied'

Thanks
0
Comment
Question by:rakkad
  • 4
  • 2
  • 2
8 Comments
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 40397940
$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -ErrorAction silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure" }
		
	}
	
	

Open in new window


Edited to include definition of $filed variable
0
 

Author Comment

by:rakkad
ID: 40397992
Can you explain how the code works and what is $filed variable ?

Thanks for your speedy response
0
 
LVL 28

Expert Comment

by:becraig
ID: 40398015
first we check the folder for any files matching bacs<anything>.txt
$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }

Then we  count how many files we found that match bacs*.txt (since you indicate you expect ONLY 3 we exit is more or less than 3 files are found matching the naming convention
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }

If ONLY 3 files are found matching the naming convention as you requested we copy them to the destination
else
{
We start by piping each file found into a for each loop
     $filer | % {
Then we create a variable name for the current file ($filed) so we have it for future use if needed
           $filed = $_.fullname
We now call copy item to copy from source to destination and use passthru so the value from copy is available as well as silently continue in the event we get an error
           $x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -ErrorAction silentlyContinue

Then if we get a value for $x we know the copy suceeded and we log it to a log file as requested
           if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
However if the copy fails we just write to screen that it failed
           else { "Copy failure" }
            
      }
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40398183
This could and should be improved. In particular I would expect to see the file which did not make it over to the destination. And there is leading space in the destination path which should be not there.
$logfile = 'C:\Temp\EE\Logfile.txt'
$filer = gci \\cslwinapp06\bacs\bacs[123].txt
if ($filer.count -ne 3) {
  Out-File $logfile -Append -Input "The expected number of files does not exist... exiting !!"
} else {
  $x = $filer | Copy-Item -Destination \\cslwinapp10\bacs -PassThru -EA silentlyContinue
  if ($x.Count -eq 3) {
     Write-Host "all files copied successfully"
  } else {
     $missing = 1..3 | ? { ($fiiler | select -Expand Name) -notcontains "bacs$_.txt" }
     Out-File $logfile -Append -Input "Error copying $missing"
}

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:becraig
ID: 40398199
I agree while the same could be accomplished by just utilizing the variable already added:
As for the overall script I can see a lot of ways to do what I think I would want it to so, however that would not be answering the "Specific" request posed in the question.


$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -EA silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure for $filed" | out-file c:\somefaillog.log -append  }
		
	}

Open in new window


I logged in both instances since it was what was requested.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40398225
@becraig:  " \\cslwinapp10\bacs"  - still wrong!
0
 
LVL 28

Expert Comment

by:becraig
ID: 40398230
@ Qlemo perfectly correct !
whitespace now  removed lol

$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination "\\cslwinapp10\bacs" -PassThru -EA silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure for $filed" | out-file c:\somefaillog.log -append  }
		
	}

Open in new window

0
 

Author Closing Comment

by:rakkad
ID: 40430959
Excellent
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

19 Experts available now in Live!

Get 1:1 Help Now