Solved

Script to Copy and Rename an Excel file Between Network Shares

Posted on 2013-12-09
6
361 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

914 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

23 Experts available now in Live!

Get 1:1 Help Now