Jump to content

New project advise


 Share

Go to solution Solved by soonyee91,

Recommended Posts

For a new project i need to lookup all the available .pdf files in a certain directory.

The pdf format looks like below

material-1a_124567.pdf

material-1b_12345_12345.pdf

material-1c_12345_nesting.pdf

material-1d_us_04030-2010-09-03.pdf

material-1e_z35_SL1020_01.pdf

material-1f_W1305050.pdf

material-1g_SL1025_02.pdf

material-1h_painting.pdf

The above mentioned pdf file names are all the available variations that can occur, but it is also possible
that there are only one or two of the the file's mentioned above.

My goal:

I want to read all the available filenames(without) the extension and

the prefix "material-1a_" (and so on for the next files), and store the value in an array? or each file in a different variable?,

but in a way that material-1a is always available in the same variable or array position, so i can use
them to place the value in an excell sheet.

Is this possible or are there better/smarter ways to do this.

 

Thanks in advance!

 

 

Link to comment
Share on other sites

  • Moderators

You can use Melba's RecFileListToArray to do what you're after. Regardless of the number of files, it will put them into an array for you. You can then manipulate the array as you see fit.

Edit: forgot the link

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

drbyte,

There are many ways to do it.

1. To allow user to choose multiple files from a directory("use fileopendialog or Melba's RecFileListToArray")

2. To obtain filenames, use pathsplit function or other similar function

3. stringsplit function can be use to split the strings into array by specify delimeters >>>show in example below

#include <Array.au3>
Local $result[8],$result1
; below is just some example for you to start
;you can use fileopendialog to retrieve multiple pdf files from certain directory
; then you convert the return value(obtained from fileopendialog) into array
; use pathsplit to obtain filenames only

$result[0] = "material-1a_124567.pdf"
$result[1]="material-1b_12345_12345.pdf"
$result[2]="material-1c_12345_nesting.pdf"
$result[3]="material-1d_us_04030-2010-09-03.pdf"
$result[4]="material-1e_z35_SL1020_01.pdf"
$result[5]="material-1f_W1305050.pdf"
$result[6]="material-1g_SL1025_02.pdf"
$result[7]="material-1h_painting.pdf"

For  $i =0 to 7
$result1=stringsplit($result[$i],"_",2)
_arraydisplay($result1)
next

Hope this can help you to start with your project!

Link to comment
Share on other sites

no. op doesn't want to choose the filles, but wants automatically lists them to array. and it doesn't need _pathsplit to strip awway the ".pdf"

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

My goal:

I want to read all the available filenames(without) the extension and

the prefix "material-1a_" (and so on for the next files), and store the value in an array? or each file in a different variable?,

 

 

and it doesn't need _pathsplit to strip awway the ".pdf"

Actually, that was one of the conditions.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ok, this is one of the way to write the prefix "material-1a, material-1b & etc" to excel range A1 in column order:

#include <Array.au3>
#include <Excel.au3>
Local $result[8],$result1
; below is just some example for you to start
;you can use fileopendialog to retrieve multiple pdf files from certain directory
; then you convert the return value(obtained from fileopendialog) into array
; use pathsplit to obtain filenames only

$result[0] = "material-1a_124567.pdf"
$result[1]="material-1b_12345_12345.pdf"
$result[2]="material-1c_12345_nesting.pdf"
$result[3]="material-1d_us_04030-2010-09-03.pdf"
$result[4]="material-1e_z35_SL1020_01.pdf"
$result[5]="material-1f_W1305050.pdf"
$result[6]="material-1g_SL1025_02.pdf"
$result[7]="material-1h_painting.pdf"

Local $oExcel=_ExcelBookNew()

For  $i =0 to 7
$result1=stringsplit($result[$i],"_",2)

_ExcelWriteCell($oExcel,$result1[0],$i+1,1)
next

Msgbox(0,"","Write to excel file complete! Excel will close without saving!")

_ExcelBookClose($oExcel,0)

$oExcel=""
Edited by soonyee91
Link to comment
Share on other sites

#include <File.au3>

$aFiles = _FileListToArray(@ScriptDir, '*.pdf', 1)

For $i = 1 To $aFiles[0]
    $iFilelength = StringLen($aFiles[$i])
    ConsoleWrite($aFiles[$i] & @LF)
    $sTrimmed = StringMid($aFiles[$i],13, $iFilelength - (12 + 4))
    ConsoleWrite($sTrimmed & @LF)
Next

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

@edano and the rest, i'am still very interested in the given solutions, but time is my biggest
problem at this moment, i did not have time to read them until now..... :shifty:

This weekend i'am gonna try all the possible solutions given, and comment on this
great forum.

Thnx a lot for the support!

 

Link to comment
Share on other sites

drbyte,

One more version

#include <array.au3>
#include <File.au3>
#include <Excel.au3>

; ------------------------------------------------------------------------------------
; create these files in @scriptdir\test\ for testing
; ------------------------------------------------------------------------------------

local $result[8]

$result[0]="material-1a_124567.pdf"
$result[1]="material-1b_12345_12345.pdf"
$result[2]="material-1c_12345_nesting.pdf"
$result[3]="material-1d_us_04030-2010-09-03.pdf"
$result[4]="material-1e_z35_SL1020_01.pdf"
$result[5]="material-1f_W1305050.pdf"
$result[6]="material-1g_SL1025_02.pdf"
$result[7]="material-1h_painting.pdf"

for $1 = 0 to ubound($result) - 1
    fileopen(@scriptdir & '\test\' & $result[$1],10)
Next

; ------------------------------------------------------------------------------------
; start of script that you'll use
; ------------------------------------------------------------------------------------

; list all .pdf type files from a specific directory
$atemp_Files = _FileListToArray(@ScriptDir & '\test\', '*.pdf', 1)

; create final result array with 2 dimensions
local $aFiles[ubound($atemp_Files)][2]

; populate 1ST dimension of final result array with complete file name (not path)

for $1 = 0 to ubound($atemp_Files) - 1
    $aFiles[$1][0] = $atemp_Files[$1]
next

; populate 2ND dimension of final result array with stripped out file name

local $aTmp
for $1 = 1 to ubound($aFiles) - 1
    $aTmp = stringregexp($aFiles[$1][0],'[^_]*_(.*)\.pdf',3)
    if isarray($aTmp) then $aFiles[$1][1] = $aTmp[0]
next

_arraydelete($aFiles,0)

; ------------------------------------------------------------------------------------
; create spreadsheet (shamelessly plagarized from soonyee91)
; ------------------------------------------------------------------------------------

Local $oExcel=_ExcelBookNew()

For $1 = 0 to ubound($aFiles) - 1
    _ExcelWriteCell($oExcel,$aFiles[$1][0],$1,1)
    _ExcelWriteCell($oExcel,$aFiles[$1][1],$1,5)
next

Msgbox(0,"","Write to excel file complete! Excel will close without saving!")

_ExcelBookClose($oExcel,0)

$oExcel=""

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

@JohnOne, sooyee91, kylomas (very nice regexpression, how do you build them?)

You solution works fine, but how to get the values
on a fixed place in the array? or in a fixed variable

I need this because not allways all the pdf documents will be needed.

See the examples below

example1

material-1a_124567.pdf - always 1th record
material-1b_12345_12345.pdf - always 2th record
material-1c_12345_nesting.pdf - always 3th record
material-1d_us_04030-2010-09-03.pdf - always 4th record
material-1e_z35_SL1020_01.pdf - always 5th record
material-1f_W1305050.pdf - always 6th record
material-1g_SL1025_02.pdf - always 7th record
material-1h_painting.pdf- - always 8th record
 
 

example2

material-1b_12345_12345.pdf - always 2th record
material-1c_12345_nesting.pdf - always 3th record
material-1f_W1305050.pdf - always 6th record
material-1g_SL1025_02.pdf - always 7th record
 
I also need to remove:
_us_ from the 4th record
_z35_ from the 5th record
 
 

Thanx again :-)

 

 

Link to comment
Share on other sites

I think I can't really get what you want to do.

if you obtain the file names using fileopendialog/any similar function. The files that stored in the array will be according to "window's sorting condition". You may sort them to your own prefrence using arraysort function.

If you use arraydelete to remove certain array position it will reorganise the index hence not the fixed position you want. Or you can try set that array position to nothing.

For example:

array[0] ="" ; means for the first term set it to nothing.

By the way, there are alot of ways to do it. Can't really give you example as I can't understand your criteria well, perhaps kylomas can...XD)

Link to comment
Share on other sites

By the way, there are alot of ways to do it. Can't really give you example as I can't understand your criteria well, perhaps kylomas can...XD)

This is the time where you put up code showing what you are doing. 

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

drbyte,

The following returns fle name segments between the last "_" and the first ".pdf" found in the file name. There is also an example of looking for a value in the file name segment and writing it to the console.

#include <array.au3>
#include <File.au3>
#include <Excel.au3>

; ------------------------------------------------------------------------------------
; create these files in @scriptdir\test\ for testing
; ------------------------------------------------------------------------------------

local $result[8]

$result[0]="material-1a_124567.pdf"
$result[1]="material-1b_12345_12345.pdf"
$result[2]="material-1c_12345_nesting.pdf"
$result[3]="material-1d_us_04030-2010-09-03.pdf"
$result[4]="material-1e_z35_SL1020_01.pdf"
$result[5]="material-1f_W1305050.pdf"
$result[6]="material-1g_SL1025_02.pdf"
$result[7]="material-1h_painting.pdf"

for $1 = 0 to ubound($result) - 1
    fileopen(@scriptdir & '\test\' & $result[$1],10)
Next

; ------------------------------------------------------------------------------------
; start of script that you'll use
; ------------------------------------------------------------------------------------

; list all .pdf type files from a specific directory
$atemp_Files = _FileListToArray(@ScriptDir & '\test\', '*.pdf', 1)

; create final result array with 2 dimensions
local $aFiles[ubound($atemp_Files)][2]

; populate 1ST dimension of final result array with complete file name (not path)

for $1 = 0 to ubound($atemp_Files) - 1
    $aFiles[$1][0] = $atemp_Files[$1]
next

; populate 2ND dimension of final result array with stripped out file name

local $aTmp
for $1 = 1 to ubound($aFiles) - 1
    ;$aTmp = stringregexp($aFiles[$1][0],'[^_]*_(.*)\.pdf',3)
    $aTmp = stringregexp($aFiles[$1][0],'.*_(.*)\.pdf',3)
    if isarray($aTmp) then $aFiles[$1][1] = $aTmp[0]
next

_arraydelete($aFiles,0)
_arraydisplay($aFiles,0)

; ------------------------------------------------------------------------------------
; iterate array looking for element with "paint" in the name
; ------------------------------------------------------------------------------------

for $1 = 0 to ubound($aFiles) - 1
    if stringinstr($aFiles[$1][1],'paint') > 0 then
        ConsoleWrite('Found "paint" in element ' & $1 & ' of array' & @LF)
    EndIf
next

; ------------------------------------------------------------------------------------
; create spreadsheet (shamelessly plagarized from soonyee91)
; ------------------------------------------------------------------------------------

;~ Local $oExcel=_ExcelBookNew()

;~ For $1 = 0 to ubound($aFiles) - 1
;~  _ExcelWriteCell($oExcel,$aFiles[$1][0],$1,1)
;~  _ExcelWriteCell($oExcel,$aFiles[$1][1],$1,5)
;~ next

;~ Msgbox(0,"","Write to excel file complete! Excel will close without saving!")

;~ _ExcelBookClose($oExcel,0)

;~ $oExcel=""

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I think I can't really get what you want to do.

Let me explain again in another way what my goal is with this application.

Read all the pdf file's in a certain directory and put a part of the pdf filenames into an array or variable(s).

It depends on the job how many pdf files there will be, sometimes there are 8 pdf files, sometimes there are 3 pdf files, or even 1 pdf file is possible.

From the prefix of the pdf file (material-1a) i know what type of document it is, and where to place its value

after the prefix "material-1a_" in the excell sheet.

When there are 8 pdf file's the array is filled from 1 to 8 with the values, so i know wich value is on array position 8, or 7

and so on.

When there are (for example) 3 pdf file's i don't know how to determine the values in the array.

example1

material-1a_124567.pdf - always 1th record in array
material-1b_12345_12345.pdf - always 2th record in array
material-1c_12345_nesting.pdf - always 3th record in array
material-1d_us_04030-2010-09-03.pdf - always 4th record in array
material-1e_z35_SL1020_01.pdf - always 5th record in array
material-1f_W1305050.pdf - always 6th record in array
material-1g_SL1025_02.pdf - always 7th record in array
material-1h_painting.pdf- - always 8th record in array
 
 

example2

material-1b_12345_12345.pdf - first record in array - with 8 pdf files this is the 2nd record in the array
material-1c_12345_nesting.pdf - second record in array - with 8 pdf files is this the 3th record in the array
material-1f_W1305050.pdf - 3th record in array - with 8 pdf files is this the 6th record in the array
 
So i need to know on which position in the array "material-1a" and so on is, which ofcourse depends on how may pdf files there are in the directory.

 

Link to comment
Share on other sites

drbyte,

The following returns fle name segments between the last "_" and the first ".pdf" found in the file name. There is also an example of looking for a value in the file name segment and writing it to the console.

 

The "looking for a value" could be the solution, i need to know on which position in the

array the value for material-1a or material-1b and so on is, so that i always take the correct value to place int the excell sheet.

I think i solved it with the code below. (only for looking up 1a)

For $1 = 0 To UBound($aFiles) - 1
    If StringInStr($aFiles[$1][0], '-1a') > 0 Then
        ConsoleWrite('Found "1a" in element ' & $1 & ' of array' & @LF)
    EndIf
Next

Is there a smart way to search from 1a to 1f ?

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