Tcl: Delete directory recursively - improving performance using threads - pthreads

I wrote a small proc to recursively delete %TEMP% dir on windows which got quite bulky over each execution cycle.
But it seemed to work slow - it was way faster to manually select different folders inside the %TEMP% path and run deletion on it.
Is there a possibility to use threads / pthreads to delete folders in parallel.
Or an alternate solution.
Need help in the following code.
proc DirRecursion {dir args} {
set options(-files) ""
set options(-fileTypesFilter) "ALL"
array set options $args
upvar $options(-files) lf
#Performing action deletion on files for dir $dir ... please be patient as this might take some time
if ![catch { append lf " [glob -directory $dir -type f *]"}] {
} "Found following files to delete in dir $dir"
foreach f [set lf [regsub -all {\\} $lf {/}]] {
catch {file delete -force $f}
}
}
if ![catch { set ld [glob -directory $dir -type d *]}] {
foreach d $ld {
DirRecursion $d -files lf
}
} else {
puts "No directories to delete in dir $dir" debug
}
catch {file delete -force $dir}
}

Well, you could do that (probably by sending requests to file delete -force into a thread pool), it's very unlikely to help at all since the bottleneck will be in the actual reading and writing of the disk, even on systems with an SSD. With an HDD, that's definitely what you'll be waiting on.
You are aware that file delete -force already handles the recursive delete case? It's probably faster than what you've written too. Since I suspect you don't actually want to delete the %TEMP% path itself (as that's a bit special to other programs in your session too), you can instead do something like this:
file delete -force {*}[glob -directory $::env(TEMP) *]
Or delete the contents of the appropriate subdirectory instead. One line instead of a complicated procedure…
Edit
This will try to delete each entry individually and will continue on even if a directory cannot be deleted due to permission errors.
foreach {entry} [glob -directory $::env(TEMP) *] {
catch { file delete -force $entry }
}

Related

Want to recursively list full path of all files in a directory structure and pipe into text file without wordwrap

I want to get a listing of the full path of every file in a multi-level directory structure, and put that information into a text file - BUT, I don't want long path's to be word-wrapped - I want to have the full path of each line on a single line in the text regardless of how long that line is.
I started out with this command:
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() }
I then have tried various means to try and redirect this output to a text file. First I tried this (but it didn't work):
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() } | Out-File C:\temp\out.txt
Next, I tried just piping all of the StdErr, StdOut etc to the text file thus:
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() } *> C:\temp\out.txt
This last command got close - but still the longer path's are wrapped onto the next line. I have been trying all sorts of things to try and stop this (wordwrap) from happening. Can anyone tell me how to get the output from the above command to go into a text file with no word-wraps (even) for very long path's?
thanks heaps,
David. :-)

I need to create a script that will check a files size, warn user, move files and link to new location

We are trying to keep as little end-user data on our companies thin-clients, so I need to create a script that will run when a file is saved locally. I'm completely new to power shell so I do apologize for the lack of knowledge.
Check file size
if (file size is equal or greater than "x"mb) output a warning "file is larger than allowed, moving file to you cloud drive (usually the K: drive)
I'd imaging I'd use Move-Item -destination "k:\My Documents\"
create a link or shortcut to the new file location.
I'm sure I will need more data at some point. I will take any/all the help I can get.
$Directory = Get-ChildItem "D:\Download" #Directory I care about
$SizeLimit = "200000" #Size limit in bytes
foreach ($File in $Directory) {
if ($File.Length -ge $SizeLimit) {
# Notify the user with a pop-up
# the coding for said popup
# Move the file
Move-Item $File.FullName K:\
mklink.exe K:\$File.Name $File.FullName

I cannot get Powershell 2.0 to move a file from one path to another

I need to make changes to to the following Powershell script, but am having a dickens of a time getting the resulting files to write to a different path...let's call it $destPath.
Consider:
Get-ChildItem $sourcePath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]|-|,|\(|\)', '') ) }
Based on my understanding of move syntax, $_.fullname is my original file, and $_.FullName -replace... is the NEW filename. Hoever, when I try to use $destPath.FullName -replace I get an error that an empty filename is not legal. Obviously, Powershell is not recognizing that as a valid pathname for the move command.
What am I missing?
Since you did not mention $destPath in context - maybe you did not define the variable $destPath at all? It is quite unlikely but just trying to narrow things down.
Another way to make this work:
Rename child-items first at original location. Then move them.
get-childitem *.txt | rename-item -newname {$_.name -replace 'o','O'}
get-childitem *.txt | % {move-item -path $_.fullname -destination .\Test-files}
But I prefer your one liner:)

how to set the path to where aapt add command adds the file

I'm using aapt tool to remove some files from different folders of my apk. This works fine.
But when I want to add files to the apk, the aapt tool add command doesn't let me specify the path to where I want the file to be added, therefore I can add files only to the root folder of the apk.
This is strange because I don't think that developers would never want to add files to a subfolder of the apk (res folder for example). Is this possible with aapt or any other method? Cause removing files from any folder works fine, and adding file works only for the root folder of the apk. Can't use it for any other folder.
Thanks
The aapt tool retains the directory structure specified in the add command, if you want to add something to an existing folder in an apk you simply must have a similar folder on your system and must specify each file to add fully listing the directory. Example
$ aapt list test.apk
res/drawable-hdpi/pic1.png
res/drawable-hdpi/pic2.png
AndroidManifest.xml
$ aapt remove test.apk res/drawable-hdpi/pic1.png
$ aapt add test.apk res/drawable-hdpi/pic1.png
The pic1.png that will is added resides in a folder in the current working directory of the terminal res/drawable-hdpi/ , hope this answered your question
There is actually a bug in aapt that will make this randomly impossible. The way it is supposed to work is as the other answer claims: paths are kept, unless you pass -k. Let's see how this is implemented:
The flag that controls whether the path is ignored is mJunkPath:
bool mJunkPath;
This variable is in a class called Bundle, and is controlled by two accessors:
bool getJunkPath(void) const { return mJunkPath; }
void setJunkPath(bool val) { mJunkPath = val; }
If the user specified -k at the command line, it is set to true:
case 'k':
bundle.setJunkPath(true);
break;
And, when the data is being added to the file, it is checked:
if (bundle->getJunkPath()) {
String8 storageName = String8(fileName).getPathLeaf();
printf(" '%s' as '%s'...\n", fileName, storageName.string());
result = zip->add(fileName, storageName.string(),
bundle->getCompressionMethod(), NULL);
} else {
printf(" '%s'...\n", fileName);
result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
}
Unfortunately, the one instance of Bundle used by the application is allocated in main on the stack, and there is no initialization of mJunkPath in the constructor, so the value of the variable is random; without a way to explicitly set it to false, on my system I (seemingly deterministically) am unable to add files at specified paths.
However, you can also just use zip, as an APK is simply a Zip file, and the zip tool works fine.
(For the record, I have not submitted the trivial fix for this as a patch to Android yet, if someone else wants to the world would likely be a better place. My experience with the Android code submission process was having to put up with an incredibly complex submission mechanism that in the end took six months for someone to get back to me, in some cases with minor modifications that could have just been made on their end were their submission process not so horribly complex. Given that there is a really easy workaround to this problem, I do not consider it important enough to bother with all of that again.)

How do you duplicate a file in XCode?

Anyone know a good solution?
So far I have not found a better way than using File>New file and then copying contents from old file to new.
You can probably duplicate in Finder and re-import but that's almost same amount of work: switching to finder, duplicate, import new files.
Doing this with one class is not so hard, but what to do if you need to generate 10+ similar Classes based on superclass.
In Eclipse you select file and then copy/paste it in same folder. In finder there's Duplicate.
There's a menu Edit > Duplicate. But it's ALWAYS disabled. I tried selecting various files, classes, methods. It's still disabled.
In XCode 4.2 (I know this is an old question) there is Duplicate under the File menu.
Select the file (you can select multiple files but it doesn't appear to do anything useful) in the Project Navigator and then File->Duplicate. Hooray!
In Xcode 4.5 we can duplicate using File-> Duplicate or cmd + shift + S
"Duplicate" is enabled for targets in XCode (pretty much nothing else that I know of).
If you have a substantial number of subclasses with the same starting point to replicate, why not make a class template from it? Then you can just use file->New to make new instances. It's fairly quick to do.
This is probably the simplest example:
http://www.macresearch.org/custom_xcode_templates
Otherwise, I'd simply duplicate the files in Finder as many times as you need, name them, and drag them into XCode en-masse.
Careful!
When you use duplicate ( CMD + Shift + S ) - Xcode have a problem with indexing headers.
Also when u want to make a refactoring it can be next error window:
So there a couple of ways what to do, to fix that.
Delete derived data from menu Window > Projects. Restart Xcode.
Product > Clean
You could use "Save As..."; you'd still have to go back and re-add the original files to the project, though.
It wouldn't be such a bad way to do a bunch of related classes, though: edit file, Save As "class2", edit file, Save As "class3", etc., then "Add Existing Files" and re-add all of the files but the last to your project.
I use the following perl script to duplicate a file pair in the Terminal. You give it the base name of the original and new file, and it copies the header and implementation (c/cpp/m/mm) file, then replaces all occurances of the base name with the new name, then adds them to subversion. You still have to add the new files in to Xcode and adjust the creation date in the comment (I've got a Keyboard Maestro macro for that), but its quicker than doing a lot of the steps manually. I operate with a Terminal window and four tabs pre-set to the Project, Source, Resources, and English.lproj directory which gives quick access for a lot of operations.
#!/usr/bin/perl
use lib "$ENV{HOME}/perl";
use warnings;
use strict;
our $cp = '/bin/cp';
our $svn = '/usr/bin/svn';
our $perl = '/usr/bin/perl';
our $source = shift;
our $add = 1;
if ( $source =~ m!^-! ) {
if ( $source eq '-a' || $source eq '--add' ) {
$add = 1;
$source = shift;
} elsif ( $source eq '-A' || $source eq '--noadd' ) {
$add = undef;
$source = shift;
} else {
die "Bad arg $source";
}
}
our $dest = shift;
die "Bad source $source" unless $source =~ m!^(.*/)?[A-Za-z0-9]+$!;
die "Bad dest $dest" unless $dest =~ m!^(.*/)?[A-Za-z0-9]+$!;
my $cpp;
$cpp = 'c' if ( -e "$source.c" );
$cpp = 'cpp' if ( -e "$source.cpp" );
$cpp = 'mm' if ( -e "$source.mm" );
$cpp = 'm' if ( -e "$source.m" );
die "Missing source $source" unless -e "$source.h" && -e "$source.$cpp";
die "Existing dest $dest" if -e "$dest.h" && -e "$dest.$cpp";
our $sourcename = $source; $sourcename =~ s!.*/!!;
our $destname = $dest; $destname =~ s!.*/!!;
print "cp $source.h $dest.h\n";
system( $cp, "$source.h", "$dest.h" );
print "s/$sourcename/$destname in $dest.h\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.h" );
print "cp $source.$cpp $dest.$cpp\n";
system( $cp, "$source.$cpp", "$dest.$cpp" );
print "s/$sourcename/$destname in $dest.$cpp\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.$cpp" );
if ( $add ) {
print "svn add $dest.$cpp $dest.h\n";
system( $svn, 'add', "$dest.$cpp", "$dest.h" );
}
In my case, one of my folder changed from one place to another place.
I have "Home" folder in Controller folder, but unfortunately it's moved from Controller folder to Manager folder.
I checked many times everything fine, but I'm getting Command PrecompileSwiftBridgingHeader failed with a nonzero exit code
But after 2 hours i realised, my folder structure changed.

Resources