Solved

Script to Copy and Rename an Excel file Between Network Shares

Posted on 2013-12-09
6
357 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
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
OfficeMate Freezes on login or does not load after login credentials are input.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

15 Experts available now in Live!

Get 1:1 Help Now