I was asked to benchmark using a powershell script to do some basic file copying as opposed to using XCopy in a batch file. It seems to run in roughly about the same amount of time, but with Powershell the parent folder of the tree structure is not being created in the destination, whereas with XCopy the parent folder does get created. For example, with
xcopy D:\Webs\First\*.* D:\Test\Sandbox\ /E
The "First" folder does get created with all its contents. Whereas with
$SourceFolder = "D:\\Webs\\First\\"
$TargetFolder = "D:\\Test\\Sandbox\\"
Copy-Item $SourceFolder $TargetFolder -recurse
The folder named "First" does not get created and rather the contents are copied to the destination. If I use "Webs" as the source folder, there are other folders at the same level as "First" that get copied as well and that is not desirable.
How do I get the Parent folder "First" to copy to the destination using the Copy-Item or some other powershell command without manually creating this folder within the script so I get the exact same results?
Thanks...
for a quick way you can append the source parent folder before copy, e.g.
$TargetFolder = "D:\\Test\\Sandbox\\"+($SourceFolder | Split-Path -leaf)
you can also use the Measure-Command Cmdlet for timings
Related
I have requirement as below.
Source : C:\s
Destination: C:\d
files located are more than 255 characters.
Moving files based on last modified or written days(10) and it should copy complete folder structure, if any of the files not modified as per the last modified date it should be available at the source file in the same folder. While the other is modified, it should be created with a new directory with the same folder structure and file to moved in the same location as per the source location path.
I have tried PowerShell script using days, however the files are being copied into and folders are staying at the source itself.
Get-ChildItem -Path C:\s -Recurse | Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(0)} | Move-Item -destination C:\d
So far the output is giving only files but not the folder structure, if it is empty folder it should be moved to the destination folder.
Thanks
Suman
The issue is that you are getting all the files with Get-ChildItem -Recurse and filtering them, but when those files are piped to Move-Item -Destination you essentially are saying to take all files (regardless of the source) and put them into the single folder C:\d. If you want to preserve the directory structure, you have to pass through and specify the directory structure in the Move-Item -Destination parameter.
#AdminOfThings is correct, an easier way to do the move is to use Robocopy. Experimenting with the various switches should get you what you need. e.g.:
Robocopy.exe C:\s C:\d /move /e /minage:10
Where:
/move says to move instead of copy
/e is copy directories including empty ones
/minage:10 is to move files older than 10 days.
I need to be able to copy all items within a drive and move them to a new destination using Powershell. At present, I've tried doing this with Copy-Item but am unable to do so within a drive. I've looked for solutions elsewhere but have yet to find a working fix, Any suggestions?
Copy-Item -Path 'P:' -Destination 'Destination'
If you're trying just to copy all of the items from one drive to a folder on another drive, you can use Copy-Item, like you are, with a few adjustments (-Destination path is just an example):
Copy-Item -Path C:\* -Destination F:\destinationPath -Recurse
Using the * in C:\* for the path tells the shell to get all files and directories at that folder, in this case, the C: root. -Recurse tells it to copy all files and directories recursively. Consider adding the -Force parameter if you want to use this in automation, as it will forcibly overwrite any existing files at the destination rather than prompt for input.
I am having a challenge while trying to move a large number of files from one location to another. I have a list of folders that I want to move in a .csv and I want to move those items to a new network location. When I use robocopy, only the content of the folders is moved, not the top level folder. This leads to unorganized files. I have tried using move-item but there is not a good logging feature that I can get to work. So here I am.
I am trying to use robocopy but I need to create a destination folder based on the last part of the source path for each item in a list. I have been working on this for hours, please help.
GC -Path C:\Test_1\Test_List.csv |
ForEach-Object
{
$Destination = new-item NAME BASED ON PART OF FILE -itemtype directory |
Robocopy $_ $Destination /e /move /LOG+:C:\Test_2\Test_Copy_Log.txt /NP
}
I want to copy a group of folders which have various files inside them from one place to a single folder elsewhere. I want to bind said folder group to a variable.
param(
$folders=('../folder1','../folder2')
)
Copy-Item -Path $folders -Destination '../folder3' -Recurse -Force;
This works, however, inside folder3, folder1's contents are spilled out, while folder2's contents are placed in a folder of the same name just like intended.
I need them both to be copied intact, if I switch their places then folder2 gets the same treatment. It's like the script does not read the first folder in line in same way as the others. Am I missing something?
EDIT:
Managed to find a work-around by running additional command to create a folder inside "folder3" named same as first in line folder before copying. Script then places the files inside that folder correctly. Still rather messy, I wonder if it's a bug.
Use a loop
foreach($folder in $folders){
Copy-Item -Path $folder -Destination '../folder3'-Recurse -Force
}
I want to copy a folder, complete with subdirectories, files and files within subdirectories, preserving the structure and create them in a new location that did not previously exist. This is my PowerShell code
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\foldertwo -recurse -Container
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\folderthree -recurse -Container
foldertwo exists and is empty, folderthree does not exist.
If I run the script, the structure is created correctly in foldertwo, however folderthree gets created, but contains only all the files from the entire substructure, all at the root folderthree level. It has not recreated the subfolders within folderone, just put all the files at the root level of folderthree.
What have I done wrong?
Here's a very basic, but fully working and tested example, building on your confirmation above that I understood the issue at-hand:
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "C:\Development\PowerShell\$folder"))
{
mkdir ("C:\Development\PowerShell\$folder") | Out-Null
}
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\$folder -recurse -Container
}
From what I understand, the question is about recreating the folder structure from [source] to [destination]. As using CmdLets is kind of overkill (and performance loss), I suggest simple batch command that may also be ran in powershell.
xcopy [source] [destination] /T /E
xcopy is a function to copy file and directory trees.
Help provides us info on usefult parameters on the case:
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.