Sign in to follow this  
Followers 0
Borje

Change only extension on a file

12 posts in this topic

Hello everybody

My question is what to do to rename only extension on a file.

Example: Testing.exe and I would like only change the exe

The filename before the exe is differnt every time but all files have extension exe and I would like to change only exe ( example to bat )

Is there anybody here can help me with a little example of a little script to do this?

Share this post


Link to post
Share on other sites



Is there anybody here can help me with a little example of a little script to do this?

http://www.autoitscript.com/forum/index.ph...c=40542&hl=

use sample #1


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Thank you /dev/null

I try this..

Share this post


Link to post
Share on other sites

Another option I put together:

;File without extension
$var = @DesktopDir & "\test"
ChangeFileExtension($var, "rar")

;Special case for tar.tz
$var = "test.tar.tz"
ChangeFileExtension($var, "rar")

;Straight rename
$var = @DesktopDir & "\test.zip"
ChangeFileExtension($var, "rar")


Func ChangeFileExtension($sSourceFileFullPath, $sExt)
    
    ;Define default destination for filenames without extensions
    Local $sNewSourceFileName = $sSourceFileFullPath & "." & $sExt
    
    If NOT FileExists($sSourceFileFullPath) Then Return SetError(1,0,1)
    
    ;Isolate filename from path
    $sSourceFileName = StringTrimLeft($sSourceFileFullPath, StringInStr ($sSourceFileFullPath, "\", 0, -1))
    
    ;If there is a period in the filename, reconstruct the path (special exception to allow tar.tz extension)
    If StringInStr($sSourceFileName, ".") Then
        $sNewSourceFileName = StringRegExpReplace($sSourceFileName, "((\.tar)?\.[a-z]+)$", "." & $sExt)
    EndIf
    
    ;ConsoleWrite($sNewSourceFileName & @CRLF)
    
    ;Rename file and return result
    Return FileMove($sSourceFileName, $sNewSourceFileName)
EndFunc

Share this post


Link to post
Share on other sites

Thanks Weaponx

I not really understand what I must do if we take a another example I want to change DiskTest-Setup.exe.txt to new name DiskTest-Setup.bat

What i must do then I am not so good in this scripting yet but try to understand from examples..

I want a little script that can take a filename that can change the ext exe.txt to only bat.

Share this post


Link to post
Share on other sites

You can do this a couple ways:

FileMove("test.txt", test.bat")

-or-

Using the function I posted:

$var = @DesktopDir & "\test.txt"

ChangeFileExtension($var, "bat")

Share this post


Link to post
Share on other sites

What I want is to sripped out exe.txt from DiskTest-Setup.exe.txt and then replace with bat The resulting file should be DiskTest-Setup.bat

I am not so good to explain but I hope this explain little bit more please if anybody can help me with this...

Share this post


Link to post
Share on other sites

What I want is to sripped out exe.txt from DiskTest-Setup.exe.txt and then replace with bat The resulting file should be DiskTest-Setup.bat

I am not so good to explain but I hope this explain little bit more please if anybody can help me with this...

What is the problem? Why don't you just rename the file? Why do you need a script to rename one file?

;Same as rename

FileMove("DiskTest-Setup.exe.txt", "DiskTest-Setup.bat")

Share this post


Link to post
Share on other sites

Hi

Yes I understand this examples but I want to know how do I stripped out this ( exe.txt) at the end of a file and replace the extension with bat from Example: DiskTest-Setup.exe.txt so the resulting file would be: DiskTest-Setup.bat

I already know how to use FileCopy and FileMove but I want to learn how I stripped out this exe.txt and replace with example bat from a file....

I think anybody here in the forum can give some example how to do this thanks.

Share this post


Link to post
Share on other sites

Dude. I created a function for you way back at post #4. It does exactly what you asked.

Share this post


Link to post
Share on other sites

Yes you have done and many thanks for that but I have that not to work perhaps I do some fault I think..

Share this post


Link to post
Share on other sites

Ok weaponx

I have it to work now it was my fault I have problem many thanks to you for your help I have to learn from your example thanks again..

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