how to add many folders from differents places to one file zip in PowerShell script using [io.compression.zipfile]
One way to do this is to use the Extensions
https://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions(v=vs.110).aspx
A simple example for adding a single file would be as follows
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
$NewFilePath = "c:\pat\to\file.txt"
$EntryName = "file.txt"
$zip = [io.compression.zipfile]::Open("test.zip",[io.compression.ziparchivemode]::Create)
[io.compression.ZipFileExtensions]::CreateEntryFromFile($zip, $NewFilePath, $EntryName)
$zip.Dispose()
You can just iterate over your list of files adding them to your new zip file.
Note that you can reference the type without the full path if you wish.
Related
I've few folders and nested sub-folders. I would like to filter all the files in the folder including the sub folders and list files with extension .XSD and search for the text called NewDataSet and replace the text with the name of the File.
I would like to perform this using Windows Powershell (ps1) script. Please help me out.
Try following :
using assembly System
using assembly System.IO
$folder = "c:\temp"
$schemas = [System.IO.Directory]::GetFiles("$folder", "*.XSD")
foreach($schema in $schemas)
{
$text = [System.IO.File]::ReadAllText($schema)
$text = $text.Replace("NewDataSet", $schema)
[System.IO.File]::WriteAllText($schema, $text)
}
I'm a newbie with some basic skills.
I get a .zip file which contains multiple directories with .csv files within. The program compiling those files does not distinguish between upper and lower case, so you can get files FILE.csv and file.csv in the same directory as separate files.
When I unzip, they are obviously recognized as duplicates.
My PowerShell script as follows works fine if there are no duplicates.
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
function Unzip {
Param(
[string]$zipfile,
[string]$outpath
)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$sourcefile3 = Get-ChildItem 'root\Drop\Archive*.zip'
$destination3 = 'root\Drop\All_Files'
Unzip $sourcefile3 $destination3
However, because my .zip contains multiple directories, I cannot specify a file name in the $destination3. I tried this, but it created a new folder instead of renaming that duplicate file within a folder.
if (Test-Path $destination3) {
$i = 0
while (Test-Path $destination3) {
$i += 1
$destination3 = ''root\Drop\All_Files$i'
}
}
I read here and here, but this talks about files, not files within directories/folders.
Is it possible to get all files names from a directory in variables ?
Consider this environment :
Dir/File.json
Dir/File7.json
Dir/File58.exe
Is it possible so that i can get File, and File7 (only the file with .json extension) in one or two variables that i'd use later in my code ?
I test dir > test.txt but : It show everything including folder or files with an other extension, and i don't know if i can then use this .txt file to get back the names individualy.
Using PowerShell
$FileNames = Get-ChildItem -Path 'C:\Dir' -Name *.json
This will return all json files in the folder C:\Dir
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 ;)
i am trying to move or copy a .xml file into a zip folder.
Move-Item-Path "C:\Users\1469\Desktop\folder1\archive.xml" -Destination "C:\Users\1469\Desktop\*.zip" -Force
But this code is not working. please help me!
You have a typo in that line (Move-Item-Path should be Move-Item -Path). You can't use wildcards in a destination path, though, and you also can't use Move-Item to move files into zip files. Doing so would simply overwrite the zip file with the XML file.
You need the Shell.Application COM object for adding files to a zip file:
$zip = 'C:\Users\1469\Desktop\your.zip'
$xml = 'C:\Users\1469\Desktop\folder1\archive.xml'
(New-Object -COM Shell.Application).NameSpace($zip).CopyHere($xml)
Note that the zip file must already exist. It won't be created automatically.