Sign in to follow this  
Followers 0
Cdma1X

is it available? FileGetOnlyName()

29 posts in this topic

#1 ·  Posted (edited)

hi, how can i get only the file name from a path?

for example how can i get only the file names without fullpath from the following variables?

$CmdLine[0]

$CmdLine[1]

$CmdLine[2]

; and so on...

or

$A1 = "C:\New Folder\Images\Old\1.bmp"
$B2 = "D:\Songs\Audio\PoP\Loveme.Mp3"
$C3 = "E:\Video\Songs\YouTube\SimSim.Flv"
$D4 = "F:\Softwares\AutoItScripts\Compiled\MyProgram.exe"
; And so on...
MsgBox(64, "File Names", $A1 & @CRLF & $B2 & @CRLF & $C3 & @CRLF & $D4)

the above code will show a message box with the paths and there names like

F:\Softwares\AutoItScripts\Compiled\MyProgram.exe

but i want to show only the file name in message box like

MyProgram.exe

how can i extract the only name from path? i have many other paths to add so any easy, fast and good example?

thankss.

Edited by Cdma1X

Share this post


Link to post
Share on other sites



Hi Cdma1X,

1st Welcome to the AutoIt Forums! :idea:

Some of the following tips may not apply to you, but it may make your life a bit easier here on the forum in the future.

CODE
  • Did you know that we have an awesome search feature?

    You can find many answers to your current questions, just by typing in the right search patterns.

  • A suggestion is to use the Advanced Search mode:

  • Type your specific search term in quotes.
  • Click the forum you want to search in (the one most likely to have your information would generally be the Example Script forum and or the General Help and Support Forum).
  • Click on "Search titles only" radio button.
  • Click perform search.
The above will help you narrow down your searches and prevent you from unneccesarily posting a new thread.

[*]Also, you should try to read the Sticky posts that are at the top of each of the AutoIt Forums you enter such as:

[*]Keep in mind, the help file will be your best friend, however you may find some of the tutorials written by some of our elite forum members helpful.

[*]Forum Etiquette:

  • Making a new thread:

    • Use the Search feature first to see if your question has already been answered.
    • Look in the help file as well before even thinking of posting (When what you want could be obtained by simply reading the help file, you don't generally get a good response from your AutoIt community).
    • Titles are very important here. 1 word titles or titles like "help me", "write something for me", "I'm a noob" etc... aren't tolerated.
    • Make sure you are posting in the correct forum:

      • General Help and Support:

        • This forum is for AutoIt related support questions only. If you have a question related to another language, or nothing at all to do with AutoIt then you need to post in the chat forum, or in that languages perspective forum.
      • Example Script:

        • This forum is for AutoIt scripts/executables only.
        • Source code is preferred but not necessary, you do have the right to just post the binary of your project if you wish.
        • Please don't post questions in this forum unless it's directly related to a thread already existing.
    • Use common sense when creating a new thread.

      Ask yourself if the title is descriptive enough to even interest someone (preferably those that know what they are talking about) to even look at your thread, let alone reply in it.

    • Think about how it would show in the search feature if someone were to look for something just like you are looking for (think of the keywords you used yourself and obviously didn't find anything (because we know you used the search feature :P ) and use those types of keywords in your title as well).
  • Thread content:

    • Be descriptive with your query. (Make sure we actually know what you want to do).
    • Show you've made an effort in coding what you want (provide the reproducer code (generally no more than 50 lines as people lose interest in debugging someones script for free)).
    • Don't talk in ebonics. A lot of the forum members are adults, and a lot of them know how to help you, but talk like a child, you'll be treated as such.
    • Don't ask for help making keyloggers, spam (even if it's to do as a prank), or anything that can be thought of as malicious. You'll more than likely have the thread locked by a moderator, and take a bashing from your fellow AutoIt community.
    • When posting code, use code boxes. This can be accomplished by using [code ]<content here>[/code ] (No spaces between the brackets []).

      Using code boxes will keep the indentation and make it easier to read for others to help you.

  • Bumping your threads:

    • Use common courtesy here.

      Keep in mind every time you bump your thread to the top of the forum, you knock the other threads down a notch.

      Everyone posting for help has just as much right for their threads to get read as you do.

      Because of that, do not bump your post more than once in a 24 hour period.

      A Bump is simply posting in your thread with nothing that pertains to your query with the sole purpose of moving it up.

      Deleting previous bumps, and posting new ones is not tolerated, and the moderators can find those deletions, so do yourself a favor and don't cross that line :( .

  • Rude or obnoxious content:

    This falls pretty much under the common sense thing. If you use it (common sense) before posting, you won't have issues.

    • Don't use foul language, remember, a lot of the community is at work when they read these threads.
    • Don't provoke or instigate an argument with someone.
  • Double Posting:

    • It's understood that sometimes there's a lag in the system, and sometimes people don't see their post go up right aways so they post again.

      If this happens to you, simply notify a moderator with the report feature in the post, and politely ask them to delete it.

    • If you're just creating another topic because your original topic is not being answered the way you want or at all, this is not tolerated. You could lose your posting privileges all together over it.
  • Non-English languages

    • If English is not your primary language, please make an attempt to interpret (yourself or online) and post that interpretation.

      We have wonderful users from around the world, so after you've done your post in English, back it up with your question also in your native tongue (You may find your answer much quicker using both).

That's it for now, I hope you have a wonderful learning experience, and hope to see you contribute to the community as your knowledge grows.

Share this post


Link to post
Share on other sites

Help File + _PathSplit()


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Help File + _PathSplit()

thanks for the quick answer. any example in showing it in messagebox? i could not understand ArrayDisplay().

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath,"Demo _PathSplit()")oÝ÷ Ú+kçZºÚ"µÍÙÐÞ
    ][ÝÑ[H[YÉ][ÝË  ÌÍÕÝ]   [ÈÔ   [È ÌÍÑ[[ÈÔ]Ü]

J

thankss.

Share this post


Link to post
Share on other sites

$A1 = "C:\New Folder\Images\Old\1.bmp"
$B2 = "D:\Songs\Audio\PoP\Loveme.Mp3"
$C3 = "E:\Video\Songs\YouTube\SimSim.Flv"
$D4 = "F:\Softwares\AutoItScripts\Compiled\MyProgram.exe"
; And so on...

$A1 = StringRegExpReplace($A1, "^.*\\", "")
$B2 = StringRegExpReplace($B2, "^.*\\", "")
$C3 = StringRegExpReplace($C3, "^.*\\", "")
$D4 = StringRegExpReplace($D4, "^.*\\", "")

MsgBox(64, "File Names", $A1 & @CRLF & $B2 & @CRLF & $C3 & @CRLF & $D4)


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

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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 UDF
 
AutoIt_Icon_small.pngExamples: 
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 Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

thanks MrCreatoR it is good. but i want to know/learn if there are any other ways to do it in future.

thanks once again.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

thanks MrCreatoR it is good. but i want to know/learn if there are any other ways to do it in future.

thanks once again.

#Include <File.au3>

Dim $szDrive, $szDir, $szFName, $szExt
$string = ""

$array = _FileListToArray(@ScriptDir, "*", 1)

For $i = 1 to $array[0]
    $TestPath = _PathSplit($array[$i], $szDrive, $szDir, $szFName, $szExt)
    $string &= $TestPath[3] & $TestPath[4] & @CRLF
Next

MsgBox(0,"test",$string)
Edited by SoulA

Share this post


Link to post
Share on other sites

thankss SoulA is it the last way or there are any more funcs/commands available?

my problem is solved by the script of MrCreatoR but i want to know more just for learning purposes.

thankss once again for the information.

Share this post


Link to post
Share on other sites

someone told me about a FileGetOnlyName() UDF. i searched but no results found.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

The code I wrote as the quickest and easiest way I can think of to get the just the file names of all the files in a given directory.

However, like everything there is always many ways to do something.

It is likely that the UDF you speak of if it exists uses one of the already mentioned options in this thread just in a UDF format.

Edited by SoulA

Share this post


Link to post
Share on other sites

thankss SoulA if this UDF is available then can i have it's link?

as i told before i searched for the UDF but no results found/match the same.

Share this post


Link to post
Share on other sites

thankss SoulA if this UDF is available then can i have it's link?

as i told before i searched for the UDF but no results found/match the same.

MrCreator presented the fastest way. I merely suggested the help file way.

MrCreator is using Regular Expressions to move through the string of the file name.

StringRegExpReplace($D4, "^.*\\", "")

^ lets the regular expression engine know to start at the beginning of the string.

.* tells the engine to go through every possible character until it reaches it's stopping point which is back slash

\\ Is an escaped character, there is only one back slash but because a backslash has more than 1 meaning in Regular expression you have to escape it, just so happens, the escape character is also a backslash.

So what the ^.*\\ is doing in StringRegExpReplace is replacing every character between the start of the string, and the last backslash... thus leaving the file name + extension.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thanks for the detailed information which i could not get in help file and could not understand.

i have many variables to get path. any way to handle all of them with some short lines?

or any UDF which can get the file path without always writing the all lines?

Share this post


Link to post
Share on other sites

Thanks for the detailed information which i could not get in help file and could not understand.

i have many variables to get path. any way to handle all of them with some short lines?

or any UDF which can get the file path without always writing the all lines?

Depends on your naming conventions.

I prefer to use arrays... And MrCreators suggested method. This would only require 3 or 4 lines of code total.

You could look at Assign and Eval to do a For/Next loop to accomplish something similar with the same method as an array.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thanks Smoke_N

i have just accomplished what i want but only i have a last problem.

on running this

StringRegExpReplace($File_Group_A6, "^.*\\", "")

i am getting a " at the end of file. like if my file name is RunTest.au3 then i will get

RunTest.au3"

can you help me to correct it?

Share this post


Link to post
Share on other sites

Thanks Smoke_N

i have just accomplished what i want but only i have a last problem.

on running this

StringRegExpReplace($File_Group_A6, "^.*\\", "")

i am getting a " at the end of file. like if my file name is RunTest.au3 then i will get

RunTest.au3"

can you help me to correct it?

Share this post


Link to post
Share on other sites

Thanks Smoke_N

i have just accomplished what i want but only i have a last problem.

on running this

StringRegExpReplace($File_Group_A6, "^.*\\", "")

As you can see here, it works fine.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thanks Smoke_N

please check this:

; $CmdLineRaw = Default
MsgBox(0, 0, StringRegExpReplace($CmdLineRaw, "^.*\\", ""))

it will show " at the end of file name. like if my file name is

RunTest.au3 then i will get

RunTest.au3"

is it a possible autoit bug?

Share this post


Link to post
Share on other sites

Thanks Smoke_N

please check this:

; $CmdLineRaw = Default
MsgBox(0, 0, StringRegExpReplace($CmdLineRaw, "^.*\\", ""))oÝ÷ Ú+pYl*ºZ¶Ø^zwh}ø¥zv¦zX¤z'æÉø¥zv¦z+ºtÞ²Ö®ÞØ^,"Xµ§Më-jíêºb²+Z¦,¹^jëhÖîú®¢×êÞßÛ'¡ê뢸­ßÛ,jeÉÚ"!jØ­ßÛ-¢Wm¡ÚÆÚÚrׯz¼­)à~º&çí¡ûazV¬µ¶ÉZ²®¶­sc²b33c´6ÖDÆæU&rÒFVfVÇ@¤×6t&÷Âb33c´6ÖDÆæU&rÂ7G&æu&VtW&WÆ6Rb33c´6ÖDÆæU&rÂgV÷Cµââ¢b3#²b3#²gV÷C²ÂgV÷C²gV÷C²oÝ÷ غ(«mëb¶W¢·¥ÈhÃ*.¶«z+ªê-zØ^­æ­¶Ú¶¬¶¸§*2¢èZ½ëh©iØî²ÙÛ¢¸­ZgËhºw^®ËZܨ»e¯zÚ,¶çrEè.ªÄÆ޲ȨƬ¶¢»¦Â)eº0¶*ÞvÚÌ®)àªç¬¶*'²X¤zØb±«­¢+ØìÀÌØí
µ1¥¹IÜôÕ±Ð)5Í  ½à À°ÀÌØí
µ1¥¹IÜ°MÑÉ¥¹IáÁIÁ± ÀÌØí
µ1¥¹IÜ°ÅÕ½Ð츨üÀäÈìÀäÈíðÅÕ½ÐìÅÕ½ÐìÅÕ½Ðì°ÅÕ½ÐìÅÕ½Ð줤
Would be a revision you could make to suit your needs.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

$explorer = "C:\WINDOWS\explorer.exe"
ConsoleWrite($explorer&@CRLF&"FileName:  "&getFileName($explorer)&@CRLF&"ParentDir: "&getParentDir($explorer)&@CRLF&"Extension: "&getExtension($explorer)&@CRLF)

Func getFileName($fpath)
    Return StringMid($fpath,StringInStr($fpath,"\",0,-1)+1)
EndFunc

Func getParentDir($fpath)
    Return StringMid($fpath,1,StringInStr($fpath,"\",0,-1))
EndFunc

Func getExtension($fpath)
    Return StringMid($fpath,StringInStr($fpath,".",0,-1)+1)
EndFunc

another way :P

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