How to get a list of all files in an IFS folder

  • Home
  • /
  • Blog
  • /
  • How to get a list of all files in an IFS folder

April 4, 2019

How to get a list of all files in an IFS folder

By NickLitten

April 4, 2019

IBM i, CL, CLLE, programming, qshell, snippet

Talking to a colleague this morning, we were discussing a vendor supplied series of spreadsheets (in CSV format) that his team needed to process. I was surprised to see there were hundreds of files in this folder and he wanted to list them, and assign different files to different people.

“What’s the best way to print a list of all the files in an IFS folder so we can process them? And make it repeatable because we get a new list sent via FTP every few weeks”

my mate, a tiny wrinkly old bald bloke in the office

Well, we have a nice simple technique to use in a little CL (IBM i Control Language) program using QSHELL to list the files.

What is QSHELL?

Qshell is an optional command-line interpreter (shell) for the IBM i operating system. It provides a Unix-like environment based on POSIX and X/Open standards. Qshell allows users to interact with the operating system using commands and scripts, similar to other Unix-like shells.

Here are some key features of Qshell:

  • Command Interpreter: Qshell reads and interprets commands from an input source, executing them using the operating system’s services.
  • Utilities: It includes various utilities (commands) that provide additional functions, ranging from simple to complex.
  • Scripting Environment: Qshell supports writing and running shell scripts, which can be used across different systems without modification.
  • File Management: It allows managing files in any file system supported by the Integrated File System (IFS).

Qshell is particularly useful for tasks like managing files, running threaded programs, and developing custom utilities.

Can we use QSHELL to list IFS Files?

Of course we can, to list all files in an IFS (Integrated File System) folder using QSHELL on an IBM i system, you can use the “ls” command. Here’s a simple example:

  1. Open QSHELL: QSH
  2. Navigate to the desired directory: cd /path/to/your/IFS/folder
  3. List all files: ls -a

If you want to list files with specific details or in a sorted manner, you can modify the command. For example, to list all files including hidden ones and sort them, you can use:

ls -al | sort

This command will list all files (-a), provide detailed information (-l), and sort the output.

List all files using QSHELL

QSH CMD('ls /home/littenn/*.csv -a | sort')

What are we saying here:

  • LS – List
  • /home/littenn – this is the folder that we want to list
  • *.csv – only list the comma separated value files (ie: something.csv)
  • -a – displays all files
  • | sort – give it in a nice, sorted list please old chap

QSHELL is a funny old thing – even though it runs as part of your interactive session, it’s not really running as part of your interactive session. QSHELL runs as a background job which talks to your interactive session while its doing things. It uses three different files to communicate with you:

  • STDIN – the input file (or commands you want to run)
  • STDOUT – the output file (or results from the input command)
  • STDERR – that’s right. The Error file. #oops

When you are using QSHELL, your running job becomes the terminal while the background QSHELL job launches into action waiting for input from the STDIN process. Anything you type into QSHELL gets placed in the STDIN pile, its processed and the results are placed in the STDOUT pile for you to enjoy. Or, obviously, the STDERR pile for you to not enjoy.

Since we just want to read a list of all the *.CSV files in a folder, lets generate a simple list of those files into a temporary work file and read it, processing each one as we go.

This already sounds like an ideal candidate for a relatively simple Control Language program to do the heavy listing for us.

Let’s look at some CONTROL LANGUAGE code on the IBM i System:

List all files in an IFS Folder with QSHELL
    • I’ve been meaning to write a blog about using SQL to read an IFS Folder for the longest time! I know you comment is old but its finally percolated to the top of my “to do” list. Cheers~!

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

    >