mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Hey, I'm trying to create a script that moves my files from a dir to a subdir.

Basically, I have new files in H:\Anime popping up each day, and I want this script to automatically move them to H:\Anime\Filename, or create a dir if one doesn't exist.
However. When I tried doing this, I noticed that trying to create a dir called "Test 2" or trying to read if $isdir it would never use anything more than the first word.

Help would be appreciated!

Including script and reference picture with echo.
Code:
alias FolderSort { 
  var %main H:\Anime
  var %files $findfile(%main,*.mkv,0,0)
  while %files {
    var %file $findfile(%main,*.mkv,%files)
    var %regex /(\[HorribleSubs\] .+ -)/iS
    if ($regex(%file,%regex)) var %file $remove($regml(1),-,[HorribleSubs]) 
    echo -ag %file
    if (!$isdir(%main $+ \ $+ %file)) { 
      echo 3 -ag I had to create a folder for %file in %main $+ .
      mkdir %main $+ \ $+ %file
    }
    dec %files
  }
}
Originally Posted By: Echo
Tokyo Ghoul
I had to create a folder for Tokyo Ghoul in H:\Anime.
Love Stage!!
I had to create a folder for Love Stage!! in H:\Anime.

As you can see, it created the Tokyo folder when a Tokyo Ghoul folder was already created, and same with the other file. Moving them does not work either, it will not use 2 word filenames either.


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Joined: Aug 2013
Posts: 80
I
Babel fish
Offline
Babel fish
I
Joined: Aug 2013
Posts: 80
When dealing with file/directory manipulation in a command, you should always surround the path in double quotes - either with the $qt() identifier, or with literal " " characters (and $+ where necessary). This ensures that the entire path is used in the command, even if it contains spaces. This is not necessary within identifiers since parameters are comma rather than space separated.

Example:
Code:
mkdir $qt(%main $+ \ $+ %file)

Joined: Jul 2006
Posts: 4,141
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,141
Btw, you're using $findfile somewhat incorrectly, $findfile itself is a loop, using it inside your while loop like this makes things much much slower, you could use something like this, which only calls $findfile once ( while your code call $findfile once to get the number of files, and then once each time the loop iterates):

Code:
alias FolderSort {
  var %main H:\Anime\
  noop $findfile(%main,*.mkv,0,0, noop $handle_anime(%main,$nopath($1-)))
}
alias handle_anime {
  var %pattern /(\[HorribleSubs\] .+ -)/iS
  if ($regex($2-,%pattern)) {
    var %f $remove($regml(1),-,[HorribleSubs])
    if (!$isdir($1- $+ %f)) mkdir $qt($1- $+ %f) 
    ; rename $qt($1- $+ $2-) $qt($1- $+ %f $+ \ $+ $2-)
  }
}



#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Thanks Wims! I never even thought of the fact that I could use an alias on the identifier when I read it in the help files. I guess it's my lack of experience smile

I noticed something about it though, and this is from my original code:

echo -ag %f
echo -ag test $+ %f
Would return
Tokyo Ghoul
test Tokyo Ghoul

So I replaced spaces with _ to see if I had a space in the beginning:
_Tokyo_Ghoul
test_Tokyo_Ghoul

Took me a while to figure out why I was getting a path with a space in the beginning, but as soon as I did it was an easy fix smile
I believe it was cause the files look like [HorribleSubs] Anime name, and I removed the [HorribleSubs], leaving a space behind.
Code:
alias FolderSort {
  var %main H:\Anime\
  noop $findfile(%main,*.mkv,0,0, noop $handle_anime(%main,$nopath($1-)))
}

alias handle_anime {
  var %pattern /(\[HorribleSubs\] .+ -)/iS
  if ($regex($2-,%pattern)) {  
    var %f $remove($regml(1),-,[HorribleSubs] )
    var %f $mid(%f,2-)
    if (!$isdir($1 $+ %f)) { 
      echo 4 -ag I had to make a new folder for %f $+ .
      mkdir $qt($1 $+ %f) 
    }
    else { 
      rename $qt($1 $+ $2-) $qt($1 $+ %f $+ \ $+ $2-)
      echo 3 -ag Moved %f without a problem.
    }
  }
}


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Joined: Jul 2006
Posts: 4,141
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,141
Yeah, and because you were using /var, which is actually one of the unique command preserving spaces (only a single trailing space is not retained with /var)


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Ah, thanks. Good to know.
I'll keep that in mind for the future. smile


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net

Link Copied to Clipboard