Solved

Script to Copy and Rename an Excel file Between Network Shares

Posted on 2013-12-09
6
367 Views
Last Modified: 2013-12-10
I have an XLS file that is updated once a day on a network share. The network share is \\company.com\Reports\Daily
The file name is a randomly generated file name with an *.xls extension.

I need a script that I can setup as a daily scheduled task that will:

-Take the *.xls file with the most recent last modified date located in: \\company.com\Reports\Daily folder
-Rename that file to the name: active.xls
-Copy that file to a location in: \\ServerName\Reports with the name Active.xls
(The order of bullet points 2 or 3 does not matter, the end result of the script just requires that I have the latest version of the *.xls file on the specific server with the name Active.xls)

The reason for this is I have an application at the destination server that calls for this specific file name, so in order to completely automate this process without having to manually rename the file every day, I would like this in a scheduled task script.

Thank you.
0
Comment
Question by:fireguy1125
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 39707057
Couple questions:

1. Will the task run once or multiple times during the day?
2. Is there a possiblity that the destination file (active.xls) be in use/open when the task runs?
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39707133
The following should work for you.  If the file is read-only you would want to add the -force switch to Get-ChildItem.  And if the destination file is locked you could run into an error.
Get-ChildItem \\company.com\Reports\Daily -filter *.xls | Sort -Descending -Property LastWriteTime | Select -First 1 | Copy-Item -Destination "\\company.com\Reports\active.xls" -force

Open in new window

0
 
LVL 1

Author Comment

by:fireguy1125
ID: 39707211
1. The task will run once per day, and there will not be more than one modified source file per day.
2. Destination file will not be in use.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 26

Expert Comment

by:pony10us
ID: 39707261
Are the source and destination mapped locations?
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 39707558
They are not mapped, but are shared and accessible via UNC paths as noted.
0
 
LVL 1

Author Closing Comment

by:fireguy1125
ID: 39709058
Works perfectly, thanks!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DCOM errors in Server 2016 2 38
Application Crash 2 23
How to create a textarea which saves text in HTML 8 37
Unable to add new user to AD 2 24
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
The viewer will learn how to dynamically set the form action using jQuery.

809 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