Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

perl grep dir and filename

// description of your code here

my($directory, $filename) = $text =~ m/(.*\/)(.*)$/;
print "D=$directory, F=$filename\n

Processing all files in a directory and its subdirectories ... revisited

Dir['**/*.jpg'].foreach do |filename|
  ... do anything with the filename
end

Processing all files in a directory and its subdirectories ... revisited

// This code shows (once again) how to process all files in a directory and its subdirectories

Dir['**/*.jpg'].foreach do |filename|
  ... do anything with the filename
end

Create a file directory listing in XML

// This code lists the filenames in the current directory which have a .xml extension, then saves them to an XML file.called 'dir.xml'.

require 'rexml/document'

include REXML

class Xmldirectory
  def initialize
  end

  def create_directory_file
    file = File.new('dir.xml','w')
    doc = Document.new
    doc.add_element('dir')

    Dir["*.xml"].each do |x|
      entry = Element.new('file')
      entry.text = x
      doc.root.add_element entry
    end
    file.puts doc
    file.close
  end
end

C - command "ls"

/*
 *
 * Esempio che scansiona una cartella stampando a video i file in essa
 * contenuti.
 */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
	DIR *dir;
	struct dirent *drent;

	if(argc < 2)
	{
		fprintf(stderr, "%s <directory>\n", argv[0]);
		return EXIT_FAILURE;
	}

	if((dir = opendir(argv[1])) == NULL)
	{
		fprintf(stderr, "Errore opendir()\n");
		return EXIT_FAILURE;
	}

	while((drent = readdir(dir)) != NULL)
	{
		fprintf(stdout, "--> %s\n", drent->d_name);
	}
	
	if(closedir(dir) < 0)
	{
		fprintf(stderr, "Errore closedir()\n");
		return EXIT_FAILURE;
	}
}

Load file info for all files in a directory

    load-dir: func [
        "Load file info for all files in a directory."
        path /local result orig-dir
    ][
        result: copy []
        orig-dir: what-dir
        change-dir dirize path
        foreach file read %. [
            repend result [
                file
                ;file: get-modes file 'full-path
                make info? file  get-modes file get-modes file 'file-modes
            ]
            ; include # of files in sub-dirs?
            ; include full-path?
        ]
        change-dir orig-dir
        result
    ]

Ensure a directory exists

    ensure-dir-exists: func [dir] [attempt [make-dir/deep dir]]

Copy a directory

    copy-dir: func [source dest] [
        if not exists? dest [make-dir/deep dest]
        foreach file read source [
            either find file "/" [
                copy-dir source/:file dest/:file
            ][
                print file
                write/binary dest/:file read/binary source/:file
            ]
        ]
    ]

Return block of files, all dirs first

    dir-file-sort: func [
        {Returns the block of files with directories first, followed by
        files, with each group sorted.}
        block [any-block!]
        /local result
    ][
        result: copy []
        foreach blk lib/ser/split block [:dir?] none [
            append result sort blk
        ]
    ]

Get all sub-directories

    dirs: func [
        {Returns a block of fully qualified subdirectories for the directory.}
        spec  [file!]   "Starting directory"
        block [block!]  "Block to append to"
        /deep           "Recurse sub-directories."
        /local f-spec
    ][
        spec: dirize spec
        foreach file read spec [
            if dir? f-spec: join spec file [
                append block f-spec
                if deep [all-dirs/deep f-spec block]
            ]
        ]
        block
    ]
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS