Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl Script to create sample files in each directories created.

by rahul_lfo (Initiate)
on Oct 08, 2013 at 08:46 UTC ( [id://1057381]=perlquestion: print w/replies, xml ) Need Help??

rahul_lfo has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks. I am facing a problem in creating the perl script to generate random data inside the directories created through the script. The script I am using to create directories is given below.What is need is that these directories should have a sample data like .png files in it---
#!/usr/bin/perl use strict; use warnings; sub MakeDirs($$$); MakeDirs("C:\\Documents and Settings\\Rahul\\Desktop\\perl\\directory\ +\New2", 15, 2); sub MakeDirs($$$) { my($base, $num_dirs, $depth) = @_; mkdir $base; #if depth = 0, no more subdirectories need to be created if($depth == 0) { return 0; } #Recurse through the directories my $dir_name = "a"; for(my $x = 10; $x < $num_dirs; $x++) { MakeDirs("$base/$dir_name", $num_dirs, $depth - 1); $dir_name++; } open(DIR, ">>contacts".$i.".json"); }

Replies are listed 'Best First'.
Re: Perl Script to create sample files in each directories created.
by kcott (Archbishop) on Oct 08, 2013 at 09:35 UTC

    G'day rahul_lfo,

    Welcome to the monastery.

    "The script I am using to create directories is given below.What is need is that these directories should have a sample data like .png files in it---"

    You can use the builtin module File::Copy to copy (or move) the sample data into the directories you create.

    The code you posted is incomplete and won't work, which you would have found out had you run it. What's "open(DIR, ">>contacts".$i.".json");" supposed to do? Where you aware that "$i" has not been declared or assigned a value? Did you have a question about this? The guidelines in "How do I post a question effectively?" may help you: a better question will get you better answers.

    -- Ken

Re: Perl Script to create sample files in each directories created.
by Laurent_R (Canon) on Oct 08, 2013 at 11:58 UTC

    Hi, it is good that you post the code your have tried, but you did not state what your problem is, nor asked any question.

    There are a few problems in your code (some already reported in previous posts), but you'll most probably find out by yourself (at least for the most severe ones) if you try to compile it and run it.

    One additional comment: you probably don't want to use function prototypes unless you clearly know why and really understand what they are doing. Don't do it if you don't know, not using them will not bite you.

      Thanks for your replies!. The problem is that I need a directory structure (which I am able to generate through the above perl script) which has sample files like .png or .doc etc. Currently I am generating directories with the above perl script and then by using touch(shell) command to create the file in there directories. The shell script to create files in there directories is given below-
      #!/bin/bash random_content () { < /dev/urandom tr -dc A-Za-z0-9_ | head -c300 } for I in {1..10} do touch file$I.txt random_content > file$I.png done
      I am a newbie ,I need these two codes to work together. Please guide.
Re: Perl Script to create sample files in each directories created.
by Anonymous Monk on Oct 08, 2013 at 09:21 UTC
Re: Perl Script to create sample files in each directories created.
by Laurent_R (Canon) on Oct 09, 2013 at 16:59 UTC

    If you need to create empty files in the directories, just open a file with Perl and close it. Something like this:

    my $file = "$dir/file.png"; open my $FH, ">", $file or die "could not open $file $!"; close $FH;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1057381]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-03-19 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found