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

x
?
Solved

Script to Copy and Rename an Excel file Between Network Shares

Posted on 2013-12-09
6
Medium Priority
?
377 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
[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
  • 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 41

Accepted Solution

by:
footech earned 2000 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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…

705 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