I am trying to create a powershell script to compare 1 folder to another, say Folder A to Folder B. I want my script to make folder B look exactly like Folder A every time this script runs. Overriding anything in there, and deleting anything in Folder B that is not in Folder A. I have no code for it yet, and everything ive tried does not work. I made a script that copies from Folder A to Folder B and it works but wont delete anything different and wont override a file. So if its already in there, it doesn't care that the item in Folder A is newer, it will keep Folder B old file:
Test-Path "C:\Users\Shawn\Desktop\Scripts\New Folder"
if((Test-Path True))
{
Copy-Item -Path "C:\Users\Shawn\Pictures\" -Destination "C:\Users\Shawn\Desktop\Scripts\New Folder" -recurse
}
else
{
New-Item -Path "C:\Users\Shawn\Desktop\Scripts\New Folder" -ItemType directory
Copy-Item -Path "C:\Users\Shawn\Pictures\" -Destination "C:\Users\Shawn\Desktop\Scripts\New Folder" -recurse
}
Related
foreach ($file in Get-ChildItem -Path $srcRoot -File -Include "*.pdf" -Force -Recurse {
Above is just a line out of my script that is moving files from one directory to another. Long story short.. My script is working and has been for months. However, today I came across where it didnt move two files that were named like the following.
Thisismyfile.pdf2.pdf
Thisisanotherfile.pdf2.pdf
Now like I said.. the script has been working fine until these files came about. Of course I told the users to make sure they name files correctly ect.. but I dont know why it still didnt move those files. It still contains "*.pdf" as an extenstion.. so what gives?
I suspect that files are not moved in scenario where you have file in folder A with the same name as in folder B. Moving files to the one destination folder will cause name collision with error like Move-Item : Cannot create a file when that file already exists.
If that is the case, please use one of snippets from this answer: Powershell Move-Item Rename If File Exists
I placed few .pdf files (named like a.pdf.pdf, b.pdf.pdf ...) in src directory, running snippet below moves those files to dst folder correctly.
$srcRoot = "C:\Users\$env:username\Desktop\src\"
foreach ($file in Get-ChildItem -Path $srcRoot -File -Include "*.pdf" -Force -Recurse)
{
Move-Item -Path $file -Destination "C:\Users\$env:username\Desktop\dst\"
}
I have .dll and .sys files under folder xyz
xyz
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
These need to be copied something like this under new folder
new
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
What i have tried:
'Copy-Item -Path $file_path\..\*sys -Destination C:\Users\Path\new\'
Here '$file_path = \xyz\QcXhciFilter8086\QcXhciFilter8086.sys'
Result: Only .sys.dll files getting copied directly under "new" folder. However i want them under the driver name. Something like this 'new\QcXhciFilter8086\QcXhciFilter8086.sys'
Okay, I think I understand what you are asking for...
You have a folder structure like this:
[xyz]
QcXhciFilter8086
QcXhciFilter8086.sys
qSarMgr8086
qSarMgr.dll
qcwlan_wpextwapi8086
WapiIhvExt.dll
And you want this copying to another folder. Using this will copy the folder structure and files from the xyz folder to the new folder. This will copy everything, however. If you only want to copy the files that are only .sys and .dll then you will need to add another Get-ChildItem in the existing ForeEach-Object loop to look at the current directory.
Here is my proposed solution:
$source = "c:\path\to\xyz"
$destination = "c:\path\to\new"
Get-ChildItem -Path $source -Directory | ForEach-Object{
Copy-Item $_.FullName -Destination $destination -Recurse -Container
}
I wanted to update an app.
In order to do this, i want to test if files AND folders from $path_new exists in $path_old. If so, delete those from $path_old and copy those from $path_new, if not, keep them on $path_old.
So, i'll keep everything except updated files.
Edit : Using PowerShell.
Edit : So if an user added a personnal folder in the app mmain folder, he'll keep it.
Maybe i'll look like this :
$FileName = "C:\Share\test.txt"
if (Test-Path $FileName)
{
Remove-Item $FileName
}
But it's not exactly what i want. I think i'll have to add a "for" loop, but how, where ?
This will copy files from the source directory to the target directory. 'Personal files', or files not existant in the source directory, will be ignored while files that exist in both folders are overwritten.
In addition to that, I'd like to point out that your approach to this problem is inefficient so to speak. It's a lot more work to search for files existing in both directories, deleting those, and then copying those over from the new directory to the old directory. Instead you should just use existing file copy actions such as Copy-Item as they have built-in functionality to overwrite existing files.
$target = ".\old"
$source = ".\new"
Get-ChildItem -Path $source | Copy-Item -Destination $target -Recurse -Force -Container
Parameters
-Recurse Copy all files and folders recursively.
-Force Copy items that cannot otherwise be changed, such as copying over a read-only file or alias.
-Container This will retain the folder structure when doing recursive copy actions.
I want to copy a folder which contain many file and other folders to a specific folder. However I get fail by using the following code.
Copy-Item "c:\F1" "c:\Fs\F1"
or
Copy-Item "c:\F1\*" "c:\Fs\F1"
The structure of folder1:
F1
F1-1
file B
F1-2
file C
file D
file A
You are probably missing the -recurse option.
Copy-Item -Path C:\F1 -Destination c:\FsF1 –Recurse
I have a simple folder structure that I need to zip up to upload to AWS Lambda function
node-modules/
index.js
package.json
The files above are in the root directory. Basically the end goal is I would like a zip of all of these files and sub-files/directories instead of the structure as it is.
When I try to run the command below it says it can't access E://apps/myapp/release.zip because it's being used by another process. However I can see it starts to create release.zip but it does not have all the contents.
Add-Type -Assembly "System.IO.Compression.FileSystem";
[System.IO.Compression.ZipFile]::CreateFromDirectory(E://apps/myapp, E://apps/myapp/release.zip);
So I tried another approach. Take the folder and two files and copy them into a temporary folder, then try to zip it back into the root.
Copy-Item E://apps/myapp E://apps/myapp/temp -recurse
I do see a temp/ folder but within the temp folder it's like an inception of never ending copies until the file path gets too long.
Any tips would be much appreciated.
Issue could be that you are creating the zip file in the same folder that you are trying to compress. And you basically do the same thing when you tried using the temporary folder, hence the inception.
Try creating destination outside of the source folder being compressed.
$source = "E://apps/myapp"
$destination = "E://apps/myapp.release.zip"
# If the archive already exists, an IOException exception is thrown.
if(Test-Path $destination) {
Remove-Item -Path $destination -Force -Recurse -Confirm:$false
}
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($source, $destination)
# Once archive created delete folder
if(Test-Path $destination) {
Remove-Item -Path $source -Force -Recurse -Confirm:$false
Write-Host "directory removed: $source"
}