Solved

Powershell - Copy-Item Command

Posted on 2014-03-27
9
1,633 Views
Last Modified: 2014-03-28
Copy-Item -path c:\folder –destination \\server\share

Basic PS command, does exactly what it should. However the files that are being copied over daily are config files for a database we use on one of our Linux boxes. (This is presented through a Samba share)

However, I need it to copy files over nightly, but place them in subfolders based on the date they were copied.

Example:

c:/share/3272014
c:/share/3282014
etc.

Any suggestions on how to accomplish this would be most appreciated. Thanks.
0
Comment
Question by:victory2201
[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
  • 5
  • 4
9 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39959924
$date = Get-Date
$folder = $date.month + $date.day + $date.year
Copy-Item -path c:\folder –destination \\server\share\$folder

Open in new window

or
Copy-Item -path c:\folder –destination \\server\share\(Get-Date).ToString("Mdyyyy")

Open in new window

HTH,
Dan
0
 

Author Comment

by:victory2201
ID: 39961906
Dan,

Thank you for the assistance.

When I ran the top option, it created a file in the corresponding directory but not a folder containing all the files. (first attachment)

The second option just threw the error provided in the second attachment.
EX1.png
EX2.png
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39961951
Did you put the paths in ""? For ex "\\server\share 1\folder 1"
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:victory2201
ID: 39961966
Dan, made some progress. (See attachment)

Its creating the folder and copying the files, however the folder name is incorrect (2045).
EX3.png
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39961972
OK, just tested and this works:

$date=(Get-Date).ToString("Mdyyyy")
Copy-Item -path c:\folder –destination \\server\share\$date -Recurse

Open in new window

0
 

Author Comment

by:victory2201
ID: 39961968
No, I didn't put "" in the path.

Is that required? I don't recall having to do that unless there are spaces in the folder path.
0
 

Author Comment

by:victory2201
ID: 39961992
Dan,

That worked like a charm. Thank you very much for assistance with this question. =)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39961996
Glad I could help!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39962275
BTW, if you want a one-liner, this is a working version:
Copy-Item -path c:\folder –destination \\server\share\$((Get-Date).ToString("Mdyyyy")) -Recurse

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

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