0

I have problem with path. I need to copy all files from directory I add as 1st parameter in Powershell command.

Now I have:

Copy-Item -Path "$args[0]/" -Filter*.*

So it will copy to location I am now (and it's ok, I don't want other location) but it doesn't copy anything. Please help.

Roberto
  • 315
  • 1
  • 5
  • 18
  • 3
    please add more code, for example the function this is called in – Paul Oct 21 '14 at 20:17
  • 2
    Based strictly on the code that you have listed you can not copy things because copying an item requires both SOURCE and DESTINATION paths to be supplied in some form. You provide one of the two, and need to supply the other. Per the syntax you use the path you have supplied is the source. You would need to add the -destination argument to specify where the files should be copied to. – TheMadTechnician Oct 21 '14 at 20:37
  • But what if I want to copy it to location I am now? I change the set-location and I still need to add Destination path? – Roberto Oct 21 '14 at 20:43
  • According to [TechNet](http://technet.microsoft.com/en-us/library/hh849793.aspx) `-Destination` is optional. What are you passing as `$args[0]` for example. Please show a code sample – Matt Oct 21 '14 at 20:49
  • C:\Users\Administrator\Desktop\test.ps1 C:\TEST And in directory C:\TEST I've got some files which I want to copy to my current location. But with my current code, it copies folder TEST (without files in) and I want only files from TEST. – Roberto Oct 21 '14 at 20:59

2 Answers2

2

Pretty sure the issue is that the 0 element in $args is not expanding in the string creating an invalid path. Wrap the variable in $() to allow it to expand inside a double quoted string. Else you would end up trying to copy the folder C:\TEST[0]/ which obviously is not correct.

Copy-Item -Path "$($args[0])/" -Filter *.* -Recurse

Not yet sure why you have a forward slash in there since Windows pathing uses backslashes.

function Copy-Something(){
    test-Path "$($args[0])/"
    test-path "$($args[0])"
}

Copy-Something C:\temp

With what little you have provided the output shows that it might be redundant to have the slash there. Would also recommend calling Test-Path on the argument anyway as it might have caught this for you.

True
True

From Question Comment

You are looking for the -Recurse parameter if you also want folder contents.

From Answer Comment

If you want the contents and not the folder you should be able to do something like this:

Copy-Item -Path "$($args[0])\*" -Filter *.* -Recurse
Matt
  • 45,022
  • 8
  • 78
  • 119
  • So now I use: C:\Users\Administrator\Desktop\test.ps1 C:\TEST and i code I have: Copy-Item -Path "$($args[0])" -Filter*.* -Recurse BUT it copies directory TEST with files in it. I want only files (without directory TEST). How to do it ? Thanks for help. – Roberto Oct 21 '14 at 21:16
  • @Roberto look at the bottom of my answer – Matt Oct 21 '14 at 21:27
0

When in doubt, Get-Help Command.

PS C:\> Get-Help Copy-Item -ShowWindow

-------------------------- EXAMPLE 3 --------------------------
This command copies the contents of the C:\Logfiles directory
to the C:\Drawings\Logs directory. It creates the \Logs
subdirectory if it does not already exist.

Windows PowerShell
PS C:\> Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse
evilSnobu
  • 24,582
  • 8
  • 41
  • 71