I'm trying to feed the results of a Get-ChildItem call through io.compression.zipfile to create a zip file of "E:\Applications_Server_Test", excluding two folders "BACKUP" and "BACKUP2".
However, Powershell seems to be interpreting this as "$items = a string of directories and file names" instead of a recursive collection of directories and files I want to zip. I can find tutorials on using Get-ChildItem to exclude directories and I can find tutorials on how to zip a full directory or zip multiple directories but I can't find anything on zipping directories with exclusions. Can somebody tell me where I'm going wrong?
$source = "E:\Applications_Server_Test"
$destination = "E:\AST_Dump.zip"
$items = Get-ChildItem $source -Recurse | ?{ $_.fullname -notmatch "\\backup\\?" }
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($items, $destination)
Thanks!
At the moment you are trying to archive the object $items, not the folder. Unfortunately this is not gonna work.
Try to move the backup folders at the same drive (this will just change records at the drive and not move any data), then to archive the whole "E:\Applications_Server_Test" folder and to move back the backup folders.
Other option is to use ZipArchive.CreateEntry method and to add file by file in to the archive. But this is not so elegant ;)
Related
I am trying to copy music lyric files (.lrc) from a folder with many subfolders such as artists and albums and paste them in an empty folder, but I still want to keep the folder structure without having to create each individual folder for those files to be put in. In other words: I want to take certain files from a folder and have it automatically create the folder structure in another folder which the original file was in.
For example: I have 10 lyric files accompanied with other music files in a single folder called "ArtistName" which is a subfolder of a subfolder of a folder called "Music". These lyric files need to be in another folder called "Music2" that is currently empty, but instead of just dumping the files in the root folder, I need to recreate the folder structure in which the original lyric files were in.
Is there any way to do this? Keep in mind, I am not very experienced when it comes to programming but I know some basics. Unfortunately I might need more of an explanation than most people here. Thank you to anyone who can help!
Here's a powershell answer:
Use Copy-Item with the -Container and -Recurse switches to copy the folder structure including files to a new location.
Copy-Item -Path "Old\Path\for\Music" -Recurse -Destination "New\Path\for\Music" -Container
If you only want to copy the lyric files while retaining the folder structure use a -Filter
Copy-Item -Path "Old\Path\for\Music" -Recurse -Filter "*.lrc" -Destination "New\Path\for\Music" -Container
You can do this in CMD
xcopy /s yourFolder\Subfolder\*.pdf destinationFolder\myfolder
powershell, recurse option keeps the folder structure
copy-item c:\\srcFolders\\* f:\\dst -force -recurse -verbose
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 have multiple files that I want to copy each file to a folder with same name
For example, the files
orange_file100 , orange_file200 , orange_file300 , apple_file120 , apple_file150
I want to move each file to a folder that contain part of the filename say orange and apple so the result will be
orange\orange_file100
orange\orange_file200
orange\orange_file300
apple\apple_file120
apple\apple_file150
How can I do that through powershell, should I use Get-ChildItem then ForEach{Copy-Item) ?
You can use Get-Childitem with a -File or -Directory to only grab the files or folders in a folder, that way you wont grab a folder and try place it in itself.
For example, the code below will only grab the files in the current directory
Get-Childitem -File
You can then use some regex to split the names so you can get the fruit name e.g.
$String.split('_')[0]
You should insert it into a list or array or something to store it, but now you have a list of files and fruit names.
Now you can loop over the list and start to move or copy the files into the right folder structure
Foreach($file in $FileList){
if($file.name -matches $Fruitname){
if($file.name -notmatch $pwd.path ){
mkdir $file.name
cd $file.name
move-item $file.fullname $pwd
}
}
}
The code above is just a quick attempt. It probably wont work the first time and you should make adjustments to understand what you are doing.
A few notes
$pwd gets the current directory. I'm assuming Get-Childitem returns the list of files in the correct order, so you will get Orange_100, then Orange_200 and so on
Get-Childitem returns a powershell object. The file names can be accessed using $_.name or the full path using $_.fullname
If -matches doesn't work, you can also try -like or -in
I didn't add in the first fruit folder into the code above, but it won't be hard to create
Remember to play around and find whats best for you.
Hope fellow scripters can help with this one :) Been breaking my head around the problem for few hours now.
I'm trying to zip up certain folders using powershell.
My folder structure is
Backups
BoxIntranet
Components
Content
Database
Exec
Files
Logs
Multibrowser
Multibrowser\Legacy\Customisation
Packages
ParentPortal
ParentPortal\customisation
StudentPortal
StudentPortal\customisation
Update
WebDav
There are a lot more files and folders in every one of the above but these are the ones I'm mainly interested in.
I am trying to zip it all up using either Write-Zip or Compress-Archive methods in PowerShell but my conditions are.
Only Content, Files, Database folders should be zipped from root
Multibrowser\Legacy\customisation, StudentPortal\Customisation and ParentPortal\customisation folders should also be backed up.
Folder structure should remain the same in the zip file meaning that Root of the zip file should have Content, Files, Database, Multibrowser, ParentPortal and StudentPortal folders. Whilst Content, Files and Database folders should have everything zipped up, Multibrowser, ParentPortal and StudentPortal folders should only have the specified sub directories and all files within them.
Code:
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
Get-ChildItem -Path "C:\Folder" -Include $FilesAndInclude -Recurse -Exclude $FilesToExclude|
Compress-Archive -DestinationPath "Archive.zip"
I've tried the above and it doesn't do anything however if I remove the -Include parameter then it zips up everything however doesn't retain folder structure.
Is there any way to complete what I am after within powershell?
Ok, first things first, the reason that you are having a hard time using the -Include parameter is because it is designed to exclusively include only the things you specify. As such, it will look at the name of things (not their path), and check against the list and if it matches something in the list it will include that item. Since you only list folder names it is only including those folders (but not their contents). So you aren't getting any files passed down the pipe this way. To get around that you'll need to build your file list first, then pipe it to the cmdlet to zip things up.
Next issue is that Compress-Archive doesn't store path info, so you'll need to use Write-Zip. I have included what I think you would want for that cmdlet.
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
[array]$FilesToZip = Get-ChildItem .\* -Exclude $FilesToExclude -File
$FilesToZip += $FilesAndInclude | ForEach{Get-ChildItem .\$_ -Exclude $FilesToExclude -File}
$FilesToZip | Write-Zip -EntryPathRoot $(Resolve-Path .\|Select -Expand Path) -OutputPath Archive.zip
My problem is the following:
I repaired a bunch of dwg that were corrupted and i need to replace the corrupted ones with the repaired ones using power shell. They have the same name, the problem is that i acquired the files through the windows search tool, and i have them all in a single folder.
The original corrupted files exist in many sub folders. How do i copy all files from say folder
"d:\repaired" to "d:\original"
replacing each original files with its repaired one?
I tried doing it with xcopy using the /u, but i also need a way to iterate through the sub-folders.
Ok guys, i did it through command line. I found the answer here:
Replacing a file into multiple folders/subdirectories
I simply used something like this:
Replace C:\SomeFile.Txt C:\SomeRootFolder_ContainsMultipleSubFolders /s
EDIT
$files = Get-ChildItem -path C:\files -Filter *.txt -Recurse
foreach
($file in $files)
{copy-item -path *.txt -destination $files -recurse -force}
Will find all files with a certain extension, then replace them with another file from a directory specified in the -path.