Tag Archive for files

C Console Scripting Framework

#include <stdio.h>
//#include <stdlib.h> //for system pause

//USAGE (in DOS):   dir blah | yourProgramName > outFile.txt
// (use /b for JUST file and folder names)
// (use /b /ad for JUST folder names)

void printsln(char *s) {printf("%s
", s);}

void error(char *s){printsln(s); exit(1);}

bool ngets(char *s, int n) {
int i = 0;
char c;
c = getchar();
if (c==EOF) {s[i] = 0; return false;}
while(c!='
'){
if(i>=n) error("input stream overflowed buffer");
s[i++] = (char)c;
c = getchar();
}
s[i] = 0;
return true;
}

int main(int argc, char *argv[])
{
char s[10000]; //note: possible (in theory) security hole
while(ngets(s, 10000)) { //security hole closed.

printf("/new/%s
", s, s);  //TWEAK THIS LINE!!!

}
//system("pause");
return 0;
}

source

Batch rename files in directory

for F in * ; do NF=`echo $F | perl -lne "s/ /_/g; s/._/./g; s/[',-]//g; print lc"` ; mv "$F" "$NF" ; done

source

batch rename files

for FILE in * ; do NEWFILE=`echo $FILE | sed 's/-//g'` ; mv "$FILE" $NEWFILE ; done

source

Get the extension of a filename

preg_replace("/.*?./", '', 'photo.jpg');

source

PHP File Editor

<?php
$loadcontent = "test-edit.php";

if($_POST['save_file']) {
$savecontent = stripslashes($_POST['savecontent']);
$fp = @fopen($loadcontent, "w");
if ($fp) {
fwrite($fp, $savecontent);
fclose($fp);
print "<html><head><META http-equiv="refresh" content="0;URL=$_SERVER[PHP_SELF]"></head><body>";

}
}
$fp = @fopen($loadcontent, "r");
$loadcontent = fread($fp, filesize($loadcontent));
$lines = explode("
", $loadcontent);
$count = count($lines);
$loadcontent = htmlspecialchars($loadcontent);
fclose($fp);
for ($a = 1; $a < $count+1; $a++) {
$line .= "$a
";
}
?>

<p><font face="tahoma">Simply edit the page and hit save!</font></p>

<form method=post action="<?=$_SERVER[PHP_SELF]?>">
<input type="submit" name="save_file" value="Save"><br /><br />
<table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="3%" align="right" valign="top"><pre style="text-align: right; padding: 4px; overflow: auto; border: 0px groove; font-size: 12px; font-face:verdana;" name="lines" cols="4" rows="<?=$count+3;?>"><?=$line;?></pre></td>
<td width="97%" align="left" valign="top"><textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px; font-face:verdana;" name="savecontent" cols="150" rows="<?=$count;?>" wrap="OFF"><?=$loadcontent?>

source

search and replace across multiple files with Perl

#print the result of search-and-replace to the terminal
perl -pe 's/bart/milhouse/g' test.html

#search-and-replace, with backup
#leave the suffix off of -i to overwrite
perl -i.bak -pe 's/bart/milhouse/g' test.html

#echo the number of lines in a file
perl -lne 'END { print $t } @w = /(w+)/g; $t += @w' test.html

#cat file with line numbers
# -p prints $_ each iteration
perl -pe '$_ = "$. = $_"' test.html

# recursive search-and-replace, only on shells that support file globs
perl -i.bak -pe 's{bart}{milhouse}' **/*html

source

Reading a file Asynchronously with ActionScript 3

// Imports
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.events.ProgressEvent;
import flash.events.Event;

// Declare the FileStream and String variables

private var _fileStream:FileStream;

private var _fileContents:String;

private function onCreationComplete():void // Fired when the application has been created

{

var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file

myFile = myFile.resolve("mySampleFile.txt"); // Point it to an actual file

_fileStream = new FileStream(); // Create our file stream

_fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress); // Add our the progress event listener

_fileStream.addEventListener(Event.COMPLETE, onFileComplete); // Add our the complete event listener

_fileStream.openAsync(myFile, FileMode.READ); // Call the openAsync() method instead of open()

}
private function onFileProgress(p_evt:ProgressEvent):void // Event handler for the PROGRESS Event
{
_fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1"); // Read the contens of the file and add to the contents variable

fileContents_txt.text = _fileContents; // Display the contents. I've created a TextArea on the stage for display
}

private function onFileComplete(p_evt:Event):void // Event handler for the COMPLETE event

{
_fileStream.close(); // Clean up and close the file stream
}

source

using find to list old files based on date

find /var/log -mtime -14 -type f -exec ls -al {} ;

source

rsync backup directory

rsync -a --delete /cygdrive/e/ /cygdrive/c/nsussman_thumb_drive

source

rename – Larry Wall’s filename fixer

#!/usr/bin/perl
# -w switch is off bc HERE docs cause erroneous messages to be displayed under Cygwin
#From the Perl Cookbook, Ch. 9.9
# rename - Larry's filename fixer
$help = <<EOF;
Usage: rename expr [files]

This script's first argument is Perl code that alters the filename (stored in $_ ) to reflect how you want the file renamed. It can do this because it uses an eval to do the hard work. It also skips rename calls when the filename is untouched. This lets you simply use wildcards like rename EXPR * instead of making long lists of filenames.

Here are five examples of calling the rename program from your shell:

% rename 's/.orig$//'  *.orig
% rename 'tr/A-Z/a-z/ unless /^Make/'  *
% rename '$_ .= ".bad"'  *.f
% rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'  *
% find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'

The first shell command removes a trailing ".orig" from each filename.

The second converts uppercase to lowercase. Because a translation is used rather than the lc function, this conversion won't be locale-aware. To fix that, you'd have to write:

% rename 'use locale; $_ = lc($_) unless /^Make/' *

The third appends ".bad" to each Fortran file ending in ".f", something a lot of us have wanted to do for a long time.

The fourth prompts the user for the change. Each file's name is printed to standard output and a response is read from standard input. If the user types something starting with a "y" or "Y", any "foo" in the filename is changed to "bar".

The fifth uses find to locate files in /tmp that end with a tilde. It renames these so that instead of ending with a tilde, they start with a dot and a pound sign. In effect, this switches between two common conventions for backup files
EOF

$op = shift or die $help;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}

source