This is what I am trying to do with powershell for zipping files.
Sort out files older than xxx days
Add those files in a zip file.
Delete the source files.
I have Powershell_Community_Extensions installed so I use Write-zip to do the job.
$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}
$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet
Remove-Item $files -Force
Issues:
I have to use -EntryPathRoot with Write-zip, otherwise it wont pick up network share
Remove-item also wont pickup files from network share, it says "Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.", why it deleting file from C:\Windows\system32\ instead of \\network\share
Write-zip -append did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. I just want to add the newer filtered files into root of that zip file.
Any idea please?
Utilizing the v5 *Archive cmdlets:
$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update
$Files | Remove-Item -Force
Related
When trying to unzip files traditionally in Windows, it unzips to the name of the zip file as a folder with the files embedded into the folder itself. I am unable to recreate this using the Extract-Archive Cmdlet. Here is the code section for this that I have:
Get-ChildItem 'path of zip' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
While running this, the unzip process works fine. I am just wondering if there is a way to keep the traditional unzip file/folder structure when using the Expand-Archive Cmdlet
Thank you
You can set the destination folder in parameter DestinationPath. If the folder with the name of the zipfile does not yet exist, it will be created:
$sourcePath = 'path of zip file(s)'
$destination = 'path to extract'
Get-ChildItem -Path $sourcePath -Filter '*.zip' -File | ForEach-Object {
$target = Join-Path -Path $destination -ChildPath $_.BaseName
$_ | Expand-Archive -DestinationPath $target -Force
}
I have a source folder(c:\test*) and I want that folder copied to all folders in the same location (c:\resultaat) but only to the folders in that location that contains the name demo_profit.
I think I'm almost there, but I'm missing a small part I think.
$destination = get-item -include demo_profit* -path C:\resultaat\*
copy-item C:\test\* $destination -recurse -force
thanks for your help
The -Directory parameter of Get-ChildItem requires AFAIK PSv5
Get-ChildItem -Directory -Path 'C:\resultaat\' | ForEach-Object{
if (Test-Path (Join-Path $_.FullName 'demo_profit')){
Copy-Item C:\test\* $_.FullName -recurse -force
}
So it is not clear if you want to copy the files to a folder containing an item named demo_profit, or to folder with demo_profit in the name. The second is simple, so I'll start there:
Get-ChildItem C:\resultaat\* -Directory |
Where{$_.Name -match 'demo_profit'} |
ForEach{ Copy-Item C:\test\* -dest $_.FullName -recurse -force }
That finds any folder within c:\resultaat that has 'demo_profit' as part of its name, and copies the desired files/folders into that folder. Example: C:\resultaat\Folder1_demo_profit would have all the files from C:\temp copied into it.
If you are looking for things named demo_profit, and want to copy the files into the same folder as that item you need to use the PSParentPath property, and some wildcards.
Get-ChildItem c:\resultaat\*\*demo_profit*
This command would find a file or folder with a path such as 'C:\resultaat\Amazon\az_demo_profit'. Then to copy files into the folder containing that you would use the PSParentPath property as such:
Get-ChildItem c:\resultaat\*\*demo_profit* |
Select -Expand PSParentPath -Unique
ForEach{ Copy-Item C:\test\* -dest $_ -recurse -force }
My folder C:\Downloads\Files\ has 3 .zip files.
test1.zip
test2.zip
test3.zip
I need to copy these files into same location and append another extension .dat to the file.
The total files in my folder C:\Downloads\Files\ should have 6 files now (3 .zip and 3.dat)
test1.zip
test2.zip
test3.zip
test1.zip.dat
test2.zip.dat
test3.zip.dat
Can anyone help me get this done?
Use the Get-ChildItem cmdlet to retrieve all zip files and copy them using the Copy-Item cmdlet:
Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' |
Copy-Item -Destination { "$($_.Name).dat" }
The following copies only files from within the $path directory with extension .dat.
Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}
Can I somehow exclude a folder when I compress an archive like this?
$compress = Compress-Archive $DestinationPath $DestinationPath\ARCHIVE\archiv-$DateTime.zip -CompressionLevel Fastest
Now it always saves the whole folder structure of $destinationpath to the archive, but since the archive is in the same folder, it always gets zipped into a new archive, making the archive double in size every time I run the command.
Get all the files you want to compress, excluding the files and folders you don't want compressed and then pass that to the cmdlet
# target path
$path = "C:\temp"
# construct archive path
$DateTime = (Get-Date -Format "yyyyMMddHHmmss")
$destination = Join-Path $path "ARCHIVE\archive-$DateTime.zip"
# exclusion rules. Can use wild cards (*)
$exclude = #("_*.config","ARCHIVE","*.zip")
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $path -Exclude $exclude
# compress
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Fastest
you can use -update option of Compress-Archive. Select your subdirs with Get-ChildItem and Where
like it:
$YourDirToCompress="c:\temp"
$ZipFileResult="C:\temp10\result.zip"
$DirToExclude=#("test", "test1", "test2")
Get-ChildItem $YourDirToCompress -Directory |
where { $_.Name -notin $DirToExclude} |
Compress-Archive -DestinationPath $ZipFileResult -Update
I know this question is rather old, but wanted to post my solution here. This solution has worked for me and I hope it may help someone else having the same issue.
I took the ideas from the previous answers and developed them a bit.
So generally speaking what you need to do is to create two lists, one for the files in the root directory and another one for directories (excluding the directory you'd want to omit). Then you need to concatenate these two lists together and put them into -Path parameter of Compress-Archive cmdlet.
Voila! It will create a .zip archive with all files and directories we need, preserving the directory structure.
$files = Get-ChildItem -Path /RootDir -File
$directories = Get-ChildItem -Path /RootDir -Recurse -Directory -Exclude DirToExclude
Compress-Archive -Path $($files + $directories) -DestinationPath Archive.zip
I am trying to write a PowerShell script that will copy a subset of files from a source folder and place them into a target folder. I've been playing with "copy-item" and "remove-item" for half a day and cannot get the desired or consistent results.
For example, when I run the following cmdlet multiple times, the files end up in different locations?!?!:
copy-item -Path $sourcePath -Destination $destinationPath -Include *.dll -Container -Force -Recurse
I've been trying every combination of options and commands I can think of but can't find the right solution. Since I'm sure that I'm not doing anything atypical, I'm hoping someone can ease my pain and provide me with the proper syntax to use.
The source folder will contain a large number of files with various extensions. For example, all of the following are possible:
.dll
.dll.config
.exe
.exe.config
.lastcodeanalysisissucceeded
.pdb
.Test.dll
.vshost.exe
.xml
and so on
The script needs to only copy .exe, .dll and .exe.config files excluding any .test.dll and .vshost.exe files. I also need the script to create the target folders if they don't already exist.
Any help getting me going is appreciated.
try:
$source = "C:\a\*"
$dest = "C:\b"
dir $source -include *.exe,*.dll,*.exe.config -exclude *.test.dll,*.vshost.exe -Recurse |
% {
$sp = $_.fullName.replace($sourcePath.replace('\*',''), $destPath)
if (!(Test-Path -path (split-path $sp)))
{
New-Item (split-path $sp) -Type Directory
}
copy-item $_.fullname $sp -force
}
As long as the files are in one directory, the following should work fine. It might be a bit more verbose than needed, but it should be a good starting point.
$sourcePath = "c:\sourcePath"
$destPath = "c:\destPath"
$items = Get-ChildItem $sourcePath | Where-Object {($_.FullName -like "*.exe") -or ($_.FullName -like "*.exe.config") -or ($_.FullName -like "*.dll")}
$items | % {
Copy-Item $_.Fullname ($_.FullName.Replace($sourcePath,$destPath))
}