Highlighted
Absent Member.
Absent Member.
690 views

How could I randomize a value when creating a new directory?

Jump to solution

Hello All, 

 

Hope everyone is doing well. 

I'm trying to make the following code dynamic in respect to the xyz in the new_dir.

So, char new_dir[] = "C:\\xyz";      the xyz portion I'd like to insert a random.

How can this be done?

 

    char filename[1024], command[1024];

    char new_dir[] = "C:\\xyz";

    // Create a directory under root called xyz and make it the current dir

    if (mkdir(new_dir))

        lr_output_message ("Create directory %s failed", new_dir);

    else

        lr_output_message ("Created new directory %s", new_dir);

    sprintf(filename, "%s\\%s", new_dir, "newfile.txt");

    // Execute a dir /b command and direct it to a new file

    sprintf(command, "dir /b c:\\ > %s /w", filename);

    system(command);

    lr_output_message ("Created new file %s", filename);

 Thanks, and kind regards.

QA_Automator

 

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Established Member..
Established Member..

Re: How could I randomize a value when creating a new directory?

Jump to solution

char filename[1024], command[1024], randomNumber[1024];

  //  char new_dir[] = "C:\\xyz"; no need

    // Create a  {randomDirectory1}-> updates once every iteration

    if (mkdir(lr_eval_string("C:\\{RandomDirectory1}")))

        lr_output_message ("Create directory %s failed", lr_eval_string("C:\\{RandomDirectory1}"));

    else

        lr_output_message ("Created new directory %s", lr_eval_string("C:\\{RandomDirectory1}"));
    
    lr_save_string(lr_eval_string("C:\\{RandomDirectory1}"),"path");

    sprintf(randomNumber,"%s",lr_eval_string("{path}"));
    sprintf(filename, "%s\\newfile.txt", randomNumber);

    // Execute a dir /b command and direct it to a new file

    sprintf(command, "dir /b c:\\ > %s /w", filename);

    system(command);

    lr_output_message ("Created new file %s", lr_eval_string("C:\\{RandomDirectory1}"));

Pavankumar

View solution in original post

5 Replies
Highlighted
Established Member..
Established Member..

Re: How could I randomize a value when creating a new directory?

Jump to solution

not sure why you want to create a random directory.. but here is a way to do random filename in a setdirectory

 

sprintf(randomNumber,"%s",lr_eval_string("{RandomNumber}"));
sprintf(filename,"C:\\PreSetFolderName\%s_NewFile.txt",randomNumber);

 

 

Pavankumar
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: How could I randomize a value when creating a new directory?

Jump to solution

Hello, Pavankumar.

 

Cheers, for the code mate! Nice one.

This is great to do as you say, static directory with random named text file.

 

I'm attempting just the opposite: Randomly created directory, with static named text file, (actually static image file)

 

The reason for this, our AUT streams a local image to a unique sub-folder on the server, then retrieves the image from the server and archives it during a soap request when the user processes the transaction.

 

Any further assistance is most appreciated.

If I get it sorted I'll post code on the same.

 

Cheers!

QA_Automator

0 Likes
Highlighted
Established Member..
Established Member..

Re: How could I randomize a value when creating a new directory?

Jump to solution

char filename[1024], command[1024], randomNumber[1024];

  //  char new_dir[] = "C:\\xyz"; no need

    // Create a  {randomDirectory1}-> updates once every iteration

    if (mkdir(lr_eval_string("C:\\{RandomDirectory1}")))

        lr_output_message ("Create directory %s failed", lr_eval_string("C:\\{RandomDirectory1}"));

    else

        lr_output_message ("Created new directory %s", lr_eval_string("C:\\{RandomDirectory1}"));
    
    lr_save_string(lr_eval_string("C:\\{RandomDirectory1}"),"path");

    sprintf(randomNumber,"%s",lr_eval_string("{path}"));
    sprintf(filename, "%s\\newfile.txt", randomNumber);

    // Execute a dir /b command and direct it to a new file

    sprintf(command, "dir /b c:\\ > %s /w", filename);

    system(command);

    lr_output_message ("Created new file %s", lr_eval_string("C:\\{RandomDirectory1}"));

Pavankumar

View solution in original post

Highlighted
Absent Member.
Absent Member.

Re: How could I randomize a value when creating a new directory?

Jump to solution

Brilliant, Pavankumar! This did the trick.

Now I'm sorting through net use command permission issues running the script in performance center.

It works fine locally. Always something to debug!

 

Cheers!

DJay

QA_Automator

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: How could I randomize a value when creating a new directory?

Jump to solution

Hmmm,

 

I have this working locally, but when I upload to Performance Center, it fails to mkdir and the script stops.

I've admin rights to all LGs.

 

If anyone has time to think on this, or knows what may be the issues, I'd really appreciate it. I've run out of things to try.

 

Thanks!

DJay

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.