Return to Snippet

Revision: 4601
at January 13, 2008 12:44 by darkphotn


Updated Code
#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\n", 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!='\n'){
   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\n", s, s);  //TWEAK THIS LINE!!!

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

Revision: 4600
at January 13, 2008 12:32 by darkphotn


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


//USAGE (in DOS):   dir blah | yourProgramName > outFile.txt

void printsln(char *s) {printf("%s\n", 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!='\n'){
   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.def\n", s, s);  //TWEAK THIS LINE!!!

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

Initial URL


Initial Description
This demonstrates how to make your own console "scripts" using C -- for example, you could capitalize every character that comes in.  Tweak the line labelled "TWEAK THIS LINE" in order to get it to do what you want.  This particular example will add "/new/" to the beginning of every filename and stick the result to a text file.  The listed DOS command can be stuck in a .BAT file and run from Windows like a program.

Initial Title
C Console Scripting Framework

Initial Tags
script, c, files

Initial Language
C++