How to make a script

Discuss and share scripts and script files...
Post Reply
Onnix
Posts: 12
Joined: 14 Aug 2023 15:03

How to make a script

Post by Onnix »

Hello everyone.

I'm wondering if anyone could please help me automate the tasks I'm currently performing manually, as listed below.

I also want that, within a directory with multiple subdirectories, when I select the parent directory and run the script, these tasks will be performed for each subdirectory.

In a subdirectory:

Select all files that do not end with the character “_a” (excluding double quotes).
Copy those files and paste them into the same directory 30 times.

Select all files that end with the character “_a”.
Copy those files and paste them 3 times into the same directory.

Select all files that end with the character “_a - Copy.”.

Change those characters to “ - Copy (5)_a”

Select all files that end with the character “_a - Copy (2)”.

Change those characters to “ - Copy (10)_a”

Select all files with the characters “_a - Copy (3)” at the end.

Change those characters to “ - Copy (20)_a”

Select all files without the characters “ - Copy” at the end.

Change those characters to “ - Copy (1)”.

Select all files with the characters “ - Copy” at the end.

Change those characters to “ - Copy (0)”.

Select all files with the characters “_a - Copy (1)” at the end.

Change those characters to “ - Copy (30)_a”

Find and replace the characters at the end of the file name (0)...(9) with (00)...(09)

Find and replace the characters “Copy ” with “ ” .

highend
Posts: 14922
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to make a script

Post by highend »

And now try it with a real world example...

In a folder named "tst1" you have these two files:

Code: Select all

hello_a.txt
world.txt
Show the FULL content of the folder (including ALL files) after the renaming process is done!
One of my scripts helped you out? Please donate via Paypal

Onnix
Posts: 12
Joined: 14 Aug 2023 15:03

Re: How to make a script

Post by Onnix »

Thank you for your reply. My files are all in pairs with the same name, one file with and one file without the _a character at the end, and they are both mp3 files:
hello.mp3
hello_a.mp3

My goal is:
- Duplicate the hello.mp3 file 30 times
- Duplicate the hello_a.mp3 file 3 times
- Arrange the files with the _a character at the end into the positions shown in the list
The final result will be as follows:
hello - (00).mp3
hello - (01).mp3
hello - (02).mp3
hello - (03).mp3
hello - (04).mp3
hello - (05).mp3
hello - (05)_a.mp3
hello - (06).mp3
hello - (07).mp3
hello - (08).mp3
hello - (09).mp3
hello - (10).mp3
hello - (10)_a.mp3
hello - (11).mp3
hello - (12).mp3
hello - (13).mp3
hello - (14).mp3
hello - (15).mp3
hello - (16).mp3
hello - (17).mp3
hello - (18).mp3
hello - (19).mp3
hello - (20).mp3
hello - (20)_a.mp3
hello - (21).mp3
hello - (22).mp3
hello - (23).mp3
hello - (24).mp3
hello - (25).mp3
hello - (26).mp3
hello - (27).mp3
hello - (28).mp3
hello - (29).mp3
hello - (30).mp3
hello - (30)_a.mp3

highend
Posts: 14922
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to make a script

Post by highend »

Let's say your root folder is:
D:\Music

And it has all direct subfolders like
a
b
c
...

And these subfolders contain your file pairs

then navigate to D:\
and select the "Music" folder and THEN execute the script...

Use it on some test data first!

Code: Select all

    // Run over all (sub-)directories
    $sel = <curitem>;
    end (exists($sel) != 2), "No folder selected, aborted!";

    $folders = listfolder($sel, , 2, <crlf>);
    end (!$folders), "No subfolder(s) found, aborted!";

    $forA = "10|20|30";
    foreach($folder, $folders, <crlf>, "e") {
        $files = listfolder($folder, , 1, <crlf>);
        foreach($file, $files, <crlf>, "e") {
            $ext  = gpc($file, "ext");
            $base = gpc($file, "base");
            // Handle "_a" files
            // Make 3 copies and rename the original
            if (regexmatches($base, "_a$")) {
                $base = regexreplace($base, "_a$");
                foreach($item, $forA, , "e") {
                    $new = $base . " - (" . $item . ")_a." . $ext;
                    copyitem $file, "$folder\$new";
                }
                $new = $base . " - (05)_a." . $ext;
                renameitem($new, $file);
            // Handle all other files
            // Make 30 copies and rename them with 00, 01, ...
            } else {
                $i = 0;
                while ($i++ < 30) {
                    $new = $base . " - (" . format($i, "00") . ")." . $ext;
                    copyitem $file, "$folder\$new";
                }
                $new = $base . " - (00)." . $ext;
                renameitem($new, $file);
            }
        }
    }
One of my scripts helped you out? Please donate via Paypal

Onnix
Posts: 12
Joined: 14 Aug 2023 15:03

Re: How to make a script

Post by Onnix »

Excellent. It works exactly as I expected. Thank you very much.

Post Reply