Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell copy script

Posted on 2014-10-22
8
Medium Priority
?
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 2000 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 29

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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 
LVL 71

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
 
LVL 29

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 71

Expert Comment

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

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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)

721 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