Sign in to follow this  
Followers 0
Skizmata

Getting a File Name From a String

12 posts in this topic

#1 ·  Posted (edited)

I'm creating a program for my work to make it easier to lookup some information from our network.

I have a file with over 2,000 file names I allready have the portion of the script that searches the network drive and gets the data.

An example entry - "N:\Specs Sheets\Aus Filter\Air\AF11420.xls"

I need to search this and match a string I get from my GUI with the file name and not the rest of the path. This is where my problem is. I'm using _ArraySearch and sometimes the results I get are from the other parts of the path not just the filename. _ArraySearch does not seem to have any parameters to allow me to only search part of each array entry.

After I match the file name I can feed the whole path string into my function to print the xls file. That is no problem.

Im using randallc's _FileListToArrayNew2g.au3 you can find it here I'm not sure how much help it will be but below is what I have so far. I am aware that _PrintSpec is empty that is because what I have there now dosn't work and is somewhat irrelevant to the problem.

#Include <File.au3>
#Include <Array.au3>
#include <GUIConstants.au3>
#include <_FileListToArrayNew2g.au3>

AutoItSetOption("RunErrorsFatal", 1)

Dim $FileList[1]


If Not _LoadSpecsFromHD() Then _LoadSpecFromNetwork()

$SpecPrintGUI = GUICreate("Spec Print - 2007", 271, 342, -1, -1)
$StatusLable = GUICtrlCreateLabel("Enter a list of part numbers below each on its own line.", 10, 10, 400)
$EditSpecList = GUICtrlCreateEdit("", 12, 46, 249, 241, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN))
GUICtrlSetData(-1, "")
$ButtonPrintSpec = GUICtrlCreateButton("&Print Specs", 74, 292, 115, 41, 0)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonPrintSpec
            $aPartNumbersToPullAndPrint = StringSplit(GUICtrlRead($EditSpecList), @CR)
            
            $i = 0
            For $element In $aPartNumbersToPullAndPrint
                If $i > 1 Then $aPartNumbersToPullAndPrint[$i] = StringTrimLeft($element, 1)
                $i = $i + 1
            Next
            
            $i = 1
            While $i < $aPartNumbersToPullAndPrint[0] + 1
                _PrintSpec ($aPartNumbersToPullAndPrint[$i])
                MsgBox(0, "Continue...", "Press Ok when spec for " & $aPartNumbersToPullAndPrint[$i] & " has finished printing.")
                $i = $i + 1
            WEnd
            ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
    EndSwitch
WEnd

Func _LoadSpecsFromHD()
    If FileExists(@ScriptDir & "\SpecArray.txt") Then
        ; Loads File Into Array
        FileChangeDir(@ScriptDir)
        FileOpen(@ScriptDir & "\SpecArray.txt", 0)
        _FileReadToArray(@ScriptDir & "\SpecArray.txt", $FileList)
        FileClose(@ScriptDir & "\SpecArray.txt")
    Else
        Return False
    EndIf
    Return True
EndFunc   ;==>_LoadSpecsFromHD

Func _LoadSpecFromNetwork()
    ToolTip("Reading Spec List This May Take Several Minutes...", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
    $FileList = _FileListToArray3 ("N:\", "*.XLS", 1, 1, "", "")

    If @error = 1 Then
        ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
        MsgBox(16, "Error", "Failed To Establish Link on N:\")
        Exit
    EndIf

    If @error = 4 Then
        ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
        MsgBox(16, "Error", "No .XLS Files Found On N:\")
        Exit
    EndIf

    FileChangeDir(@ScriptDir)
    FileDelete("SpecArray.txt")
    FileOpen("SpecArray.txt", 1)
    For $x = 0 To $FileList[0]
        FileWrite("SpecArray.txt", $FileList[$x])
        If $x <> $FileList[0] Then FileWrite("SpecArray.txt", @CRLF)
    Next
    FileClose("SpecArray.txt")

    ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
EndFunc   ;==>_LoadSpecFromNetwork

Func _PrintSpec ($SpecToFind)

EndFunc   ;==>_PrintSpec

Thank you very much!

Edited by Skizmata

AutoIt changed my life.

Share this post


Link to post
Share on other sites



$string = "N:\Specs Sheets\Aus Filter\Air\AF11420.xls"

MsgBox(0,"",_GetFileName($string))

Func _GetFileName($path)
    Local $array = StringSplit($path,"\",1)
    Return $array[$array[0]]
EndFunc

Im assuming that you want to return AF11420.xls part of the path?


www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Share this post


Link to post
Share on other sites

I'm creating a program for my work to make it easier to lookup some information from our network.

I have a file with over 2,000 file names I allready have the portion of the script that searches the network drive and gets the data.

An example entry - "N:\Specs Sheets\Aus Filter\Air\AF11420.xls"

I need to search this and match a string I get from my GUI with the file name and not the rest of the path. This is where my problem is. I'm using _ArraySearch and sometimes the results I get are from the other parts of the path not just the filename. _ArraySearch does not seem to have any parameters to allow me to only search part of each array entry.

After I match the file name I can feed the whole path string into my function to print the xls file. That is no problem.

Im using randallc's _FileListToArrayNew2g.au3 you can find it here I'm not sure how much help it will be but below is what I have so far. I am aware that _PrintSpec is empty that is because what I have there now dosn't work and is somewhat irrelevant to the problem.

#Include <File.au3>
#Include <Array.au3>
#include <GUIConstants.au3>
#include <_FileListToArrayNew2g.au3>

AutoItSetOption("RunErrorsFatal", 1)

Dim $FileList[1]
If Not _LoadSpecsFromHD() Then _LoadSpecFromNetwork()

$SpecPrintGUI = GUICreate("Spec Print - Jon Copas 2007", 271, 342, -1, -1)
$StatusLable = GUICtrlCreateLabel("Enter a list of part numbers below each on its own line.", 10, 10, 400)
$EditSpecList = GUICtrlCreateEdit("", 12, 46, 249, 241, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN))
GUICtrlSetData(-1, "")
$ButtonPrintSpec = GUICtrlCreateButton("&Print Specs", 74, 292, 115, 41, 0)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonPrintSpec
            $aPartNumbersToPullAndPrint = StringSplit(GUICtrlRead($EditSpecList), @CR)
            
            $i = 0
            For $element In $aPartNumbersToPullAndPrint
                If $i > 1 Then $aPartNumbersToPullAndPrint[$i] = StringTrimLeft($element, 1)
                $i = $i + 1
            Next
            
            $i = 1
            While $i < $aPartNumbersToPullAndPrint[0] + 1
                _PrintSpec ($aPartNumbersToPullAndPrint[$i])
                MsgBox(0, "Continue...", "Press Ok when spec for " & $aPartNumbersToPullAndPrint[$i] & " has finished printing.")
                $i = $i + 1
            WEnd
            ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
    EndSwitch
WEnd

Func _LoadSpecsFromHD()
    If FileExists(@ScriptDir & "\SpecArray.txt") Then
        ; Loads File Into Array
        FileChangeDir(@ScriptDir)
        FileOpen(@ScriptDir & "\SpecArray.txt", 0)
        _FileReadToArray(@ScriptDir & "\SpecArray.txt", $FileList)
        FileClose(@ScriptDir & "\SpecArray.txt")
    Else
        Return False
    EndIf
    Return True
EndFunc   ;==>_LoadSpecsFromHD

Func _LoadSpecFromNetwork()
    ToolTip("Reading Spec List This May Take Several Minutes...", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
    $FileList = _FileListToArray3 ("N:\", "*.XLS", 1, 1, "", "")

    If @error = 1 Then
        ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
        MsgBox(16, "Error", "Failed To Establish Link on N:\")
        Exit
    EndIf

    If @error = 4 Then
        ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
        MsgBox(16, "Error", "No .XLS Files Found On N:\")
        Exit
    EndIf

    FileChangeDir(@ScriptDir)
    FileDelete("SpecArray.txt")
    FileOpen("SpecArray.txt", 1)
    For $x = 0 To $FileList[0]
        FileWrite("SpecArray.txt", $FileList[$x])
        If $x <> $FileList[0] Then FileWrite("SpecArray.txt", @CRLF)
    Next
    FileClose("SpecArray.txt")

    ToolTip("", @DesktopWidth / 2, @DesktopHeight / 2, '', '', 6)
EndFunc   ;==>_LoadSpecFromNetwork

Func _PrintSpec ($SpecToFind)

EndFunc   ;==>_PrintSpec

Thank you very much!

Have a look at _PathSplit in the help file. Based on what you are looking for, that should give you what you need.

Share this post


Link to post
Share on other sites

@Reaper HGN Thanks,

Unfortunately I don't see _PathSplit in the help file (v3.2.4.9) I did find a thread about it http://www.autoitscript.com/forum/index.php?showtopic=634 but can't get the function to work. You couldn't give me and example usage could you?


AutoIt changed my life.

Share this post


Link to post
Share on other sites

Did you try Toady's code? Thats how I would do it.

Share this post


Link to post
Share on other sites

No, and I feel like a fool, somehow I glassed right over Toadys post. Thanks weapon.

And thanks Toady once again you have came through for me!!!

-Skizmata

#1 Toady Fan


AutoIt changed my life.

Share this post


Link to post
Share on other sites

@Reaper HGN Thanks,

Unfortunately I don't see _PathSplit in the help file (v3.2.4.9) I did find a thread about it http://www.autoitscript.com/forum/index.php?showtopic=634 but can't get the function to work. You couldn't give me and example usage could you?

_PathSplit is in the help file. Type it on the Index tab. Here is the example from the help file.

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath,"Demo _PathSplit()")

Share this post


Link to post
Share on other sites

_PathSplit is in the help file. Type it on the Index tab. Here is the example from the help file.

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath,"Demo _PathSplit()")
I think people miss things in the help file because they use the 'Contents' tab too much. The function _PathSplit is under 'File Management' in the 'Contents' tab, which might not be obvious to everyone. I never use that, always preferring the 'Index' tab instead.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

_PathSplit is included in File.au3 UDF

You may take a look at and get some ideas :)


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

If you need only file name, try RegExp:

$FileName = StringRegExpReplace("N:\Specs Sheets\Aus Filter\Air\AF11420.xls", "^.*\\", "")
Edited by MsCreatoR

 

Spoiler

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

Spoiler

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

Wow its amazing how far we have came in this thread from my first posting. I need to learn more about string regular expressions. Very good job Creator!


AutoIt changed my life.

Share this post


Link to post
Share on other sites

I need to learn more about string regular expressions.

...and then you can explain it to me!

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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