Powershell 2 copy-item which creates a folder if doesn't exist

前端 未结 9 2466
余生分开走
余生分开走 2020-12-05 12:55
$from = \"\\\\something\\1 XLS\\2010_04_22\\*\"
$to =  \"c:\\out\\1 XLS\\2010_04_22\\\"
copy-item $from $to -Recurse 

This works if c:\\out\\

9条回答
  •  眼角桃花
    2020-12-05 13:12

    I have stumbled here twice, and this last time was a unique situation and even though I ditch using copy-item I wanted to post the solution I used.

    Had a list of nothing but files with the full path and in majority of the case the files have no extensions. the -Recurse -Force option would not work for me so I ditched copy-item function and fell back to something like below using xcopy as I still wanted to keep it a one liner. Initially I tied with Robocopy but it is apparently looking for a file extension and since many of mine had no extension it considered it a directory.

    $filelist = @("C:\Somepath\test\location\here\file","C:\Somepath\test\location\here2\file2")
    
    $filelist | % { echo f | xcopy $_  $($_.Replace("somepath", "somepath_NEW")) }
    

    Hope it helps someone.

提交回复
热议问题