PowerShell - extract contents of a specific folder recursively - powershell

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;

Related

remove folder with certain name in powershell

I need to copy folders from $Frompath to $topath . Now the folders with same name exists in the topath location. Now I need to replace that files with the new files .How can i copy files from one folder to another when the file already exists in the destination.

Merge CSV Files by names into one from various subfolders in parent folder using Powershell

I have multiple zip files with same names except Abc0.zip, Abc1.zip.. files. I need to unzip all of them and all of them having almost same name folders having some .pdf, .doc etc files. Along with that in the unzipped parent folder having CSV files. Now, what I need to do is to:
1 - Unzip the zip files and merge sub-folders by their names and load their files to respective name sub-folder in a newly created final folder.
2 - Merge all the CSV Files based in their names into one single CSV File.
File Structure:
abc0.zip -> having sub folders ab, ac, ad etc with files and abc.csv, adc.csv
abc1.zip -> having sub folders ab, ac, ad etc with files and abc.csv, adc.csv
....
abcn.zip -> having sub folders ab, ac, ad etc with files and abc.csv, adc.csv
FinalFolder->
ab -with all files from same name sub-folders
ac -with all files from same name sub-folders
ad -with all files from same name sub-folders
..
Final_abc.csv
Final_adc.csv
...
Tried So Far:
Base location
cd I:\LocalPath\Test
#Extract zip files to different location with same name in Unzip Folder (extracted folder name will contains .zip)
gci -Filter *.zip |ForEach-Object {Expand-Archive -Path $_ -DestinationPath Unzip_Location\$_ -Force}
# Copying and merging into single Final Folder from base Unzip location(failing to merge csv)
(Get-ChildItem 'Unzip_Location\*' -Directory).FullName | ForEach-Object { Join-Path $_ '*' } | Copy-Item -Destination 'I:\Local_Path\Final' -Recurse
Merge all CSVs with same name to Final Folder
'Code to be written'
My above code is working fine and merging all the sub-folders to final location but,I'm not sure how to merge the CSV Files from various sub-folders with same name into one single CSV File.
I'm not very good with Powershell and all this I did with some online search, any help would be appreciated. Thanks

Compress-Archive, exclude the containing folder

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*.*

How to copy a list of folder names in a folder to text file in Powershell?

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

Moving files based on their name and extension to a folder matching the name of the file?

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