[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1774
  • Last Modified:

Powershell - Copy-Item Command

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
victory2201
Asked:
victory2201
  • 5
  • 4
1 Solution
 
Dan CraciunIT ConsultantCommented:
$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
 
victory2201Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
Did you put the paths in ""? For ex "\\server\share 1\folder 1"
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
victory2201Author Commented:
Dan, made some progress. (See attachment)

Its creating the folder and copying the files, however the folder name is incorrect (2045).
EX3.png
0
 
Dan CraciunIT ConsultantCommented:
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
 
victory2201Author Commented:
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
 
victory2201Author Commented:
Dan,

That worked like a charm. Thank you very much for assistance with this question. =)
0
 
Dan CraciunIT ConsultantCommented:
Glad I could help!
0
 
Dan CraciunIT ConsultantCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now