Jump to content

Mass Renamer to upper or lowercase.


ezzetabi
 Share

Recommended Posts

Mhz said that this script deserved a place in Scripts and Scraps forum and so I posted it.

Actually I think there is nothing great. It is just a way to quickly rename all files and subfolders to uppercase or lowercase.

Func _Ren2UpLow($sFolder, $bType)
  ; $bType can be
  ; 0 and it means rename to uppercase
  ; 1 (or any other value) and it means rename to lowercase.
   
  ; returns 0 if everything is allright.
  ; returns -1 something goes wrong,
  ; and @error is set to
  ; 1 if no files where found
  ; 2 if $sFolder was not a folder.
   
   
   If Not StringInStr(FileGetAttrib($sFolder), 'd') Then
      SetError(2)
      Return -1
   EndIf
   Local $msg, $aFiles, $c
   
   If StringLeft($sFolder, 1) <> '\' Then $sFolder = $sFolder & '\'
   $aFiles = _FileSearch($sFolder, 1)
   
   If $aFiles[0] <> 0 Then
      $msg = ''
      If $bType = 0 Then
         For $c = 1 To $aFiles[0]
            $msg = $msg & 'ren "' & $aFiles[$c] & '" "' & StringUpper( StringTrimLeft($aFiles[$c], StringInStr($aFiles[$c], '\', 0, -1))) & '"' & @CRLF
         Next
      Else
         For $c = 1 To $aFiles[0]
            $msg = $msg & 'ren "' & $aFiles[$c] & '" "' & StringLower( StringTrimLeft($aFiles[$c], StringInStr($aFiles[$c], '\', 0, -1))) & '"' & @CRLF
         Next
      EndIf
      
      FileWrite(@TempDir & '\~execute.bat', $msg)
      
      RunWait(@ComSpec & ' /c "' & @TempDir & '\~execute.bat"', '', @SW_HIDE)
      FileDelete(@TempDir & '\~execute.bat')
      Return 0
   Else
      SetError(1)
      Return -1
   EndIf
EndFunc  ;==>_Ren2UpLow

Func _FileSearch($sIstr, $iSF)
   Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   While $sCS <> - 1
      $sCF = FileFindNextFile($sCS)
      If @error Then
         FileClose($sCS)
         ExitLoop
      EndIf
      If $sCF = '.' Or $sCF = '..' Then ContinueLoop
      If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
         If BitAND($iSF, 2) Then ExitLoop
      EndIf
   Wend
   
   If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                            for future search
               $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
               While $iH2 <> - 1
                  $sCF2 = FileFindNextFile($iH2)
                  If @error Then
                     FileClose($iH2)
                     ExitLoop
                  EndIf
                  If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                  If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then
                     $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     If BitAND($iSF, 2) Then
                        FileClose($iH2)
                        FileClose($iH)
                        ExitLoop 3
                     EndIf
                  EndIf
               Wend
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

If, anyone wants, I can add rename with the first letter upper or every word first letter upper. :)

Edited by ezzetabi
Link to comment
Share on other sites

You have changed the code alittle. So long as it is worth it. Hey, try to google for a solution to changing case and see what you get. Not much, best is what This-is-me offered http://stevemiller.net/apps/. But your effort, ezzetabi, is self contained script. Much worth Script and Scraps. Even if you do not realize. May not be an every day used script, but when you need it, just search Scripts and Scraps. :)

Link to comment
Share on other sites

Even so, the code would be far less complex and there would be no need to waste disk space on a temporary file (even if it is removed). If your application errors out, it leaves the chance for the batch file to be left on the computer.

Don't get me wrong, it is an interesting concept and I hope someone uses it. Thanks for at least giving back to the forum!

*** Matt @ MPCS

Link to comment
Share on other sites

Not because I think it is actually better,

But just for Matt that is worried for a remote chance of leaving a few kilobytes file in the @Tempdir folder, here the version with Matt's idea.

Also, this is true. This is 100% Autoit without asking anything to the Windows console.

Func _Ren2UpLow($sFolder, $bType)
  ; $bType can be
  ; 0 and it means rename to uppercase
  ; 1 (or any other value) and it means rename to lowercase.
  ; returns 0 if everything is allright.
  ; returns -1 something goes wrong,
  ; and @error is set to
  ; 1 if no files where found
  ; 2 if $sFolder was not a folder.
   
   If Not StringInStr(FileGetAttrib($sFolder), 'd') Then
      SetError(2)
      Return -1
   EndIf
   Local $aFiles, $sTmpname, $c, $sDest
   
   If StringLeft($sFolder, 1) <> '\' Then $sFolder = $sFolder & '\'
   $aFiles = _FileSearch($sFolder, 1)
   
   If $aFiles[0] <> 0 Then
      $c = 0
      Do;Looks for a tempname rolling a number.
         If Not FileExists($sFolder & $c) Then
            $sTmpname = $sFolder & $c
            ExitLoop
         EndIf
         $c = $c + 1
      Until 0
      
      For $c = 1 To $aFiles[0]
         If $bType = 0 Then
            $sDest = StringUpper($aFiles[$c])
         Else
            $sDest = StringLower($aFiles[$c])
         EndIf
         If StringInStr(FileGetAttrib($aFiles[$c]), 'd') Then
            DirMove($aFiles[$c], $sTmpname)
            DirMove($sTmpname, $sDest)
         Else
            FileMove($aFiles[$c], $sTmpname)
            FileMove($sTmpname, $sDest)
         EndIf
      Next
      Return 0
   Else
      SetError(1)
      Return -1
   EndIf
EndFunc  ;==>_Ren2UpLow

Func _FileSearch($sIstr, $iSF)
   Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   While $sCS <> - 1
      $sCF = FileFindNextFile($sCS)
      If @error Then
         FileClose($sCS)
         ExitLoop
      EndIf
      If $sCF = '.' Or $sCF = '..' Then ContinueLoop
      If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
         If BitAND($iSF, 2) Then ExitLoop
      EndIf
   Wend
   
   If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                            for future search
               $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
               While $iH2 <> - 1
                  $sCF2 = FileFindNextFile($iH2)
                  If @error Then
                     FileClose($iH2)
                     ExitLoop
                  EndIf
                  If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                  If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then
                     $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     If BitAND($iSF, 2) Then
                        FileClose($iH2)
                        FileClose($iH)
                        ExitLoop 3
                     EndIf
                  EndIf
               Wend
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch
Edited by ezzetabi
Link to comment
Share on other sites

Nice ezzetabi, :)

You do good.

@Matt

As to FileCopy. Why would I want to move 442 Mbs of files to a temp directory and back again, to simply change the case? This is for a program will be used often for this function. Filemove does not work with cd's? I do not understand how Filecopy can achieve this task better, if at all?

Link to comment
Share on other sites

MHz, FileMove is actually renaming if you are inside the same partition...

If it is an other partition then it is file copy + delete, but as you can see my script rename in a temp name inside the $sFolder set from the user, so it cannot be an other partition.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...