Count *.tif files from today in all subdirectory's

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
bennie
Newbie
Posts: 2
Joined: Wed Aug 18, 2021 7:11 am

Count *.tif files from today in all subdirectory's

Post by bennie » Wed Aug 18, 2021 7:29 am

Hi All,

I need a bit off your help to finish a script.
I want to generate a count report of new *.tif files in a project folder and skip te ones whit a # in them.
I have:

C:\workdir\proj1
C:\workdir\proj2
C:\workdir\#services

//In the proj1 folder are subfolders with tif files. I need to know howmany tif files are added on a dayly base.
//here is my script so far:
#########################################################################

Code: Select all

//make a list of dirs to scan:
Let>GFL_TYPE=1
Let>GFL_SORTTYPE=1
GetFileList>C:\workdir\,strDirList,;
Separate>strDirList,;,dirs
let>l=dirs_count

//Get the date:
day>d
Month>m
Year>y
let>date=%d%-%M%-%Y%

//Open an sheet for the output and set the first free row:
XLOpen>C:\workdir\#Services\xlsheed.xlsx,0,xlBook
XLGetSheetDims>xlBook,Blad2,r,nCo

//Loop trough the dirs and skip the ones with #:
Let>k=0
Repeat>k
  Let>k=k+1
  Let>r=r+1
  Let>dir=dirs_%k%
  Position>#,dir,1,nPos,TRUE
  if>nPos<>0,skip
  // write date and folder name to excel:
  XLSetCell>xlBook,Blad2,r,1,%date%,scResult
  XLSetCell>xlBook,Blad2,r,2,%dir%,scResult
  //Find today Tif files count
[b] #########HELP###########[/b]
  // Write %TCount% to excel
  XLSetCell>xlBook,Blad2,r,3,%TCount%,scResult
  label>skip
Until>k,l
XLSave>xlBook,C:\workdir\#Services\xlsheed.xlsx

XLQuit>xlBook
########################################################################
I have tryed to change this to do my bidding witout any luk.

Code: Select all

VBSTART
Function NewestFile(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oFile In oFolder.Files
If DateDiff("s", dPrevDate, oFile.DateLastModified) > 0 Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
End If
Next
NewestFile = sNewestFile
End Function
VBEND
VBEval>NewestFile("C:\workdir\proj1"),filename
I tryed this but get a empty result:

Code: Select all

VBSTART
Function Tifcount(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oFile In oFolder.Files
x = DateDiff("d", oFolder.getdetailsOf(File, 3), Now())
                    If x = 0 Then count = count + 1
If DateDiff("s", dPrevDate, oFile.DateLastModified) > 0 Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
End If
Next
Tifcount = count
End Function
VBEND
VBEval>Tifcount("C:\workdir\proj1"),count
mdl>count
Last edited by bennie on Wed Aug 18, 2021 11:13 am, edited 2 times in total.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Count *.tif files from today in all subdirectory's

Post by Dorian (MJT support) » Wed Aug 18, 2021 10:47 am

This will give you a starting point. You may have to play with it to get the exact output you want, but it goes through each subfolder, looks for files, and writes them to Excel if their filedate matches today.

Code: Select all

Let>TCount=0
//Set to find dirs
Let>GFL_TYPE=1
Let>GFL_SORTTYPE=1
GetFileList>c:\workdir\*.*,strDirList,;

//Set GFL_TYPE to find files
Let>GFL_TYPE=0
Separate>strDirList,;,dirs
let>l=dirs_count

//Get the date
day>dd
Month>mm
Year>yyyy
let>xldate=%yyyy%-%mm%-%dd%
stringreplace>xldate,-,,date

//Open Excel
XLOpen>c:\workdir\#Services\xlsheed.xlsx,1,xlBook
XLGetSheetDims>xlBook,Blad2,r,nCo


Let>k=0
Repeat>k
Let>k=k+1
Let>r=r+1
Let>dir=dirs_%k%
Position>#,dir,1,nPos,TRUE

  //If # is not present
  if>nPos=0

    //Get list of files in this folder
    GetFileList>%dir%\*.*,strFileList,;
    Separate>strFileList,;,files
    let>fc=files_count
    
    
    Let>kk=0

    //Loop through these files
    Repeat>kk
      Let>kk=kk+1

      //Get the file date
      FileDate>files_%kk%,FileDate

      //Is it today?
      If>FileDate=date
        let>r=r+1
        XLSetCell>xlBook,Blad2,r,1,%xldate%,scResult
        XLSetCell>xlBook,Blad2,r,2,files_%kk%,scResult
        let>TCount=TCount+1
      endif

    Until>kk,files_count
  endif

// Write %TCount% for each subfolder to Excel
  if>nPos=0
    XLSetCell>xlBook,Blad2,r,3,%TCount%,scResult
    Let>TCount=0
  endif
Until>k,l

//Save and Quit
XLSave>xlBook,c:\workdir\#Services\xlsheed.xlsx
//XLQuit>xlBook
Yes, we have a Custom Scripting Service. Message me or go here

bennie
Newbie
Posts: 2
Joined: Wed Aug 18, 2021 7:11 am

Re: Count *.tif files from today in all subdirectory's

Post by bennie » Wed Aug 18, 2021 11:06 am

Hi Dorian,

Thanks for your help!!
The problem is there are over 300k tif files in the folders and going to them 1 by 1 is taking to mutch time.
I only need the number of added files on a dayly base. not the filenames oid.

I have fount a powershell shript to do my bidding but was unable to get it in ms.
Just now I fond a post from marcus on this subject : https://www.mjtnet.com/blog/2018/04/04/ ... ur-macros/
Bud its hard to get the powershell variable back to the ms script. I'm trying it now whit the exitcode, as I can't find any other way to do it.
Maby someone out here knows how?

For the shareing:

Code: Select all

  LabelToVar>MyPowershellScript,theScript
  Run>powershell.exe -executionPolicy bypass -command "%theScript%"
  Let>TCount=RP_RESULT

/*
MyPowershellScript:
 $test = (Get-ChildItem -Path %dir%\*.tif -recurse | ? { $_.CreationTime -gt (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-1) }).count
exit $test;
*/

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Count *.tif files from today in all subdirectory's

Post by Dorian (MJT support) » Wed Aug 18, 2021 11:21 am

Powershell isn't in my repertoire, but maybe you can save it to a .bat file and then run that?

I know there are others here who know Powershell and vbs too.
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Count *.tif files from today in all subdirectory's

Post by Grovkillen » Wed Aug 18, 2021 8:31 pm

Use the tool sfk.exe together with list command. You can output it to a text file.

Code: Select all

sfk.exe list -since ...
http://www.stahlworks.com/dev/swiss-file-knife.html

Perhaps the switch "-today" is exactly what you want (instead of the "-since" switch).

And the "-pure" will output the result with only the file names.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Count *.tif files from today in all subdirectory's

Post by Grovkillen » Thu Aug 19, 2021 7:47 pm

Code: Select all

sfk.exe list -today -juststat -dir "C:\root\folder\for\tifs" -file *.tif
And if you want to exclude those # folders you do it like this:

Code: Select all

sfk.exe list -today -juststat -dir "C:\root\folder\for\tifs" !# -file *.tif
And if you want to get the info using the clipboard you do it like this:

Code: Select all

Let>RP_WAIT=1
Let>SFK_COMMAND=cmd /c C: & cd "%SCRIPT_DIR%" & sfk.exe list -today -juststat -dir "C:\root\folder\for\tifs" !# -file *.tif | clip
RunProgram>SFK_COMMAND
GetClipBoard>CLIPBOARD
RegEx>\d*(?=\sfiles),CLIPBOARD,0,FOUND_FILES,,0,,
Let>TIFS_OF_TODAY=FOUND_FILES_1
MDL>TIFS_OF_TODAY
Make sure the sfk.exe is in the script directory and save the test script before you run it.

If you want to not include tif files with the # you use this instead (move the !# to behind the -file switch):

Code: Select all

Let>SFK_COMMAND=cmd /c C: & cd "%SCRIPT_DIR%" & sfk.exe list -today -juststat -dir "C:\root\folder\for\tifs" -file *.tif !# | clip

Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts