Borje Posted April 2, 2008 Posted April 2, 2008 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?
/dev/null Posted April 2, 2008 Posted April 2, 2008 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 *
weaponx Posted April 2, 2008 Posted April 2, 2008 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
Borje Posted April 2, 2008 Author Posted April 2, 2008 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.
weaponx Posted April 2, 2008 Posted April 2, 2008 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")
Borje Posted April 2, 2008 Author Posted April 2, 2008 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...
weaponx Posted April 2, 2008 Posted April 2, 2008 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.batI 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 renameFileMove("DiskTest-Setup.exe.txt", "DiskTest-Setup.bat")
Borje Posted April 3, 2008 Author Posted April 3, 2008 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.
weaponx Posted April 3, 2008 Posted April 3, 2008 Dude. I created a function for you way back at post #4. It does exactly what you asked.
Borje Posted April 3, 2008 Author Posted April 3, 2008 Yes you have done and many thanks for that but I have that not to work perhaps I do some fault I think..
Borje Posted April 3, 2008 Author Posted April 3, 2008 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..
TesterMachine Posted May 30, 2022 Posted May 30, 2022 @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
jchd Posted May 30, 2022 Posted May 30, 2022 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 hereRegExp tutorial: enough to get startedPCRE 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)
TesterMachine Posted May 30, 2022 Posted May 30, 2022 @jchd I understand the same, thanks for giving your knowledge on the subject, if you have a better way to change the extension of a file... I can't wait to show it now
pixelsearch Posted May 30, 2022 Posted May 30, 2022 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 ? "I think you are searching a bug where there is no bug... don't listen to bad advice."
jchd Posted May 30, 2022 Posted May 30, 2022 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 hereRegExp tutorial: enough to get startedPCRE 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now