Move-Item not moving top folder in some cases - powershell

Trying to move all files and folders and how its behaving has me a little stumped.
Let's say we have a folder tree like Below
Example Path
Folder A
FileA1
FileA2
Folder B
FileB1
When using the Move-Item command like this move-Item -Path 'C:\Example Path\*' -Destination 'C:\Destination Path' every time the first folder in the list just seems to get deleted. So the outcome of that command would be like this.
Destination Path
FileA1
FileA2
Folder B
FileB1
How would I get around this issue, Or am I doing something incorrectly here? Any help would be gladly appreciated.

i try this on my pc, it's working (PowerShell v5)
move-item -Path "C:\temp\Example Path" -Destination "C:\temp\Destination Path"

Try creating the destination folder manually before running the Move-Item command.
New-Item -ItemType directory -Path 'C:\Destination Path'
Move-Item -Path 'C:\Example Path\*' -Destination 'C:\Destination Path'
https://github.com/PowerShell/PowerShell/issues/13352#issuecomment-669025179

Related

Issue in Copy-Item in powershell

I used the Copy-Item command to copy a set reports to a server location. It was working fine before but recently more reports have been added to the folder and now most of the times it works fine but in some cases it shows the error:
The target file "$destination" is a directory, not a file
The code I used is:
Copy-Item -Path "$Source" -Destination "$destination" -Recurse -Force
I am not sure why I am not getting this error for every case.
What you have should work but this may work around the issue if there is some limitation with certain shared folder destinations:
Note: Remove the -WhatIf once you confirm the changes it will make are correct. Currently it will only output what files would have been copied.
Get-ChildItem $Source | Foreach-Object {
Copy-Item $_.FullName $destination -Recurse -Force -WhatIf
}
Basically, this enumerates all of the files and folders under the $Source directory, then copies the files or folders individually to the destination.
If you are able to offer the values of $Source and $Destination I or another might be able to offer a solution to your problem, rather than a workaround.

What am I doing wrong with Copy-Item?

Copy-Item trips me up every time I use it. Would someone please tell me what I did wrong here?
I have a set of folders:
C:\FolderA\Thing\Sub1\1_file.css
C:\FolderA\Thing\Sub2\A_file.css
C:\FolderA\Thing\page.html
I try to use Copy-Item to send them to C:\FolderB\* and preserve structure:
Copy-Item -Path $srcFolder\Thing\* -Destination $destFolder\Thing -Recurse -Force -Verbose
Expected result:
C:\FolderB\Thing\Sub1\1_file.css
C:\FolderB\Thing\Sub2\A_file.css
C:\FolderB\Thing\page.html
Instead I end up with:
C:\FolderB\Sub1\1_file.css
C:\FolderB\Sub2\A_file.css
C:\FolderB\page.html
There's clearly a \Thing folder in there, so why doesn't it create the \Thing if I'm telling it to go recursively?
You need to tell it to create the directory before the copy, for example:
$dst = C:\FolderB\Thing
New-item $dst -type directory
`

Powershell: Why is recurse not working?

Hi I have a script that should ideally copy an item and paste it onto a destination folder including all of it's sub folders. However I cannot get it to copy the item into the subfolders.
Here is the code:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Recurse -Force
Please help!
Recurse copies the subfolders and item from the source to the destination, preserving directory structure. It doesn't recurse in the destination and make multiple copies in the destination. You could use a ForEach-Object loop from the output of Get-ChildItem where you retrieve all of the folders you wish to copy to.
Get-ChildItem "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Directory -Recurse | % {
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination $_.fullname -Force }
Two things from my observation.
1)You are telling you are copying an item including its sub-folders, but in the source you have mentioned one file name with extension. not sure if thats the folder name in your case.
2) I found that in the destination you are not going inside the final level folder.
So put this and check:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\*.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018\" -Recurse -Force
Note: If its a specific file, then you can remove the wildcard because that will copy all the files with the extension .xlsm . I have kept it because I am assuming you have only one file under that folder.
See the Documentation and the issue with PS:
Copy-Item Nature & Issue

Remove-Item : Cannot find path '\\footfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does not exist

I am trying to remove all files and folders in a specific network path location (the tfs build drop folder of one of my builds).
In my post-build script I tried the following lines: (with same results)
$Destination = '\\footfs32\Builds\TestBuildTest'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
Remove-Item \\footfs32\Builds\TestBuildTest -Force -Recurse
Remove-Item "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
Remove-Item -LiteralPath "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
All included folders are being removed, but when it tries to remove the files in the dir, the following error pops ups:
Remove-Item : Cannot find path
'\abctfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does
not exist.'
Why is this error popping up on files only, while all folders are deleted correctly? This doesn't make any sense to me. And how do I fix it?
According to the error info.Very likely the file had been deleted in your build process.
Some events triggered during your build process, and that file had been deleted. When running the powershell script, those files couldn't be found. The Remove-item command Can not delete a file twice.
Please double check your build definition, build process and drop folder.
Please try the following command:
Remove-Item -Path "\\footfs32\Builds\TestBuildTest\*.*" -Force -Recurse

Should Copy-Item create the destination directory structure?

I'm trying to copy a file to a new location, maintaining directory structure.
$source = "c:\some\path\to\a\file.txt"
destination = "c:\a\more\different\path\to\the\file.txt"
Copy-Item $source $destination -Force -Recurse
But I get a DirectoryNotFoundException:
Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt'
The -recurse option only creates a destination folder structure if the source is a directory. When the source is a file, Copy-Item expects the destination to be a file or directory that already exists. Here are a couple ways you can work around that.
Option 1: Copy directories instead of files
$source = "c:\some\path\to\a\dir"; $destination = "c:\a\different\dir"
# No -force is required here, -recurse alone will do
Copy-Item $source $destination -Recurse
Option 2: 'Touch' the file first and then overwrite it
$source = "c:\some\path\to\a\file.txt"; $destination = "c:\a\different\file.txt"
# Create the folder structure and empty destination file, similar to
# the Unix 'touch' command
New-Item -ItemType File -Path $destination -Force
Copy-Item $source $destination -Force
Alternatively, with PS3.0 onwards, you can simply use the New-Item to create the target folder directly, without having to create a "dummy" file, e.g. ...
New-Item -Type dir \\target\1\2\3\4\5
...will happily create the \\target\1\2\3\4\5 structure irrespective of how much of it already exists.
Here's a oneliner to do this. Split-Path retrieves the parent folder, New-Item creates it and then Copy-Item copies the file. Please note that the destination file will have the same filename as the source file. Also, this won't work if you need to copy multiple files to the same folder as with the second file you'll get An item with the specified name <destination direcory name> already exists error.
Copy-Item $source -Destination (New-Item -Path (Split-Path -Path $destination) -Type Directory)
I had files in a single folder in Windows 7 that I wanted to rename and copy to nonexistent folders.
I used the following PowerShell script, which defines a Copy-New-Item function as a wrapper for the Test-Item, New-Item, and Copy-Item cmdlets:
function Copy-New-Item {
$SourceFilePath = $args[0]
$DestinationFilePath = $args[1]
If (-not (Test-Path $DestinationFilePath)) {
New-Item -ItemType File -Path $DestinationFilePath -Force
}
Copy-Item -Path $SourceFilePath -Destination $DestinationFilePath
}
Copy-New-Item schema_mml3_mathml3_rnc schema\mml3\mathml3.rnc
# More of the same...
Copy-New-Item schema_svg11_svg_animation_rnc schema\svg11\svg-animation.rnc
# More of the same...
Copy-New-Item schema_html5_assertions_sch schema\html5\assertions.sch
# More of the same...
(Note that, in this case, the source file names have no file extension.)
If the destination file path does not exist, the function creates an empty file in that path, forcing the creation of any nonexistent directories in the file path. (If Copy-Item can do all that by itself, I could not see how to do it from the documentation.)
It is coming late, but as I stumbled upon this question looking for a solution to a similar problem, the cleanest one I found elsewhere is using robocopy instead of Copy-Item. I needed to copy the whole file structure together with the files, that's easily achieved via
robocopy "sourcefolder" "destinationfolder" "file.txt" /s
Detail about robocopy: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
None of the current answers worked for me to fix the Could not find a part of the path error raised by Copy-Item. After some research and testing, I discovered this error can be raised if the Destination path goes over the 260 character Windows path length limit.
What I mean by that is: if you supply a path to the Destination argument of Copy-Item and any of the files you are copying would exceed the 260 character limit when copied to the Destination folder, Copy-Item will raise the Could not find a part of the path error.
The fix is to shorten your Destination path, or to shorten/flatten the folder structure in the source directory that you are trying to copy.
May be Helpfull:
$source = 'c:\some\path\to\a\file.txt'
$dest = 'c:\a\more\different\path\to\the\file.txt'
$dest_dir = 'c:\a\more\different\path\to\the\'
[System.IO.Directory]::CreateDirectory($dest_dir);
if(-not [System.IO.File]::Exists($dest))
{
[System.IO.File]::Copy($source,$dest);
}
I have been digging around and found a lot of solutions to this issue, all being some alteration not just a straight copy-item command. Grant it some of these questions predate PS 3.0 so the answers are not wrong but using powershell 3.0 I was finally able to accomplish this using the -Container switch for copy-item.
Copy-Item $from $to -Recurse -Container
this was the test i ran, no errors and destination folder represented the same folder structure.
New-Item -ItemType dir -Name test_copy
New-Item -ItemType dir -Name test_copy\folder1
New-Item -ItemType file -Name test_copy\folder1\test.txt
#NOTE: with no \ at the end of the destination the file is created in the root of the destination, does not create the folder1 container
#Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2 -Recurse -Container
#if the destination does not exists this created the matching folder structure and file with no errors
Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2\ -Recurse -Container