Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy-item does not create folder to destination when copy one file #18828

Closed
5 tasks done
Petri-X opened this issue Dec 20, 2022 · 7 comments
Closed
5 tasks done

Copy-item does not create folder to destination when copy one file #18828

Petri-X opened this issue Dec 20, 2022 · 7 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module

Comments

@Petri-X
Copy link

Petri-X commented Dec 20, 2022

Prerequisites

Steps to reproduce

Hi,
When running the following command:
Copy-Item -Path .\test.txt -Destination .\TestFolder\

I do get the following error:
Copy-Item: The filename, directory name, or volume label syntax is incorrect. : 'C:\Temp\TestFolder\'

This is the same behavior on PowerShell and also Windows PS.

Expected behavior

Obviously I'm expecting that copy-item creates the folder as I asked and copy the file into it. And this is actually what it does when copy folder instead single file.

Actual behavior

The following error message:
`Copy-Item: The filename, directory name, or volume label syntax is incorrect. : 'C:\Temp\TestFolder\'`

And the file is not copied.

Error details

PS C:\Temp> get-error

Exception             :
    Type       : System.IO.IOException
    TargetSite :
        Name          : CopyFile
        DeclaringType : System.IO.FileSystem, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, Public
KeyToken=7cec85d7bea7798e
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : The filename, directory name, or volume label syntax is incorrect. : 'C:\Temp\TestFolder\'
    Source     : System.Private.CoreLib
    HResult    : -2147024773
    StackTrace :
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite)
   at Microsoft.PowerShell.Commands.FileSystemProvider.CopyFileInfoItem(FileInfo file, String destinationPath, Boolean
force, PowerShell ps)
TargetObject          : C:\Temp\test.txt
CategoryInfo          : WriteError: (C:\Temp\test.txt:FileInfo) [Copy-Item], IOException
FullyQualifiedErrorId : CopyFileInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
InvocationInfo        :
    MyCommand        : Copy-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 12
    Line             : Copy-Item -Path .\test.txt -Destination .\TestFolder\
    PositionMessage  : At line:1 char:1
                       + Copy-Item -Path .\test.txt -Destination .\TestFolder\
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Copy-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Environment data

PS C:\Temp> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

@Petri-X Petri-X added the Needs-Triage The issue is new and needs to be triaged by a work group. label Dec 20, 2022
@doctordns
Copy link
Contributor

Use -force to create the necessary folder structure.

@Petri-X
Copy link
Author

Petri-X commented Dec 20, 2022

Big thanks @doctordns for your propose, but:
PS C:\Temp> Copy-Item -Path .\test.txt -Destination .\TestFolder\ -Force
Still gives the following:
Copy-Item: The filename, directory name, or volume label syntax is incorrect. : 'C:\Temp\TestFolder\'

@mklement0
Copy link
Contributor

mklement0 commented Dec 20, 2022

  • Copy-Item currently doesn't support what you're trying to do, namely on-demand creation of the target directory. Its -Force parameter is unrelated to target-directory creation (unlike with New-Item -Type Directory -Force, see below).

  • The error message is confusing; the more sensible one - which you can provoke by appending a dummy file name to the path - would be "Could not find a part of the path 'C:\Temp\TestFolder'"

I agree that on-demand target-directory creation would be useful, but I suggest framing this as an enhancement that requires opt-in, and the syntax for that needs to be worked out.

In fact, this has come up before, in the context of #12805, where I've proposed an on-demand-directory-creation switch tentatively named -CreateOnDemand

The - cumbersome - workaround for now is:

# Note: New-Item -Type Directory -Force returns a directory-info object either for a preexisting directory 
# or one created on demand.
Copy-Item -Path .\test.txt -Destination (New-Item -Type Directory -Force .\TestFolder)

@StevenBucher98 StevenBucher98 added Issue-Enhancement the issue is more of a feature request than a bug WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module labels Dec 21, 2022
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

1 similar comment
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module
Projects
None yet
Development

No branches or pull requests

4 participants