Jump to content

Change only extension on a file


Recommended Posts

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?

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 *

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
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.

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...

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")

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.

Link to post
Share on other sites
  • 14 years later...

@weaponx your code is great, although when I tried it in the most recent version of AutoIT it didn't work as expected, that's why I decided to edit the code a bit and correct some values to make it work... I hope you don't mind, I leave the code here :😁

;UDF File Extension Changer Edited by TesterMachine

;Comando: EXAMPLE
;var = @DesktopDir & "\test.exe"
;ChangeFileExtension($var, "bak")


;Function
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($sSourceFileFullPath, "((\.$sExt)?\.[a-z]+)$", "." & $sExt)

    EndIf

    ;ConsoleWrite($sNewSourceFileName)
    ;ConsoleWrite($sSourceFileFullPath & @TAB & $sNewSourceFileName)

    ;Rename file and return result
    Return FileMove($sSourceFileFullPath, $sNewSourceFileName, 0)
EndFunc
Link to post
Share on other sites

Extensions don't have to be ASCII alpha only and can be whatever you want to regard as "extension". In a (valid) filename like thename.abc.def.386.asm.gzip.bin what do you consider as extension?

Also the regex engine (PCRE) doesn't support variable interpolation: (\.$sExt) is taken literally.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
2 hours ago, jchd said:

In a (valid) filename like thename.abc.def.386.asm.gzip.bin what do you consider as extension?

In my mind, the extension is always what follows the last dot (when it exists), for example :

filename         : no extension
filename.zip     : extension is zip
filename.zip.txt : extension is txt

That last filename would be opened as a txt file thru Explorer when you double-click on it (also its icon is the text file icon)

@jchd did you mean something different with your question ?

Link to post
Share on other sites

I agree, so .tar.gz (and friends) isn't an extension. BTW it should be one of .tz, .taz, .tgz by now.

I never hit a .tar.tz file (.tz itself implies both tar and GNU zip).

When in the old days DOS/Windows filenames were all stored in 8+3 format, what was an extension was very clear. Now that dots are allowed in the file name part and multiple sections of "extension" are in use, things are much muddier. Extensions are both a metadata and part of a unique identifier.

When metadata in passed "in-band" with the rest, expect issues!

Another often forgotten point (irrelevant here): Windows pathnames can use either \ or /.

Anyway, I believe the following regex fits the bill, even if some input pathnames are invalid:

Local $a = [ _
    ".ext", _
    "..ext", _
    "...ext", _
    "....ext", _
    "try2.#_", _
    "try3.tgz", _
    "try4.tar.gz", _
    "try4.1/.tar.gz.tar.gz", _
    "try5.saved.tar.gz.386", _
    "try6:more", _
    "try7.txt:more", _
    "try8.txt:more.ads", _
    "try9:more.ads", _
    "C:\.../../try10.sav.tar.gz.386" _
]
Local $sExt = "bak", $sNewFileName

For $s In $a
    $sNewFileName = StringRegExpReplace($s, "(?i)(.*?)\.(tar\.gz|[^:.]+)$", "$1." & $sExt)
    ConsoleWrite(StringFormat("%30s    %s\n", $s, $sNewFileName))
Next

When a pathname refers to an ADS, you want to change the extension of the ADS.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...