cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
wiebepo
Level III

get file size

I am using JMP 10.0.0 with Windows 7, and would like to use JSL to determine a file size in bytes given a file path. Here is an example structure of what I am trying to achieve, where in JMP 10.0.0 'file size()' is not an option.

f_path=pick file();

f_size=file size(f_path);

Is there a method to determine the file size without openning or loading the file?

1 ACCEPTED SOLUTION

Accepted Solutions
mattflynn
Level III

Re: get file size

Nice one with R function, MS!

For Windows, here is the hard way.

// filesize.jsl

fname = "c:/temp/bubble.jpg";

//******************************************

// with JMP version 11

rc = if(host is(Windows) & JMP Version()=="11.0.0",

  fs = File Size(fname);

  );

//******************************************

// with R file.info {base}

// http://stat.ethz.ch/R-manual/R-devel/library/base/html/file.info.html

// returns a data frame with various file info, is a dir?, times, IDs, etc.;

R Init();

R Send(fname);

R submit("\[

  df = file.info(fname);

]\");

fs = R get(df$size);

show(fs);   // in KB;

R Term();

//******************************************

// or, the hard way

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364955(v=vs.85).aspx

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364957(v=vs.85).aspx

kernel32 = Load DLL( "kernel32" );

// use createfile to retrive a reference to a file, i.e. a file handle

kernel32 << DeclareFunction( "CreateFileA", Convention( STDCALL ), Alias( "CreateFile" ),

  Arg( AnsiString( 256 ), "lpFileName" ),

  Arg( Int32, "dwDesiredAccess" ),

  Arg( Int32, "dwShareMode" ),

  Arg( IntPtr, "lpSecurityAttributes" ),

  Arg( Int32, "dwCreationDisposition" ),

  Arg( Int32, "dwFlagsAndAttributes" ),

  Arg( UIntPtr, "hTemplateFile" ),

  Returns( Int32 )

);

open_existing = 3;

hFile = kernel32 << CreateFile( fname, 0, 0, 0, open_existing, 0, 0);

// GetFileSize requires file handle (from above)

kernel32 << DeclareFunction( "GetFileSizeEx", Convention( STDCALL ), Alias( "GetFileSizeEx" ),

  Arg( Int32, "hFile" ),

  Arg( Int32, "lpFileSize", update  ),

  Returns( Int32 )

);

fs = 0;

rc =  kernel32 << GetFileSizeEx( hFile, fs );

show(fs);

// clean up

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

kernel32 << DeclareFunction( "CloseHandle", Convention( STDCALL ), Alias( "CloseHandle" ),

  Arg( Int32, "hFile" ),

  Returns( Int32 )

);

rc =  kernel32 << CloseHandle( hFile );

kernel32 << Unload DLL();

Best regards,-Matt

View solution in original post

4 REPLIES 4
gzm
gzm
Level III

Re: get file size

The File Size() function is available in JMP 11.

Below is a modification of a portion of the script "2_Extra_MSDirUsingWeb.jsl" from chapter 2 of the book "JSL Companion Applications of the JMP Scripting Language."  It is a kludged solution that works for Windows only.  Note, the script is a bit wordy and has extra commands to demonstrate the functions and output.  The full script describes the syntax of the MS DOS dir command.

==========================================================================================================

// cmd_dir is the command to get a listing of JMP sample data

// c:\temp\jmpDir.bat is the command file to run

// c:\temp\dirOut.txt is the file of results from the dir command

batFid = "c:\temp\jmpDir.bat";

outDir = "c:\temp\dirOut.txt";

// Get the full path for a file

f_path0=pick file();

f_path = Convert File Path( f_path, Windows );

// Build the DOS "dir" command

cmd_txt ="\[dir /-C /T:C /S "]\" 

|| f_path

|| "\[" > ]\"

|| outDir;

show(cmd_txt);

// Save to a file with a .bat extension.  Windows interprets .bat files

// the same as the Run (run command) window on the Start Button

Save Text File( batFid, cmd_txt );

// Windows interprets this  "open" path file as "run"

t0 = Today();

Web( batFid );

Wait( 1 ); 

// Wait a second for Windows to complete, maybe more if a huge directory.

// Note the wait is the only control you have and then look to see if the file exists.

// Show results

// A couple of "checks" that you may employ here if it does not exist or if it is older than expected

If( !File Exists( outDir ) | (Creation Date( outDir ) - t0 > 10),

      Caption( "There was a problem with the dir command, it did not run. Aborting..." );

      Wait( 4 );

      Caption( Remove );

      Throw();

);

_crlf = "\!N";

dir_results = words(Load Text File(outDir),_crlf);

dir_size_str = words(dir_results[nitems(dir_results)-1]);

f_size = num(dir_size_str[3]);

show(dir_size_str, f_size);  //number of bytes

//=============================================================================

//the command below createsa file that shows the results of the DOS dir command that was used

// Open the results file

dir_dt = Open(

      outDir,

      columns( Name( "Dir results" ) = Character ),

      Import Settings(

            End Of Line( CRLF ),

            End Of Field( CRLF ),  // ensures only 1 column

            Strip Quotes( 1 ),

            Use Apostrophe as Quotation Mark( 1 ),

            Scan Whole File( 1 ),

            Treat empty columns as numeric( 0 ),

            Labels( 0 ),

            Data Starts( 1 ),

            Lines To Read( All ),

            Year Rule( "10-90" )

      )

);

ms
Super User (Alumni) ms
Super User (Alumni)

Re: get file size

If you have R installed this should work (only tested in JMP 10 for Mac):

R Init();

mypath = Pick File();

R Execute( {mypath}, {x}, "x <- file.info(mypath)$size" );

Show( x );

R Term();

mattflynn
Level III

Re: get file size

Nice one with R function, MS!

For Windows, here is the hard way.

// filesize.jsl

fname = "c:/temp/bubble.jpg";

//******************************************

// with JMP version 11

rc = if(host is(Windows) & JMP Version()=="11.0.0",

  fs = File Size(fname);

  );

//******************************************

// with R file.info {base}

// http://stat.ethz.ch/R-manual/R-devel/library/base/html/file.info.html

// returns a data frame with various file info, is a dir?, times, IDs, etc.;

R Init();

R Send(fname);

R submit("\[

  df = file.info(fname);

]\");

fs = R get(df$size);

show(fs);   // in KB;

R Term();

//******************************************

// or, the hard way

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364955(v=vs.85).aspx

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364957(v=vs.85).aspx

kernel32 = Load DLL( "kernel32" );

// use createfile to retrive a reference to a file, i.e. a file handle

kernel32 << DeclareFunction( "CreateFileA", Convention( STDCALL ), Alias( "CreateFile" ),

  Arg( AnsiString( 256 ), "lpFileName" ),

  Arg( Int32, "dwDesiredAccess" ),

  Arg( Int32, "dwShareMode" ),

  Arg( IntPtr, "lpSecurityAttributes" ),

  Arg( Int32, "dwCreationDisposition" ),

  Arg( Int32, "dwFlagsAndAttributes" ),

  Arg( UIntPtr, "hTemplateFile" ),

  Returns( Int32 )

);

open_existing = 3;

hFile = kernel32 << CreateFile( fname, 0, 0, 0, open_existing, 0, 0);

// GetFileSize requires file handle (from above)

kernel32 << DeclareFunction( "GetFileSizeEx", Convention( STDCALL ), Alias( "GetFileSizeEx" ),

  Arg( Int32, "hFile" ),

  Arg( Int32, "lpFileSize", update  ),

  Returns( Int32 )

);

fs = 0;

rc =  kernel32 << GetFileSizeEx( hFile, fs );

show(fs);

// clean up

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

kernel32 << DeclareFunction( "CloseHandle", Convention( STDCALL ), Alias( "CloseHandle" ),

  Arg( Int32, "hFile" ),

  Returns( Int32 )

);

rc =  kernel32 << CloseHandle( hFile );

kernel32 << Unload DLL();

Best regards,-Matt

wiebepo
Level III

Re: get file size

Thank you for all the responses.Declaring a function using the kernel32 dll works best for my application.