====== Useful Scripts ======
Below are a random selection of scripts for a random set of tasks! They are written in different languages and in some cases are operating system dependent.
To download a script you can just click on the file name above the code (note some scripts only have a downloadable version).
===== File Utilities =====
==== Multiple File Diff ====
|**Description:**|Diffs all files within two directories, using underlying 'diff' tool.|
|**Language:**|Perl|
|**Example:**|//perl diffallfiles.pl source/directory target/directory// |
# S.Chudley (www.chudley.me) : Diff all files between two directory structures
my %l_src = ();
my %l_dst = ();
&list_files($ARGV[0], \%l_src);
&list_files($ARGV[1], \%l_dst);
foreach my $l_file (sort keys %l_src)
{
print "$l_file: ";
if (defined $l_dst{$l_file})
{
my @l_results = `diff \"$ARGV[0]\\$l_file\" \"$ARGV[1]\\$l_file\"`;
if ($? != 0)
{
print "\n\n";
foreach my $l_line (@l_results) { print " ".$l_line; }
print "\n";
}
else { print "No change.\n"; }
}
else { print "Not found in target directory.\n"; }
}
foreach my $l_file (sort keys %l_dst)
{
if (!defined $l_src{$l_file})
{
print "$l_file: Not found in source directory.\n";
}
}
sub list_files
{
local ($a_dir, *a_list) = @_;
chomp(my @l_files = `dir /B \"$a_dir\"`);
foreach my $l_name (@l_files)
{
my $l_file = $a_dir."\\".$l_name;
if (-f $l_file)
{
my $l_path = $ARGV[1];
$l_path =~ s/\\/\\\\/g;
if ($l_file =~ /^$l_path/)
{ $l_file =~ s/^$l_path\\//; }
else
{
$l_path = $ARGV[0];
$l_path =~ s/\\/\\\\/g;
$l_file =~ s/^$l_path\\//;
}
$l_file =~ s/^\.\\//;
$a_list{$l_file} = 1;
}
elsif (-d $l_file) { &list_files($l_file); }
}
}
----
==== File System Fill Up ====
|**Description:**|Script to fill up a drive, leaving a given number of bytes free.|
|**Language:**|Perl|
|**Example:**|//perl fillup.pl 1024768//|
# S.Chudley (www.chudley.me) : Script to fill up a drive, leaving a given number of bytes free
my $leave = $ARGV[0];
if ($leave eq "")
{
print "Specify amount of space to leave (as bytes).\n";
exit(1);
}
print "Filling buffer...\n";
$block_size = 1024 * 1024;
my $data_buffer = "";
for (my $i=0;$i!=$block_size;$i++) { $data_buffer .= " "; }
if ($leave >= 2000 * $block_size)
{
print "Can't leave more than ".(2000 * $block_size / 1024)." kb free!\n";
exit(1);
}
my $free_space = &get_free();
print "Drive has ".($free_space/1024)." kb free...\n";
if ($free_space >= 2097152000)
{
print "Making a big file (2048 MB) called file.big...";
&make_file("file.big", 2000 * $block_size);
print " Done.\n";
}
$free_space = &get_free();
print "Drive now has ".($free_space/1024)." kb free...\n";
if ($free_space <= $leave)
{
print "Arn't $leave bytes free anyway!\n";
exit(1);
}
my $multi = int($free_space / (2000 * $block_size));
print "Spinning out $multi copies of this file (total ".(2000 * 1024 * $multi)." kb)...\n";
for (my $i=0;$i!=$multi;$i++)
{
`copy file.big file.$i.big`;
if ($? != 0)
{
print "Oops? File write failed?\n";
exit(1);
}
my $pc = ($i / $multi) * 100;
$pc = substr($pc, 0, 5);
print " Created ".($i + 1)." files [".$pc." %]\n";
}
$free_space = &get_free();
print "Drive now has ".($free_space/1024)." kb free...\n";
my $remains = $free_space - $leave;
print "Consuming up remaining ".($remains / 1024)." kbs... ";
&make_file("remaining.big", $remains);
print "Done\n";
$free_space = &get_free();
print "Drive now has ".($free_space/1024)." kb free...\n";
if ($free_space ne $leave)
{
print "Erm? Something went wrong\n";
exit(1);
}
exit(0);
sub make_file
{
my ($name, $size) = @_;
open(OUT,">$name") or die "Can't create file '$name'!\n";
my $done = 0;
my $blocks = int($size / $block_size);
while ($done < ($blocks * $block_size)) { print OUT $data_buffer or die "Oops? Write failed in make_file()?"; $done += $block_size; }
for (my $i=$done;$i!=$size;$i++) { print OUT " " or die "Oops? Write failed in make_file()?"; }
close(OUT);
}
sub get_free
{
my @free_txt = `dir /-C`;
my $free = 0;
foreach (@free_txt) { if ($_ =~ m/\s+([0-9,]+)\s+bytes\s+free/) { $free = $1; $free =~ s/,//g; } }
return $free;
}
----
==== For Each File ====
|**Description:**|Executes a passed command on every file within a target directory. |
|**Language:**|Perl|
|**Example:**|//perl foreachfile.pl c:\scratch *.bmp gzip FILE//|
# S.Chudley (www.chudley.me) : Executes a passed command on every file within a target directory
my $l_dir = $ARGV[0];
my $l_ext = $ARGV[1];
my $l_cmd = "";
for (my $i = 2; $i < @ARGV;$i++) { $l_cmd .= " ".$ARGV[$i]; }
if ($l_dir eq "" or $l_cmd eq "" or $l_cmd !~ /FILE/)
{
print "foreach_file \n";
print "\nWhere contains one of more occurence of FILE, which will be replaced by the file being processed.\n";
exit(1);
}
chdir($l_dir);
foreach (`dir $l_ext /B`) # For Windows
# foreach (`ls $l_ext -1`) # For Linux
{
chomp;
my $l_loc_cmd = $l_cmd;
$l_loc_cmd =~ s/FILE/$_/g;
print `$l_loc_cmd`;
}
----
==== Lock File ====
|**Description:**|Lock a file exclusively for read/write.|
|**Language:**|Perl|
|**Example:**|//perl lockit.pl TheFile.txt//|
# S.Chudley (www.chudley.me) : Lock a file
use Fcntl qw (:flock);
my $file = $ARGV[0];
if (! -f $file) { print "Can't find '$file'\n"; exit(1); }
open(FILE, ">>$file");
flock(FILE, 2);
print "Type something and hit enter to unlock: ";
;
close(FILE);
----
==== Modify File Access Time ====
|**Description:**|Modifies the file last access time.|
|**Language:**|Perl|
|**Example:**|//perl mod_file_access_time.pl file.txt//|
# S.Chudley (www.chudley.me) : Modify file access time
use Time::Local;
$when = timegm(0,0,10,12,8,10);
utime $when, $when, $ARGV[0];
----
==== Screwup a File ====
|**Description:**|Corrupts a file by writing random bytes into it.|
|**Language:**|Perl|
|**Example:**|//perl screwup.pl UnluckyFile.txt//|
# S.Chudley (www.chudley.me) : Corrupt a file
my $file = $ARGV[0];
if (! -f $file)
{
print "Can't find '$file'\n";
exit(1);
}
my $size = (stat($file))[7];
open(INF, "+>".$file) or die "Can't open '$file' for writing!";
binmode INF;
my $k = $size / 1000;
for (my $i = 1;$i != 1000;$i++)
{
my $pos = int($k * $i);
&blat($pos);
}
close(INF);
sub blat
{
my $pos = shift;
seek INF, $pos, 0;
print INF "\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255";
}
===== Programming Utilities =====
==== C++ Line Count ====
|**Description:**|Count number of lines in C++ app, where files end cc, c, hh or h.|
|**Language:**|Perl (Windows)|
|**Example:**|//perl line_count.pl//|
# S.Chudley (www.chudley.me) : Count number of lines in C++ app, where files end cc|c|hh|h
my $l_extensions = "cc|c|hh|h|cpp|hpp";
my $l_count = 0;
&count_dir(".");
sub count_dir
{
my $a_path = shift;
chomp(my @l_files = `dir /B $a_path`);
foreach my $l_name (@l_files)
{
my $l_file = $a_path."\\".$l_name;
if (-f $l_file and $l_file =~ m/\.($l_extensions)$/i)
{
my $l_c = 0;
open(INF, $l_file);
while (my $l_line = )
{
if ($l_line !~ m/^\s*\/\// and
$l_line !~ m/^\s*\*/ and
$l_line !~ m/^\s*\/\*/ and
$l_line !~ m/^\s*#/ and
$l_line !~ m/^\s*$/) { $l_c++; }
}
close(INF);
$l_file =~ s/^\.\\//;
print $l_file.": ".$l_c."\n";
$l_count += $l_c;
}
elsif (-d $l_file) { &count_dir($l_file); }
}
}
print "\nTotal: $l_count\n";
----
==== Nuke Tabs in Source ====
|**Description:**|Replaces all tabs with spaces within all cc, c , hh or h files within a given directory recursively.|
|**Language:**|Perl (Windows)|
|**Example:**|//perl nuketabs.pl//|
# S.Chudley (www.chudley.me) : Replace tabs with spaces in source code
my $l_extensions = "cc|c|hh|h";
&do_dir(".");
sub do_dir
{
my $a_path = shift;
chomp(my @l_files = `dir /B $a_path`);
foreach my $l_name (@l_files)
{
my $l_file = $a_path."\\".$l_name;
if (-f $l_file and $l_file =~ m/\.($l_extensions)$/)
{
my $l_tfile = $l_file.".tmp";
print "Processing $l_file...\n";
open(INF, $l_file);
open(OUTF, ">".$l_tfile);
while (my $l_line = )
{
if ($l_line !~ m/^\s*\/\// and
$l_line !~ m/^\s*\*/ and
$l_line !~ m/^\s*\/\*/ and
$l_line !~ m/^\s*#/ and
$l_line !~ m/^\s*$/) { $l_line =~ s/\t/ /g; }
print OUTF $l_line;
}
close(OUTF);
close(INF);
`copy $l_tfile $l_file`;
if ($? != 0) { die "Failure with copy on $l_tfile to $l_file!"; }
`del $l_tfile`;
if ($? != 0) { die "Failure to delete $l_tfile!"; }
}
elsif (-d $l_file) { &do_dir($l_file); }
}
}
===== Windows System Control =====
==== Adjust Windows Volume ====
|**Description:**|Allows you to set the master Windows volume via a script.|
|**Language:**|VBScript|
|**Example:**|//adjust_windows_volume.vbs//|
'S.Chudley (www.chudley.me) : Set Windows master volume (hAE = DOWN, hAF = UP)
Set WshShell = CreateObject("WScript.Shell")
For i=1 To 100
WshShell.SendKeys(chr(&hAE))
Next
----
==== Toggles Windows Mute ====
|**Description:**|Toggels the master mute setting on Windows.|
|**Language:**|VBScript|
|**Example:**|//toggle_mute_windows_volume.vbs//|
'S.Chudley (www.chudley.me) : Toggels Windows master mute
set oShell = CreateObject("WScript.Shell")
oShell.run"Sndvol32"
WScript.Sleep 1500
oShell.SendKeys"{TAB 2} "
oShell.SendKeys"%{F4}"
===== Windows Utilities =====
==== Perl In a Batch ====
|**Description:**|How to run Perl embedded into a DOS batch file.|
|**Language:**|DOS Batch|
|**Example:**|//perl_in_a_batch.bat//|
@echo off
REM S.Chudley (www.chudley.me) : Embed Perl into a batch script
echo Now we're in a batch file
perl -x -S %0 %*
goto endofperl
@rem ';
#!/perl
print "Now we're in a Perl script - type something... ";
chomp(my $a = );
print "You typed '$a'\n";
__END__
:endofperl
echo Now we're back in a batch file!