mIRC Homepage
Posted By: sparta rename file - 16/10/07 09:09 AM
I have a "background system" in my script, and i use a while loop to list all *.jpg files in a directory, this working just fine. Now to my problem, i need all images to be named as bla1.jpg - bla2.jpg and so on, any way to do this? and if i have lets say 3 *.jpg files in the directory, then they are named bla1.jpg - bla2.jpg - bla3.jpg , now i put in test.jpg, then it should be renamed to bla4.jpg, any way to do this?

rename <filename> <newfilename>

will rename the file, but i cant solve the thing with the image number, if bla3.jpg = free use it, or if it's taken take bla4.jpg instead..
Posted By: Riamus2 Re: rename file - 16/10/07 10:28 AM
First, $findfile with its internal loop would probably be preferable to using a while loop to list files.

Second, if you zero pad the numbers so that the numbers can go high enough and stay the same number of digits, then you can use /inc on the last X characters in the filename (before the .jpg) and zero pad it again.

By zero padding, I mean:

0001
0002
0003
0004
...
0110

instead of:

1
2
3
4
...
110

With it zero padded, you always know how many characters make up the number that you want to /inc.
Posted By: jaytea Re: rename file - 16/10/07 10:44 AM
if they all have the same prefix (bla*.jpg) you can just count the current number of matching files with $findfile(dir,bla*.jpg,0) and inc that by 1. obviously this is flawed, if you delete a file then add another, it'll mess up

this is analogous to what i've seen people doing with INI files, variables etc. to maintain a collection of items they append numbers to enforce order (and try keep it sequential, from 1 to n). but instead, their order alone could just as easily serve as an index. this doesn't help you that much with $findfile (the files are returned to you in alphabetical order, not order of creation) but you can maintain a list in a text file of the filenames without worrying about any of this renaming and keeping the suffixes in sequence. just something to think about!
Posted By: sparta Re: rename file - 16/10/07 12:32 PM
This is what i use:
Code:
on *:dialog:backg:*:*: {
  if ($devent == init) {
    did -a $dname 4 %mybg
    var %images = 1
    var %count = $findfile($mircdirsystem\images\backgrounds\,*.jpg,0,1,)
    while (%images <= %count) {
      did -a $dname 5 $nopath($mircdirsystem\images\backgrounds\ bla. $+ %images )
      inc %images
    }
  }
}

as you see i use $findfile in it, but cant make it identify the other images in the folder backgrounds, then rename them as i want..
Posted By: sparta Re: rename file - 18/10/07 09:27 AM
I got this code:
Code:
alias testar {
  var %d = $qt($mircdirsystem\images\backgrounds\),%x = $findfile(%d,*.jpg,1,0)
  echo -s X1: %x
  while ($findfile(%d,*.jpg,%x,0)) {
    echo -s X2: %x $v1
    if (bla* !iswm $nopath($v1)) { echo -s V: $v2 | var %nb = %nb $v2 }
    else { var %b = %b $regsubex($v2,/^bla/,) }
    dec %x
  }
  var %x = 0
  while (%x < $right(%b,1)) {
    inc %x
    if (%x !isin %b) { var %c = %c %x }
  }
  while (%nb) {
    if (%b = $regsubex(%b,/\d/g,\n)) {
      var %nn = $calc($right(%b,1)+1)
    }
    else {
      var %nn =  $gettok(%c,1,32)
      var %c = $remtok(%c,%nn,1,32)
    }
    var %onb = $gettok(%nb,1,32)
    var %nb = $remtok(%nb,%onb,1,32)
    rename $+(%d,%onb) $+(%d,bla,%nn,.jpg)
  }
}

And now to the problem, when i trigger the script i see this in my status window (the echo -s X1: %x and echo -s X1: %x)

X1: C:\mIRC\system\images\backgrounds\bla1.jpg
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5

as you see it echo the same image all the time, after like 200 echo's i get this error "* /set: line too long (line 50, script1.mrc)", now i add one more echo to the code:

I adding this: echo -s %nb

X1: C:\mIRC\system\images\backgrounds\bla1.jpg
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5

look at the row with only 5's in it, it will be filled up. it starting with one 5 then it adding more and more untill it halts with the same error as befor, the last lines looks like:

X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
X2: C:\mIRC\system\images\backgrounds\bla1.jpg 5
V: 5
-
* /set: line too long (line 50, script1.mrc)

And I havent been able to solve it, I got this code from a person on irc, i dont understand how it works, so im not able to fix the problem..

line 50 = if (bla* !iswm $nopath($v1)) { echo -s V: $v2 | var %nb = %nb $v2 }
Posted By: Horstl Re: rename file - 18/10/07 03:12 PM
A quite different approach..
I like hidden windows as they're a fast & "easy" way to dump & manipulate data (and one can use the powerfull /filter command as well)
...It looks a bit messy as I added comments to help you through the code smile
Should be notably faster as it uses $findfile only once; and it uses $findfile's command ability.

Note that the alias will rename files matching the "name pattern" as well if there's a "gap" in numbering, e.g if you have: "bla1.jpg bla2.jpg bla5.jpg" it will rename the bla5.jpg to bla3.jpg - Hope that's ok.

Code:
alias tobla {
  ; setup the directory (including final slash), file extension (including dot) and the name prefix
  var %dir = $mircdirsystem\images\backgrounds\, %ext = .jpg, %prefix = bla

  ; create hidden window to buffer all files in %dir matching the extension %ext
  window -h @found.files

  ; add all filenames in %dir having extension %ext to the window
  noop $findfile(%dir,$+(*,%ext),0,0,aline @found.files $nopath($1-))

  ; loop the lines (filenames) of the window. as long as a file is found that matches
  ; the "%prefix[NUMBER].%ext" pattern, remove it's line (as there's no need to rename these files)
  var %match.nr = 1
  while ($fline(@found.files,$+(%prefix,%match.nr,%ext))) {
    dline @found.files $v1
    inc %match.nr
  }

  ; loop the remaining lines (filenames) of the window; and rename the files
  ; (the "starting" number had been found in the loop above)
  var %nr = 1
  while ($line(@found.files,%nr)) {
   .rename $qt($+(%dir,$v1)) $qt($+(%dir,%prefix,%match.nr,%ext))
    inc %nr
    inc %match.nr
  }

  ; finally, close the hidden window
  window -c @found.files
}
Posted By: sparta Re: rename file - 18/10/07 07:00 PM
Thank you smile worked as a charm smile i will try to understand your code "thnx for the comments", then try to make my own and see if i can smile
Posted By: MeStinkBAD Re: rename file - 03/11/07 12:13 PM
Originally Posted By: sparta
I have a "background system" in my script, and i use a while loop to list all *.jpg files in a directory, this working just fine. Now to my problem, i need all images to be named as bla1.jpg - bla2.jpg and so on, any way to do this? and if i have lets say 3 *.jpg files in the directory, then they are named bla1.jpg - bla2.jpg - bla3.jpg , now i put in test.jpg, then it should be renamed to bla4.jpg, any way to do this?

rename <filename> <newfilename>

will rename the file, but i cant solve the thing with the image number, if bla3.jpg = free use it, or if it's taken take bla4.jpg instead..


hmmm

do

var %i = 0
:startrename
/rename <file> <newfile>%i<ext>
/return
:error
inc %i
/reseterror
goto startrename

© mIRC Discussion Forums