OS2World OLD-STATIC-BACKUP Forum

OS/2 - SIGs => Rexx => Topic started by: jep on 2008.04.28, 21:11:27

Title: Rename files that contain special characters
Post by: jep on 2008.04.28, 21:11:27
Marked as: Normal
Hello,

if you have files that contain characters such as "[" and "]" (square brackets) or + or multiple "." (dots) you'll soon see that you can't double click on the files to start the application you want. With "[...]" the OS will interpret it as you want to add a parameter before it opens the file, while more than one "." cause the file association to not work any longer.

The example provided below rename torrent files, but you can modify the code to support more file formats if you like.

//Jan-Erik

/* Rename torrent files, remove unwanted characters like [] and + */
'@echo off'

call RxLoadLib 'SysFileTree', 'SysLoadFuncs', 'RexxUtil'
call SysFileTree DIRECTORY()||'*.torrent', 'file', 'OF'
renamed = 0
do i = 1 to file.0
   fname = strip( translate( file.i, '', '.[]+' ) ) /* Remove characters. Adjust to your preference if needed */
   do while pos( '%', fname ) > 0 then
     parse value fname with pre'%'post
     fname = pre||substr( post, 3 )
   end
   do while pos( '  ', fname ) > 0 then
     parse value fname with pre'  'post
     fname = pre||' '||post
   end
   parse value translate( fname ) with start' TORRENT'
   fname = strip( substr( fname, 1, length( start ) ) )||'.torrent'
   parse value file.i with file.i'%'post
   if length( post ) > 0 then
   do
     do while pos( '%', post ) > 0 then
       parse value post with pre'%'post
       file.i = file.i||'%%'||pre
     end
     file.i = file.i||'%%'||post
   end
   if fname \= file.i then do
      Say 'Renaming "'||filespec( 'N', file.i )||'" to "'||filespec( 'N', fname )||'"'
      '@rename "'||file.i||'" "'||filespec( 'N', fname )||'"'
      if rc \= 0 then
        say 'failed!!!'
      else renamed = renamed + 1
   end /* do */
end /* do */
Say renamed 'of' file.0 'files has been renamed.'
Return 0

/* Load Libraries */
RxLoadLib: Procedure
  If RxFuncQuery( ARG(1) ) \= 0 Then do
    call RxFuncAdd ARG(2), ARG(3), ARG(2)
    interpret 'If '||ARG(2)||'() \= 0 Then Return 1'
  end
Return 0
Title: Re: Rename files that contain special characters
Post by: Saijin_Naib on 2008.04.29, 02:34:46
Haha, awesome Jep. I was experiencing this issue with some .torrent files I had, but I was not sure what made the parameter box show up. Is there a way to turn that off in the OS so that the [] are parsed as just part of the name?
Title: Re: Rename files that contain special characters
Post by: jep on 2008.04.29, 12:17:27
Hello Saijin_Naib,

Nope, not that I'm aware of, so I tend to use that script quite alot  ;D

It would be nice if they'd rewrite the procedure so that only program objects would trigger on [...].

//Jan-Erik