Extracting Multiple 7z Files Overrides Same Folder - powershell

I'm currently working on a project where I take multiple 7z files and extract the contents of these files in a folder named the same way as the 7z file itself. I also apologize if something like this has been answered already; I spent time trying to investigate this issue but I can't seem to find anyone else who has had a similar issue.
For example:
a1.7z -> <targetpath>/a1/<contents within a1.7z>
The following shell line: a1.7z | % {& "C:\Program Files\7-Zip\7z.exe" "x" $_.fullname "-o<targetpath>\a1" -y -r}
Works like a dream, but only for one 7z file. However, whenever I start extracting a second 7z file, it won't create a new folder but instead will continue to add into the same first folder that is created; second folder is never made. When I manually highlight all of the 7z files I want to extract, right click and select "Extract to "*\", it does what I would like it to do but I can't figure out how to script this action. I should also mention that some of the 7z files, when extracted, can contain subfolders of the same name. I'm not sure if this is throwing off the recursion cycle, but I'm assuming this might be the case.
Any help or advice on this topic would be greatly appreciated!

If you get all the .7z files as IOFileInfo objects (Using get-ChildItem) you can use Mathias comment, as one way to do this with the pipeline, but I recommend you put this inside a loop and look for a better way to choose the names of the folders I.e. "NofFolder_$_.BaseName" just in case of more than 1 folder with the same name.
It really depends on the format you want.

Related

Scripting help for copying new items added to a set of directories

I have a set of folders which have new files being added to them regularly, but I have to process those files as they come in. This can be a time consuming process to dig into each folder one by one. I need to figure out how to write a script that will filter out the new files, and copy them into a new directory.
So far I have figured out how to use the Get-ChildItem -Path -Recurse command in powershell to list the new items in the corresponding folders as shown in the third script on this Microsoft page.
So I can see the new files in their folders. How do I copy those items to the destination folder while replicating their original folder structure? I want to be able to recreate the original folders so that I can just overwrite the originals with the edited versions later.
I discovered robocopy, and was able to use it to solve my problem. The /maxage:x option was perfect for my needs.

Short path if files is in the same directory

If dataset and Matlab script are both in the same folder, is it possible to denote to dataset in my script as '../dataset.csv'? I tried it in my script but it doesn't seem to be working.
The two dots refer to the parent folder. Try './dataset.csv' or even 'dataset.csv'.

Create windows script to traverse all subfolders of some folder, and delete a certain folder and all of it's contents if it exists

here's the trick. Supposing I want to free up some space, and run that script in F:\UnrealProjects, which has a bit over 50-60 subfolders, I would like that script to traverse all of it's subfolders, check whether it contains a folder named Binaries, Build, Intermediate and delete everything in it including the folder(s) itself it it exists.
I'm not quite lazy to do it manually once, but when reopening some projects, they do partially or fully rebuild themselves and that eats up space since I really don't need them fully built at all times. I just need to have them archived :) And build only the ones i do want to have built.
Thanks for any help in advance guys (or gals), it's appreciated alot.
If however someone has a better idea to retag this question, I'm all ears and very open to suggestions.
Something like this, should do it
ls -Recurse -Include Build,Intermediates,Binaries -Attribute Directory | Remove-Item -Force -Recurse

Copy all files of one type from directory into one folder

This is a new one for me so I'm pretty much flying blind.
I have a folder at 192.168.1.2\mainFolder that contains folder1, folder2, and folder3. Inside each of those folders are a handful of different file extentions, and a couple of files of each type. I need to take all files that exist inside mainFolder of the .dep type, and copy them to 192.168.1.2\copyFolder
copyFolder will not have any folders inside it, but just many many files.
What is the best way to go about doing this? I have been told by TPTB that robocopy would be helpful, however I have never used robocopy and thought you guys may know of something better
So you don't want .dep files inside folder1, 2 etc.? Robocopy / xcopy is usually a good choice. Powershell is slow for such a simple operation. If you just want the .dep files in mainfolder but not those inside the subfolders, try:
robocopy \\192.168.1.2\mainFolder \\192.168.1.2\copyFolder *.dep

rename files& directories {searchstr} with {replacestr}

I have an application (Templify) that creates a templatized directory structure, but it seems to not be able to rename the "__NAME__" with what I've identified as the target.
This is fine if I can find a clean way to rename all files & directories with my replacement text.
I found a rename.pl method that renames files, and I found some code that removes underscores in file names and replaces it with spaces... but when I modify the code to put in my search terms, it never seems to work.
So, basically, I need to replace "__NAME__" with something like "Project-Name".
I'm happy to modify the search strings for each future reuse, but I'd love to figure out how to create a file to which I can pass ARGS.
I'm on XP and can use cygwin (cygwin doesn't seem to have 'rename' which makes it hard to locate linux-type solutions with using the function called 'rename'....)
I did find this which is easy to use for files in the current directory, but I don't know enough to tell it to recurse into sub-directories.
Any help would be great.
Thanks,
Scott
From cygwin:
find /cygdrive/c/mytree -type f | perl -ne 'rename $_, $1/Project-Name if m[^(.*)/__NAME__$]'
Or using python:
import os
for root, dirs, files in os.walk("C:\\mytree"):
for filename in files:
if filename == "__NAME__":
os.rename(os.path.join(root, filename), os.path.join(root, "Project-Name"))