Sign in to follow this  
Followers 0
Gyzmok

File Rename - Uppercase To Lowercase

14 posts in this topic

I would like to change some file names from uppercase to lowercase

e.g. C:\test\TEST1.TXT should become C:\test\test1.txt

Autoit does not have a built-in 'File_rename' command.

I tried it with the 'Filemove' command :

$var = FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

But this not only gives me an error, but it also DELETES the source file ! :think:

Is this the intended behaviour of the Filemove command ?

so I tried it in this way :

Func _setnewname($filename)
   $conv_name = StringLower($filename)&"°"
   $var = FileMove ($filename, $conv_name ,0 )
   if $var = 0 Then MsgBox(4096,"Error", "Problem setting Lowercase for "&$filename)
   $var = FileMove ($conv_name, StringTrimRight($conv_name,1) ,1 )
   If $var = 0 Then MsgBox(4096,"Error", "Problem setting filename for "&$filename)
EndFunc

This works, but maybe there is a better way than this ?

By coincidence I tried this one :

FileSetAttrib("C:\test\TEST1.TXT","+R")
$var = FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

and it works ! (apart from the read-only status after the change)

but is this also an intended behaviour ?

any suggestions ?


D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper

Share this post


Link to post
Share on other sites



FileSetAttrib("C:\test\TEST1.TXT","+R")
$var = FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")
FileSetAttrib("C:\test\test1.txt","-R")

#)

Share this post


Link to post
Share on other sites

@nfwu : that was very helpfull :think:

I meant :

Is this behaviour not accidently and won't it be "fixed" in the future. :(

Can I use it "safely" or is there a far better solution ...


D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper

Share this post


Link to post
Share on other sites

Even doing the same with the Explorer do the same. When I want to do it I rename something else and after with the case I want.

I think Windows does not do anything when you just change the case. :think:

Share this post


Link to post
Share on other sites

I would like to change some file names from uppercase to lowercase

e.g. C:\test\TEST1.TXT should become C:\test\test1.txt

Autoit does not have a built-in 'File_rename' command.

I tried it with the 'Filemove' command :

$var = FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

But this not only gives me an error, but it also DELETES the source file ! :think:

Is this the intended behaviour of the Filemove command ?

so I tried it in this way :

Func _setnewname($filename)
   $conv_name = StringLower($filename)&"°"
   $var = FileMove ($filename, $conv_name ,0 )
   if $var = 0 Then MsgBox(4096,"Error", "Problem setting Lowercase for "&$filename)
   $var = FileMove ($conv_name, StringTrimRight($conv_name,1) ,1 )
   If $var = 0 Then MsgBox(4096,"Error", "Problem setting filename for "&$filename)
EndFunc

This works, but maybe there is a better way than this ?

By coincidence I tried this one :

FileSetAttrib("C:\test\TEST1.TXT","+R")
$var = FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

and it works ! (apart from the read-only status after the change)

but is this also an intended behaviour ?

any suggestions ?

The following uses the REN command in a shell, and tested well on Win2KPro SP4. The function returns a 1 for success and 0 for failure. You may or may not want to move the two lines that generate the destination file name to inside the _SetNewName() function:

; Test function to change filename to lower case
$SrcName = "C:\test\TEST1.TXT"

; Strip to only file name because REN command does not accept path on destination
$SplitName = StringSplit($SrcName, "\")
$DestName = StringLower($SplitName[$SplitName[0]])

If _SetNewName($SrcName, $DestName) Then
    MsgBox(64, '_SetNewName() Test', 'Success changing "' & $SrcName & '" to "' & $DestName & '"')
Else
    MsgBox(16, '_SetNewName() Test', 'Failed changing "' & $SrcName & '" to "' & $DestName & '"')
EndIf


Func _SetNewName($SrcFile, $DestFile)
    $DosCmd = 'REN "' & $SrcFile & '" "' & $DestFile & '"'
    $ErrLvl = RunWait(@ComSpec & ' /c ' & $DosCmd, @WorkingDir, @SW_SHOW)
    If $ErrLvl <> 0 Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc  ;==>_SetNewName

Hope that helps... :(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Calling @Comspec for a directory of files is too slow. Been there before, and it can take up to 5 mins to convert 500 files. Using a batch file can do it in about 5 seconds as it uses 1 instance of @Comspec.

This is done with only AutoIt and should take just seconds to do a whole dir of files. Or one file with the small UDF only.

; Change a single file
_FileCase('C:\folder\test.txt', 1)

; Change a directory of files
_FileCaseInDir('C:\folder', 1)

Func _FileCaseInDir($path, $case = 1)
    Local $workingdir_old = @WorkingDir
    Local $handle_search, $file
    If FileChangeDir($path) Then
        $handle_search = FileFindFirstFile("*") 
        If $handle_search <> -1 Then
            While 1
                $file = FileFindNextFile($handle_search) 
                If @error Then ExitLoop
                If $file = '.' Or $file = '..' Then ContinueLoop
                _FileCase($file, $case)
            WEnd
            FileClose($handle_search)
        Else
            MsgBox(0x30, "Error", "No files/directories matched the search pattern")
            Exit
        EndIf
        FileChangeDir($workingdir_old)
    EndIf
EndFunc

Func _FileCase($path, $case = 1)
    Local $temp = $path & '~TMP'
    FileMove($path, $temp)
    If $case Then
        FileMove($temp, StringUpper($path))
    Else
        FileMove($temp, StringLower($path))
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Even doing the same with the Explorer do the same. When I want to do it I rename something else and after with the case I want.

I think Windows does not do anything when you just change the case. :think:

jpm, I just tried with XP Pro sp2 and was able to change the case with Explorer just fine. But I'm pretty sure Windows 2000 and earlier behave the way you describe.

Also, I'm concerned that FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1) causes the file to be DELETED! The behavior makes sense, but it might be good to add a warning in the help file docs.


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

jpm, I just tried with XP Pro sp2 and was able to change the case with Explorer just fine. But I'm pretty sure Windows 2000 and earlier behave the way you describe.

Also, I'm concerned that FileMove("C:\test\TEST1.TXT","C:\test\test1.txt",1) causes the file to be DELETED! The behavior makes sense, but it might be good to add a warning in the help file docs.

I agree with your diagnostic, you know when you are getting older you just remember your young stuff.

I will have a look to see if the filemove can be corrected without the deletion :think:

Share this post


Link to post
Share on other sites

Thanks everyone for the responses.

@mhz and @PSaltyDS both your code works :think:

@jpm and @CyberSlug :

Thanks for your concerns about the file deletion.

Just for the info :

Filecopy also DELETES the file in this code :

$var = FileCopy("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks everyone for the responses.

@mhz and @PSaltyDS both your code works :think:

@jpm and @CyberSlug :

Thanks for your concerns about the file deletion.

Just for the info :

Filecopy also DELETES the file in this code :

$var = FileCopy("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")

of course it does... its actually the same name...

try that code with a different destination folder

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Just for the info :

Filecopy also DELETES the file in this code :

$var = FileCopy("C:\test\TEST1.TXT","C:\test\test1.txt",1)
if not $var then MsgBox(0,"","error")
I would consider it wise to fail the operation if it were to do a destructive action as deletion.

Within DOS, Move does rename the file case. Copy fails with "The file cannot be copied onto itself"

Share this post


Link to post
Share on other sites

@MHZ : you are right.

@Valuator :

of course it does... its actually the same name...

Yes... but this is also the same name and it

does NOT delete the file :

$var = FileCopy("C:\test\TEST1.TXT","C:\test\TEST1.TXT",1)
if not $var then MsgBox(0,"","error")

D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper

Share this post


Link to post
Share on other sites

#13 ·  Posted

I have a quick question regarding this thread... I am somewhat familier with autoit, but i am not quite sure i understand what to change in the coding listed here to make one of these scripts do what i want it to do. What i want it to do is search a directory for a list of files beginning with "shortcut to" and rename the all or I should say remove the "shortcut to" portion. Is there a way to do that? I appreciate your help.

PS: all the files are different names, i just want the "shortcut to" portion gone.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

I have a quick question regarding this thread... I am somewhat familier with autoit, but i am not quite sure i understand what to change in the coding listed here to make one of these scripts do what i want it to do. What i want it to do is search a directory for a list of files beginning with "shortcut to" and rename the all or I should say remove the "shortcut to" portion. Is there a way to do that? I appreciate your help.

PS: all the files are different names, i just want the "shortcut to" portion gone.

The problem you mention does not relate to the topic that started this thread. Please post a new topic next time.

A small change to my UDF above can do as you are asking. Since you say that you have some AutoIt knowledge, then you can add SetError() or MsgBoxes to suit your needs.

; 1st parameter = Path to the folder
; 2nd parameter = Text to remove from the left of the filename

_FilenameTrimLeft('C:\Folder', 'shortcut to')

Func _FilenameTrimLeft($path, $clip)
    Local $workingdir_old = @WorkingDir
    Local $handle_search, $file_found, $file_new
    Local $length = StringLen($clip)
    If Not $length Then Return
    If FileExists($path) And Not StringInStr(FileGetAttrib($path), 'D') Then
        Return
    ElseIf Not FileExists($path) Then
        Return
    EndIf
    If FileChangeDir($path) Then
        $handle_search = FileFindFirstFile("*") 
        If $handle_search <> -1 Then
            While 1
                $file_found = FileFindNextFile($handle_search) 
                If @error Then ExitLoop
                If $file_found = '.' Or $file_found = '..' Then ContinueLoop
                If StringLeft($file_found, $length) = $clip Then
                    $file_new = StringStripWS(StringTrimLeft($file_found, $length), 1)
                    FileMove($file_found, $file_new)
                EndIf
            WEnd
            FileClose($handle_search)
        Else
            MsgBox(0x30, "Error", "No files/directories matched the search pattern")
            Exit
        EndIf
        FileChangeDir($workingdir_old)
    EndIf
EndFunc

:)

Edit:

Added StringStripWS() to remove any leading whitespace from new filename.

Edited by MHz

Share this post


Link to post
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
Sign in to follow this  
Followers 0