1

php, command line, windows.

I need to sequentially number each .txt file in a directory. Any way I can specify the first number to use in the sequence in the command line when I type the script? (Instead of every time manually editing the script itself).

Or instead (even better) being prompted to enter the first number twice (for confirmation)?

Like, in command line ("285603" is just an example number):

c:\a\b\currentworkingdir>php c:\scripts\number.php 285603

or (even better)

c:\a\b\currentworkingdir>php c:\scripts\number.php
c:\a\b\currentworkingdir>Enter first number: 
c:\a\b\currentworkingdir>Re-enter first number: 

The numbering script:

<?php
$dir = opendir('.');
// i want to enter this number OR being prompted for it to enter twice in the command line 
$i = 285603;
while (false !== ($file = readdir($dir)))
{
    if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'txt')
    { 
        $newName = $i . '.txt';
        rename($file, $newName);
        $i++;
    }
}
closedir($dir);
?>

Any hints please?

user3026965
  • 673
  • 4
  • 8
  • 22

2 Answers2

1

Command-line arguments are available to PHP in the global $argv array, as described in the manual here

This array contains the name of the script, followed by each proceeding argument. In your case, when you run:

php c:\scripts\number.php 285603

The argument 285603 will be available as the variable $argv[1]. You can replace your $i variable with this, and the script will work as intended.

iainn
  • 16,826
  • 9
  • 33
  • 40
1

You should use $argv variable. It is an array with a first element indicating script file name and next elements are arguments passed. Given you type php script.php 1234 in the console, $argv variable is as follows:

array(4) {
  [0]=>
  string(10) "script.php"
  [1]=>
  string(4) "1234"
}

EDIT: Your code should be like below:

<?php
# CONFIRMATION
echo 'Are you sure you want to do this [y/N]';
$confirmation = trim(fgets( STDIN));
if ($confirmation !== 'y') {
   exit (0);
}

$dir = opendir('.');
$i = $argv[1];
while (false !== ($file = readdir($dir)))
{
    if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'txt')
    { 
        $newName = $i . '.txt';
        rename($file, $newName);
        $i++;
    }
}
closedir($dir);
?>
  • thanks! i am a novice to php. do i insert this into my numbering script? or create a new script with it and place into the same numbering-script dir? else? i normally have to number 5-20K files at once (if that makes a difference re scripting). – user3026965 May 18 '17 at 13:20
  • Just replace `$i = 285603;` with `$i = $argv[1];`. – Damian Czapiewski May 18 '17 at 13:30
  • ` string(13) "numbering.php" [1]=> string(6) "$argv[1]" } $i = $argv[1];` //Parse **error:** syntax error, unexpected '{' in C:\scripts\numbering.php on line 3 – user3026965 May 18 '17 at 13:45
  • beautiful! thanks! any way do double enter the number (for confirmation)? – user3026965 May 18 '17 at 13:53
  • Sure, check my answer. I added confirmation code at the beginning. – Damian Czapiewski May 18 '17 at 14:03
  • nice safety confirmation `[y/N]`, thanks, will keep in the code! any way to require to **actually re-type** the number a second time? or maybe enter as `$argv[2]`, like `>php c:\scripts\number.php 285603 285603` and error message if the two numbers are not equal (i need to enter 6-7 digit numbers, so this would ensure that there is no mistyping before renaming everything)? – user3026965 May 18 '17 at 20:28
  • this is what i did to re-enter the number: `$confirmation = trim(fgets( STDIN)); if ($confirmation !== $argv[1]) { exit (0); }` thanks again! – user3026965 May 18 '17 at 20:37