GameMonkey Script

GameMonkey Script Forums
It is currently Mon Mar 18, 2019 7:47 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sat Apr 24, 2004 7:43 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
One of the many ways to use GameMonkey is of course embedding it into a command line console app with a few basic bindings, which is exactly what the GME.exe program is.

A while back, in another forum, a guy called Supamodel asked how to use another utility or batch files to rename a bunch of files:
Quote:
Basically, for some stuff I'm doing, I need to rename some files, which have some numbers at the end of them. For brevity, let's just make the directory look like this:
drillhole109124567.asc
drillhole109124572.asc
drillhole109124666.asc
I want to extract the following string from the filenames - the last 5 digits, the . and the extension, so if we ran a loop that did that over the above files, you would get:
24567.asc
24572.asc
24666.asc
now, I also want to rename the original files given some other substring, let's call it %1, since it's going to be done in windows batch files.


Thinking that sounded like a simple job for GME, I had a go:
Just copy the script below into a file such as rename.gm and run it like 'gme.exe rename.gm' or 'gme.exe rename.gm -k drillhole1091 test' for a configured run.
Code:
// Example GameMonkey Script to rename a set of files as per forum question

// Init seach and replace values
prefix = arg[0]; // Grab seach prefix from first command line argument
if(!prefix)
{
  prefix = "drillhole1091"; // The default
}
prefixSize = prefix.Length();
newPrefix = arg[1]; // Grab new prefix from second command line argument
if(!newPrefix) // No command line args, so just use test
{
  newPrefix = "test";
}

// Find all appropriate files
fileList = table();
handle = system.FileFindFirst(`*.asc`); // Just fine .asc files for now
while(handle)
{
  if(handle.filename.Find(prefix) == 0) // Does file start with prefix?
  {
    fileList[tableCount(fileList)] = handle.filename; // Add to our list
    print("found file:", handle.filename); // Log file
  }
  handle = system.FileFindNext(handle);
}

// Now rename the files
foreach(filename in fileList)
{
  origFileName = filename;
  newFileName = newPrefix + origFileName.RightAt(prefixSize);
 
  print("old:",origFileName,"new:",newFileName); // Log names
 
  // No one has bound a 'rename' function yet, so let just call the OS
  command = `ren ` + origFileName + ` ` + newFileName;
  print("executing command:", command); // Log command
  system.Exec(command);
}

print("Script completed"); // Show we're finished if waiting for keypress or such


/*
Output from test run:


found file: drillhole109124567.asc
found file: drillhole109124572.asc
found file: drillhole109124666.asc
old: drillhole109124567.asc new: test24567.asc
executing command: ren drillhole109124567.asc test24567.asc
old: drillhole109124572.asc new: test24572.asc
executing command: ren drillhole109124572.asc test24572.asc
old: drillhole109124666.asc new: test24666.asc
executing command: ren drillhole109124666.asc test24666.asc
Script completed
*/


Another forum member, Hogwash, pointed out that this particular problem could be accomplished with a straight .bat file eg.
Code:
for %%f in (*.asc) do call :renum %1 %%f
goto :end

:renum
set end=%2
ren %2 %1%end:~-9%

:end


But as coders who appreciate the C syntax with functions and variables, a quick GM script merely using the GME.EXE program can be a powerful alternative to the good old .BAT.

There are much more exciting things to use GM for than command scripts and simple utility bindings. Hopefuly we'll hear some interesting testimonials over the next year.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group