I need to pack a folder using Compress-Archive in PShell
My folder structure is as following:
|- C:\MyFolder
|--- Files
|------ Start.ps1
|------ Stop.ps1
|--- Manifests
|------ Start.xml
|------ Stop.xml
When I issue this command Compress-Archive -Path "C:\MyFolder" -DestinationPath "C:\MyFolder.zip"
The result is a .zip which has a root folder called "MyFolder" while what I need is a .zip which replicate the tree of MyFolder without having MyFolder as the root one. Is that possible?
The Path parameter accepts specific file names and file names with wildcards. So instead of C:\MyFolder as Path parameter you could do pass "C:\MyFolder\*"(all its files and subdirectories hence skipping the root directory)
Compress-Archive -Path "C:\MyFolder\*" -DestinationPath "C:\MyFolder.zip"
This is also documented here,
-Path
Specifies the path or paths to the files that you want to add to the archive zipped file. To specify multiple paths, and include files
in multiple locations, use commas to separate the paths.
This parameter accepts wildcard characters. Wildcard characters allow
you to add all files in a directory to your archive file.
Using wildcards with a root directory affects the archive's contents:
To create an archive that includes the root directory, and all its
files and subdirectories, specify the root directory in the Path
without wildcards. For example: -Path C:\Reference
To create an
archive that excludes the root directory, but zips all its files and
subdirectories, use the asterisk (*) wildcard. For example: -Path
C:\Reference*
To create an archive that only zips the files in the
root directory, use the star-dot-star (.) wildcard. Subdirectories
of the root aren't included in the archive. For example: -Path
C:\Reference*.*
Related
I am trying to zip files that are in directories that have subdirectories and I can't figure out how to zip the files and not the subdirectories.
Here is the current setup:
C:\users\user\appdata\local\folder\
Inside of this folder, I need 3 out of the 20 or so folders that are in there so I used the Get-Childitem to accomplish this:
GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*}
Now that I have that, I don't want the subdirectories and just want the files that are sitting in the folder itself. I have not found a way to do this, but I have gotten close with using this:
& "C:\program files\7-zip\7z.exe" "a" "D:\TestBackup\Zipfile.7z" (GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*} | select -expandproperty FullName)
But this gives me the entire contents of the folder. I want to keep the structure so that it looks like this:
folder 1\files
folder 2\files
folder 3\files
I hope I am explaining myself well. The files are all different types of extensions so I was wanting a blanket way to do this or to exclude the subdirectories when zipping.
I had to consult the FAQ to get this right:
7-Zip stores only relative paths of files (without drive letter
prefix). You can change current folder to folder that is common for
all files that you want to compress and then you can use relative
paths:
cd /D C:\dir1\
7z.exe a c:\a.7z file1.txt dir2\file2.txt
Solution:
# Set base directory that is common for all files
Push-Location 'C:\users\user\appdata\local\folder'
# Get names of directories that match filter
$folderNames = (Get-ChildItem -Directory -Filter '*folder*').Name
# Call 7-zip, passing the list of directory names.
& 'C:\Program Files\7-Zip\7z.exe' a 'D:\TestBackup\Zipfile.7z' $folderNames
# Restore current directory
Pop-Location
Remarks:
Push-Location sets the current directory, while Pop-Location restores the previous current directory. Changing the current directory is crucial for this solution, as explained by the 7-zip FAQ. It is also the only way to set the base directory for Resolve-Path -Relative.
Pass -Directory or -File to Get-ChildItem if you are only interested in either directories or files.
Use -Filter instead of Where-Object (alias ?) if you only need simple wildcard filtering. -Filter is faster because it uses the FileSystem provider which filters at a lower API level, which avoids PowerShell overhead.
I have a folder, folder A in a file path, inside folder A are multiple folders (folder 1, folder 2, folder 3). I want to create a Powershell script that goes to the file path where folder A is, read the names of the folders in it and create a .txt file
Inside the .txt file it would have:
folder 1
folder 2
folder 3
I'm not sure how to do this
(there shouldn't be the extra lines in between each folder name in the .txt file, I just can't figure out how to format it properly
So I figured out the solution to my problem. What I was looking for was:
c:\Windows> Get-ChildItem -Path C:\Users... -Name | Out-File -FilePath C:\Users...\test.txt
This outputs the names of the directories that I put in the path and takes them into the test.txt file
I want to extract contents of a folder out of a zip file to a different location using powershell.
zip file: archive.zip
path in zip: mypath (folder contains multiple files and subfolders)
destination: c:\destination
If I extract normally I get the folder created:
Command:Expand-Archive -Path archive.zip -DestinationPath c:\destination
Folder Created: c:\destination\mypath
I want to extract the contents of the folder into c:\destination directly.
If it is a specific requirement with the values mentioned in the OP then this should do the work
$Path='c:\temp\destination';Expand-Archive archive.zip $Path;Move-Item $Path\mypath\* $Path;rm $Path\mypath;
I'm in the process of preparing files for uploading into a content management system and they must be in sequentially numbered folders. Each folder will contain image and data files as .tif and .txt, respectively. Currently all the files are in one directory and the goal is to move each file into its corresponding folder.
I can move the files based on the extension and create a folder that matches using this script:
$files = Get-ChildItem *.tif ; ForEach ($file in $files){$folder = New-Item -type directory -name $file.BaseName; Move-Item $file.FullName $folder.FullName;}
This results in folders containing a single .tif with both the folder and the file having a matching name. This is step one.
I'm stuck on step two. I have to move the .txt files that are currently in the parent directory into the matching folders based on the names. The goal is to have folders containing their matching .tif and .txt files. For example, folder "0001" has "0001.tif" and "0001.txt" inside.
Image of the parent directory with .txt and folders
My folder structure looks like this:
SourceFolder
├─file1.txt
├─file1.doc
└─Subfolder1
├─file2.txt
├─file2.doc
└─SubSubFolder
├─file3.txt
└─doc3.txt
This script copies all *.txt files from folders, whose (folder) names contains the eng, to a destination folder. Only the files inside the folder.
$dest = "C:\Users\username\Desktop\Final"
$source = "C:\Users\username\Desktop\Test1"
Get-ChildItem $source -Filter "*.txt" -Recurse |
Where-Object { $_.DirectoryName -match "eng" } |
ForEach-Object { Copy-Item $_.fullname $dest }
In my situation the folders are in .rar format and I want the script to search .rar folders and copy the *.txt files from folder eng to destination. Is that possible with PowerShell?
Rar is a proprietary archive format. Your .rar items aren't folders but compressed archives of that format. PowerShell can't transparently handle such archives, and I'm not aware of a third-party provider that would add this functionality.
Basically, no, what you're asking isn't possible.
What you can do is extract files from the archive with tools like 7-zip, e.g.:
7z.exe e your.rar *.txt -r
For extracting only files from a nested subfolder inside the archive prepend the extract pattern with that path:
7z.exe e your.rar "nested\subfolder\*.txt" -r