Jump to content

remove file extensions from array


mud409
 Share

Recommended Posts

I'm attempting to loop a process using the file names of a directory. I'm using _FileListToArrayEx to feed the directory to an array. However, it outputs the entire file name including extension, and I only need the file name. How would I remove this from the array?

I don't have much for code yet, because I can't do anything till I remove that extension. Below is _FileListToArrayEx.

;===============================================================================

;

; Description: lists all or preferred files and or folders in a specified path (Similar to using Dir with the /B Switch)

; Syntax: _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '')

; Parameter(s): $sPath = Path to generate filelist for

; $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details, support now for multiple searches

; Example *.exe; *.txt will find all .exe and .txt files

; $iFlag = determines weather to return file or folders or both.

; $sExclude = exclude a file from the list by all or part of its name

; Example: Unins* will remove all files/folders that start with Unins

; $iFlag=0(Default) Return both files and folders

; $iFlag=1 Return files Only

; $iFlag=2 Return Folders Only

;

; Requirement(s): None

; Return Value(s): On Success - Returns an array containing the list of files and folders in the specified path

; On Failure - Returns the an empty string "" if no files are found and sets @Error on errors

; @Error or @extended = 1 Path not found or invalid

; @Error or @extended = 2 Invalid $sFilter or Invalid $sExclude

; @Error or @extended = 3 Invalid $iFlag

; @Error or @extended = 4 No File(s) Found

;

; Author(s): SmOke_N

; Note(s): The array returned is one-dimensional and is made up as follows:

; $array[0] = Number of Files\Folders returned

; $array[1] = 1st File\Folder

; $array[2] = 2nd File\Folder

; $array[3] = 3rd File\Folder

; $array[n] = nth File\Folder

;

; All files are written to a "reserved" .tmp file (Thanks to gafrost) for the example

; The Reserved file is then read into an array, then deleted

;===============================================================================

Func _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '')

If Not FileExists($sPath) Then Return SetError(1, 1, '')

If $sFilter = -1 Or $sFilter = Default Then $sFilter = '*.*'

If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0

If $sExclude = -1 Or $sExclude = Default Then $sExclude = ''

Local $aBadChar[6] = ['\', '/', ':', '>', '<', '|']

For $iCC = 0 To 5

If StringInStr($sFilter, $aBadChar[$iCC]) Or _

StringInStr($sExclude, $aBadChar[$iCC]) Then Return SetError(2, 2, '')

Next

If StringStripWS($sFilter, 8) = '' Then Return SetError(2, 2, '')

If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, '')

If Not StringInStr($sFilter, ';') Then $sFilter &= ';'

Local $aSplit = StringSplit(StringStripWS($sFilter, 8), ';'), $sRead

For $iCC = 1 To $aSplit[0]

If StringStripWS($aSplit[$iCC], 8) = '' Then ContinueLoop

If StringLeft($aSplit[$iCC], 1) = '.' And _

UBound(StringSplit($aSplit[$iCC], '.')) - 2 = 1 Then $aSplit[$iCC] = '*' & $aSplit[$iCC]

Local $iPid = Run(@ComSpec & ' /c ' & 'dir "' & $sPath & '\' & $aSplit[$iCC] _

& '" /b /o-e /od', '', @SW_HIDE, 6)

While 1

$sRead &= StdoutRead($iPid)

If @error Then ExitLoop

WEnd

Next

If StringStripWS($sRead, 8) = '' Then SetError(4, 4, '')

Local $aFSplit = StringSplit(StringTrimRight(StringStripCR($sRead), 1), @LF)

Local $sHold

For $iCC = 1 To $aFSplit[0]

If $sExclude And StringLeft($aFSplit[$iCC], _

StringLen(StringReplace($sExclude, '*', ''))) = StringReplace($sExclude, '*', '') Then ContinueLoop

Switch $iFlag

Case 0

$sHold &= $aFSplit[$iCC] & Chr(1)

Case 1

If StringInStr(FileGetAttrib($sPath & '\' & $aFSplit[$iCC]), 'd') Then ContinueLoop

$sHold &= $aFSplit[$iCC] & Chr(1)

Case 2

If Not StringInStr(FileGetAttrib($sPath & '\' & $aFSplit[$iCC]), 'd') Then ContinueLoop

$sHold &= $aFSplit[$iCC] & Chr(1)

EndSwitch

Next

If StringTrimRight($sHold, 1) Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))

Return SetError(4, 4, '')

EndFunc

#include <array.au3>; Only used for _ArrayDisplay

;Return all files/folders

$hFilesFolders = _FileListToArrayEx(@ScriptDir & "\roms")

_ArrayDisplay($hFilesFolders, 'Files and Folders')

Edited by mud409
Link to comment
Share on other sites

For $i = 1 to UBound($YourArray)                      ; Determines the size of the array and repeats that many times.
  $YourArray[$i] = StringTrimRight($YourArray[$i],4)     ; The 4 is the length of the extension, I.E. ".jpg"
Next

This should do it.

-Fett

P.S. We have awesome AutoIt tags - [ autoit][/ autoit] (minus the spaces) for code. :graduated:

Edited by fett8802
[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
Share on other sites

I use this because many file extensions have more than 3 letters like .docx, .dvr-ms, etc...

For $i = 1 to UBound($YourArray) - 1    ;need  -1 or you'll go out of range
  $YourArray[$i] = StringTrimRight(StringRegExpReplace($YourArray[$i], '[^\.]+$', ''), 1)   ;Use this just in case the extension is more than 3 letters like "Video.dvr-ms"
Next

Link to comment
Share on other sites

Thanks for showing me up!!! :x

(Just kidding)

I've never used the StringRegExpReplace function. Looks useful!

[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...