OS2 World.Com Forum
2012.05.26, 00:26:20 *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Rename files that contain special characters  (Read 1750 times)
jep
Global Moderator
Sr. Member
*****
Posts: 402


View Profile
« 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

Code:
/* 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
Logged
Saijin_Naib
Hero Member
*****
Posts: 1357


Birdie Num-Nums


View Profile WWW
« Reply #1 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?
Logged

jep
Global Moderator
Sr. Member
*****
Posts: 402


View Profile
« Reply #2 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  Grin

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

//Jan-Erik
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!