Jump to content



Photo

_FileGetValidName() UDF


  • Please log in to reply
4 replies to this topic

#1 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,241 posts

Posted 25 June 2007 - 02:07 AM

Hi all!

Until today, i was using one quite slow function to replace the unsupported characters in file name (* ? \ / | : < > "), and not just replace them, but also to insure that the replaced chars will not been repeated...

This is the Old function (uses loop to strip repeating chars):

Func ReplaceUnsupported($String, $Patern='[*?\\/|:<>"]', $Replace="_")     If StringLen($String) = 0 Then Return $String     $String = StringRegExpReplace($String, $Patern, $Replace)     While StringInStr($String, $Replace & $Replace) <> 0         $String = StringReplace($String, $Replace & $Replace, $Replace)     WEnd     Return $String EndFunc



And this one is New function + usage example (it's uses a genius RegExp engine :rambo:):

#include <Date.au3> $sFileName = _Now() & "_File.txt" $sFileName = _FileGetValidName($sFileName, '[*?\\/|:<>"]', '_') ConsoleWrite("Replacements: " & @extended & @CRLF & "Result: " & $sFileName) ;Will output something like this: ;  Replacements: 2 ;  Result: 19.01.2009 4_15_59_File.txt Func _FileGetValidName($sString, $sPatern='[*?\\/|:<>"]', $sReplace='_')     If StringStripWS($sString, 8) = '' Then Return $sString         $sString = StringRegExpReplace($sString, $sPatern, $sReplace)     Return SetExtended(@extended, StringRegExpReplace($sString, '(' & $sReplace & '+)', $sReplace)) EndFunc


I hope that someone will find it usefull :rolleyes:

Edited by MrCreatoR, 12 June 2009 - 10:04 AM.

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: ATT - Application Translate Tool [new] | BlockIt - Block files & folders [new] | SIP - Selected Image Preview [new] | SISCABMAN - SciTE Abbreviations Manager [new] | AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramPosted Image UDFs: OnAutoItErrorRegister - Handle AutoIt critical errors [new] | AutoIt Syntax Highlight [new] | Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDFPosted Image Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team








#2 MCP

MCP

    Adventurer

  • Active Members
  • PipPip
  • 107 posts

Posted 12 October 2007 - 10:22 AM

I hope that someone will find it usefull :)


Indeed, thx a lot

#3 goldenix

goldenix

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 735 posts

Posted 04 June 2009 - 11:25 AM

very nice, but why do you need this line?

#include <Date.au3> $sFileName = _Now() & "_File.txt"


Thanx for UDF.

#4 KaFu

KaFu

    Hey, it's just me, KhaFoo...

  • MVPs
  • 3,167 posts

Posted 04 June 2009 - 11:46 AM

Only to create a filename containing invalid characters (":") for the example.

Edited by KaFu, 04 June 2009 - 11:46 AM.


#5 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,241 posts

Posted 12 June 2009 - 10:05 AM

Oops, forgot to use $sFileName instead of $sString in the function call :D
Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: ATT - Application Translate Tool [new] | BlockIt - Block files & folders [new] | SIP - Selected Image Preview [new] | SISCABMAN - SciTE Abbreviations Manager [new] | AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramPosted Image UDFs: OnAutoItErrorRegister - Handle AutoIt critical errors [new] | AutoIt Syntax Highlight [new] | Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDFPosted Image Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users