We Recommend

C++ The Core Language C++ The Core Language
C++: The Core Language is for C programmers transitioning to C++. It's designed to get readers up to speed quickly by covering an essential subset of the language. The subset consists of features without which it's just not C++, and a handful of others that make it a reasonably useful language.


Posted By

darkphotn on 01/13/08


Tagged

script c console files rename


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

wbowers


C Console Scripting Framework


Published in: C++ 


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.


  1. #include <stdio.h>
  2. //#include <stdlib.h> //for system pause
  3.  
  4.  
  5. //USAGE (in DOS): dir blah | yourProgramName > outFile.txt
  6. // (use /b for JUST file and folder names)
  7. // (use /b /ad for JUST folder names)
  8.  
  9. void printsln(char *s) {printf("%s\n", s);}
  10.  
  11. void error(char *s){printsln(s); exit(1);}
  12.  
  13. bool ngets(char *s, int n) {
  14. int i = 0;
  15. char c;
  16. c = getchar();
  17. if (c==EOF) {s[i] = 0; return false;}
  18. while(c!='\n'){
  19. if(i>=n) error("input stream overflowed buffer");
  20. s[i++] = (char)c;
  21. c = getchar();
  22. }
  23. s[i] = 0;
  24. return true;
  25. }
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. char s[10000]; //note: possible (in theory) security hole
  30. while(ngets(s, 10000)) { //security hole closed.
  31.  
  32. printf("/new/%s\n", s, s); //TWEAK THIS LINE!!!
  33.  
  34. }
  35. //system("pause");
  36. return 0;
  37. }

Report this snippet 

You need to login to post a comment.