Issue in Copy-Item in powershell - 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.

Related

Verify file copied using copy-item

i'm trying to copy file from source to destination any verify if file copied or not.
But the problem is if i make changes inside the source file which was copied earlier then destination file not getting override when i execute the code again. Also i want log file each time files are copied.
Files in folder:- .csv, .log, .png, .html
$source="C:\52DWM93"
$destination="C:\Temp\"
Copy-Item -Path $source -Destination $destination -Force
$ver=(Get-ChildItem -file -path $destination -Recurse).FullName | foreach {get-filehash $_ -Algorithm md5}
If($ver)
{Write-Host "ALL file copied"}
else
{Write-Host "ALL file not copied"}
If you copy directories like this you need the -Recurse switch for Copy-Item. Without it you're not going to copy anything except the directory itself.
You can of course also use Get-ChildItem with whatever filter or Recurse flag you care about and pipe that into Copy-Item.
Use the *-FileCatalog cmdlets for verification.

PowerShell Move Files From One Server to Another

I know this has been asked a million times, but I can't seem to find anything that works for me. I don't know if there is a permissions issue or what, but I am trying to move files from one server to another using a PowerShell script in the task scheduler and it worked for about a week before it stopped working. There are no errors in the task scheduler, and I'm not well versed in PowerShell at all, I'm just trying to get something quick and simple for our CMS manager to move her files from the website to a folder on another server.
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST= "\\server-folder-structure\uploads\" ## enter your destination folder
foreach ($ORG in gci $DEST -include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -recurse)
{
Move-Item -path $ORG -destination $DEST ## Move the files to the destination folder
}
I tried this too, in hopes it would work, but still no files are being moved.
Get-ChildItem E:\folder-structure\uploads\* -Include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -Recurse |ForEach-Object { Move-Item $_.FullName \\server-folder-structure\uploads\ }
Am I doing something wrong? Are there permissions to folders that I need to set that I don't know about? Is PowerShell just not the best way to do this? Thanks in advance.
I believe you're making this harder than it should be (with respect to PowerShell being new to you). You don't need a loop on any of your examples if you want to pipe directly to Move-Item:
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST = "\\server-folder-structure\uploads\" ## enter your destination folder
$filterFor = "*.doc","*.docx","*.pdf","*.png","*.gif","*.jpg","*.jpeg","*.html","*.htm"
Get-ChildItem $ORG -Include $filterFor -File -Recurse |
Move-Item -Destination $DEST -WhatIf
As for what you tried, and as Mathias pointed out, you would be searching your $DEST location in which the files wouldn't exist as they would only be in $ORG; given that that's the actual source folder.
This would also overwrite you $ORG variable with the current item in your iteration in: foreach ($ORG in gci ..){ ... }.
Meaning, your Move-Item would be invalid.

How to copy all files in a folder to another folder and change the extension?

I have some files in a folder with no extension file. I want to copy all the files to another folder and change their extension to .txt.
I tried this code, but it still errors for creating the destination file.
$JOB = Copy-Item -Path C:\Users\XX\Documents\Folder1* -Destination "C:\Users\XX\Documents\Folder2"
Rename-Item -Path C:\Users\XX\Documents\Folder2\* -NewName *.TXT
Get-ChildItem -Path .\Folder1 -File |
ForEach-Object { Copy-Item -Path $_.FullName -Destination ".\Folder2\$($_.BaseName).txt" }
# Alternatively without ForEach-Object (see note below)
Get-ChildItem -Path .\Folder1 -File |
Copy-Item -Path $_.FullName -Destination { ".\Folder2\$($_.BaseName).txt" }
That should do the job. Keep in mind it will work only for one level, if you want recursive copy of folder structure, you'll have to modify the script slightly.
Basically, what's happening here is you find all the files and then pipe them to Copy-Item constructing destination path with BaseName property of source file (which doesn't have extension included, in comparison to Name property).
NOTE: as -Path accepts pipeline input (see docs here), you don't need to use ForEach-Object. However, it might still be useful for visibility (depending on your preferences).
Credits to #LotPings for noticing the above.

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

PowerShell to copy files to destination's subfolders while excluding certain folders in the destination

So I have danced with this off and on throughout the day and the timeless phrase "There's more than one way to skin a cat" keeps coming to mind so I decided to take to the community.
Scenario:
Source folder "C:\Updates" has 100 files of various extensions. All need to be copied to the sub-folders only of "C:\Prod\" overwriting any duplicates that it may find.
The Caveats:
The sub-folder names (destinations) in "C:\Prod" are quite dynamic and change frequently.
A naming convention is used to determine which sub-folders in the destination need to be excluded when the source files are being copied (to retain the original versions). For ease of explanation lets say any folder names starting with "!stop" should be excluded from the copy process. (!stop* if wildcards considered)
So, here I am wanting the input of those greater than I to tackle this in PS if I'm lucky. I've tinkered with Copy-Item and xcopy today so I'm excited to hear other's input.
Thanks!
-Chris
Give this a shot:
Get-ChildItem -Path C:\Prod -Exclude !stop* -Directory `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
This grabs each folder (the -Directory switch ensures we only grab folders) in C:\Prod that does not match the filter and pipes it to the ForEach-Object command where we are running the Copy-Item command to copy the files to the directory.
The -Directory switch is not available in every version of PowerShell; I do not know which version it was introduced in off the top of my head. If you have an older version of PowerShell that does not support -Directory then you can use this script:
Get-ChildItem -Path C:\Prod -Exclude !stop* `
| Where-Object { $_.PSIsContainer } `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
To select only sub folders which do not begin with "!stop" do this
$Source = "C:\Updates\*"
$Dest = "C:\Prod"
$Stop = "^!stop"
$Destinations = GCI -Path $Dest |?{$_.PSIsContainer -and $_.Name -notmatch $Stop }
ForEach ($Destination in $Destinations) {
Copy-Item -Path $Source -Destination $Destination.FullName -Force
}
Edited Now copies all files from Update to subs of Source not beginning with "!stop" The -whatif switch shows what would happen, to arm the script remove the -whatif.
Edit2 Streamlined the script. If also Sub/sub-folders of C:\Prod shall receive copies include a -rec option to the gci just in front of he pipe.