Jump to content

How to add "and" function to a BAT file to copy files and then rename

Rohith_Kumar_Sp
Go to solution Solved by mariushm,

why don't you write a basic script in php or some other language?

 

A php script can be run from command line, simply say c:\programs\php\php.exe c:\path\to\your\php_script.php    - you can make it a shortcut if you want. 

(assuming php is unpacked/installed in c:\programs\php folder) 

 

A basic php script would be something like this (I'm literally writing the code in this comment box so it probably has some errors but you can read the php documentation and figure things out)  :

 

<?php

// the folder where you want to look for files, use / instead of \, 
// or if you want to use \, escape it by writing \\ instead of \
$folder_search = "C:/Users/Rohith/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/";
// where to copy the files
$folder_destination  = "Q:/D Drive/D Drive/Wallpaper/Rohith/New folder (2)/";

// is it a proper folder?
if (is_dir($folder_search)) {
	// try to open a handle to the folder so we can read contents 
    if ($folder_handle = opendir($folder_search)) {
    	// opened folder, now read all the folders and file names from the folder
        while (($filename = readdir($folder_handle)) !== FALSE) {
          // $filename holds the name of the file or folder
          // . and .. are "special" folder names that show up when you parse a folder 
          // they mean . = "refresh" folder, ".." = go back one folder 
          // so we ignore these two.
          if (($filename!='.') && ($filename!='..')) {
            // we only care about files, so ignore folders
            if (is_dir($folder_search.$filename)==true) {
             echo "skipping folder $filename ...\n";
            } else {
              echo "copying $filename... ";
              // copy the file to destination folder, add .jpg to the end of whatever filename we have
              copy($folder_search.$filename,$folder_destination.$filename.".jpg");
              echo "DONE \n";
            }
          }
        }
        // we're closing the folder handle because we're done with it.
        closedir($folder_handle);
    }
}

?>

 

 

 

so i really like the windows login screen images so i had created a bat file to copy from location A to location B and then rename the files to "*jpg" , 

so it looks something like this 
 

ROBOCOPY "C:\Users\Rohith\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" "Q:\D Drive\D Drive\Wallpaper\Rohith\New folder (2)" /mir
Ren *.* *.jpg



this used to work before powershell was so important, it used to run well on cmd prompt, now that powershell is more relevant,  Ren *.* *.jpg just won't work in powershell, so i found another script for it 
 

gci -ex "*.xyz" | ?{!$_.PsIsContainer} | ren -new {$_.name + ".jpg"}



but now i don't know how to combine both, i want to copy a file using first statement and then rename the files in that folder using the 2nd statement, can anyone help me? 

 

 

Spoiler
Spoiler

AMD 5000 Series Ryzen 7 5800X| MSI MAG X570 Tomahawk WiFi | G.SKILL Trident Z RGB 32GB (2 * 16GB) DDR4 3200MHz CL16-18-18-38 | Asus GeForce GTX 3080Ti STRIX | SAMSUNG 980 PRO 500GB PCIe NVMe Gen4 SSD M.2 + Samsung 970 EVO Plus 1TB PCIe NVMe M.2 (2280) Gen3 | Cooler Master V850 Gold V2 Modular | Corsair iCUE H115i RGB Pro XT | Cooler Master Box MB511 | ASUS TUF Gaming VG259Q Gaming Monitor 144Hz, 1ms, IPS, G-Sync | Logitech G 304 Lightspeed | Logitech G213 Gaming Keyboard |

PCPartPicker 

Link to comment
Share on other sites

Link to post
Share on other sites

why don't you write a basic script in php or some other language?

 

A php script can be run from command line, simply say c:\programs\php\php.exe c:\path\to\your\php_script.php    - you can make it a shortcut if you want. 

(assuming php is unpacked/installed in c:\programs\php folder) 

 

A basic php script would be something like this (I'm literally writing the code in this comment box so it probably has some errors but you can read the php documentation and figure things out)  :

 

<?php

// the folder where you want to look for files, use / instead of \, 
// or if you want to use \, escape it by writing \\ instead of \
$folder_search = "C:/Users/Rohith/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/";
// where to copy the files
$folder_destination  = "Q:/D Drive/D Drive/Wallpaper/Rohith/New folder (2)/";

// is it a proper folder?
if (is_dir($folder_search)) {
	// try to open a handle to the folder so we can read contents 
    if ($folder_handle = opendir($folder_search)) {
    	// opened folder, now read all the folders and file names from the folder
        while (($filename = readdir($folder_handle)) !== FALSE) {
          // $filename holds the name of the file or folder
          // . and .. are "special" folder names that show up when you parse a folder 
          // they mean . = "refresh" folder, ".." = go back one folder 
          // so we ignore these two.
          if (($filename!='.') && ($filename!='..')) {
            // we only care about files, so ignore folders
            if (is_dir($folder_search.$filename)==true) {
             echo "skipping folder $filename ...\n";
            } else {
              echo "copying $filename... ";
              // copy the file to destination folder, add .jpg to the end of whatever filename we have
              copy($folder_search.$filename,$folder_destination.$filename.".jpg");
              echo "DONE \n";
            }
          }
        }
        // we're closing the folder handle because we're done with it.
        closedir($folder_handle);
    }
}

?>

 

 

 

Edited by mariushm
corrected typo in code
Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, mariushm said:

why don't you write a basic script in php or some other language?

 

A php script can be run from command line, simply say c:\programs\php\php.exe c:\path\to\your\php_script.php    - you can make it a shortcut if you want. 

(assuming php is unpacked/installed in c:\programs\php folder) 

 

A basic php script would be something like this (I'm literally writing the code in this comment box so it probably has some errors but you can read the php documentation and figure things out)  :

 




<?php

// the folder where you want to look for files, use / instead of \, 
// or if you want to use \, escape it by writing \\ instead of \
$folder_search = "C:/Users/Rohith/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/";
// where to copy the files
$folder_destination  = "Q:/D Drive/D Drive/Wallpaper/Rohith/New folder (2)/";

// is it a proper folder?
if (is_dir($folder_search)) {
	// try to open a handle to the folder so we can read contents 
    if ($folder_handle = opendir($folder_search)) {
    	// opened folder, now read all the folders and file names from the folder
        while (($filename = readdir($folder_handle)) !== FALSE) {
          // $filename holds the name of the file or folder
          // . and .. are "special" folder names that show up when you parse a folder 
          // they mean . = "refresh" folder, ".." = go back one folder 
          // so we ignore these two.
          if (($filename!='.') && ($filename!='..')) {
            // we only care about files, so ignore folders
            if (is_dir($folder_search.$filename)==true) {
             echo "skipping folder $filename ...\n";
            } else {
              echo "copying $filename... ";
              // copy the file to destination folder, add .jpg to the end of whatever filename we have
              copy($folder_search.$filename,$folder_destination.$filaname.".jpg");
              echo "DONE \n";
            }
          }
        }
        // we're closing the folder handle because we're done with it.
        closedir($folder_handle);
    }
}

?>

 

Hi, thanks for the response, i didn't pass out my computers and shifted to VFX, so the things you wrote above, while i can understand some, i have no clue about php scripts, but i'm willing to learn, so as i shared above, i just need to copy files from A to B and rename them, i can see the script you wrote does something similar, but my ghetto scripting was just writing things in notepad and saving it as a .BAT file, how would i incorporate your script? i'm a novice 

 

Spoiler
Spoiler

AMD 5000 Series Ryzen 7 5800X| MSI MAG X570 Tomahawk WiFi | G.SKILL Trident Z RGB 32GB (2 * 16GB) DDR4 3200MHz CL16-18-18-38 | Asus GeForce GTX 3080Ti STRIX | SAMSUNG 980 PRO 500GB PCIe NVMe Gen4 SSD M.2 + Samsung 970 EVO Plus 1TB PCIe NVMe M.2 (2280) Gen3 | Cooler Master V850 Gold V2 Modular | Corsair iCUE H115i RGB Pro XT | Cooler Master Box MB511 | ASUS TUF Gaming VG259Q Gaming Monitor 144Hz, 1ms, IPS, G-Sync | Logitech G 304 Lightspeed | Logitech G213 Gaming Keyboard |

PCPartPicker 

Link to comment
Share on other sites

Link to post
Share on other sites

you save the code as a text file with the extension .php 

 

then you run the script simply by passing it to php.exe as a parameter   

you need to download php for windows and save it in a folder somewhere, just like robocopy or some other application. 

 

php.exe  script.php 

 

php parses the code and does what the code says and then closes. 

 

The code above has a lot of comments but it's basically like this 

 

is the given string a proper folder path? 

  if yes,  then create a connection to folder 

    if a connection is created   start reading records/ file names from the folder until you fail to read any more records  / file names - file name is bad because it's also folder names... so records is more proper

      for every record, if a record/filename is read successfully,  then  

           if the file name is not "."  and it's not ".."   then   if  it's not a folder,  copy file from specified folder to destination folder  adding ".jpg" to the end 

 

close connection to folder (it's closed either way when php.exe quits, but it's good practice to do it)

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, mariushm said:

you save the code as a text file with the extension .php 

 

then you run the script simply by passing it to php.exe as a parameter   

you need to download php for windows and save it in a folder somewhere, just like robocopy or some other application. 

 

php.exe  script.php 

 

php parses the code and does what the code says and then closes. 

 

The code above has a lot of comments but it's basically like this 

 

is the given string a proper folder path? 

  if yes,  then create a connection to folder 

    if a connection is created   start reading records/ file names from the folder until you fail to read any more records  / file names - file name is bad because it's also folder names... so records is more proper

      for every record, if a record/filename is read successfully,  then  

           if the file name is not "."  and it's not ".."   then   if  it's not a folder,  copy file from specified folder to destination folder  adding ".jpg" to the end 

 

close connection to folder (it's closed either way when php.exe quits, but it's good practice to do it)

 

 

so that needs an installation of a software, the bat file i have is a click and go solutions even outside my computer if i wanted to do the same on a different system, is there anyway just to add the "Do this and then do that" function in the bat file? i mean it used to work until microsoft changed how they worked 

 

Spoiler
Spoiler

AMD 5000 Series Ryzen 7 5800X| MSI MAG X570 Tomahawk WiFi | G.SKILL Trident Z RGB 32GB (2 * 16GB) DDR4 3200MHz CL16-18-18-38 | Asus GeForce GTX 3080Ti STRIX | SAMSUNG 980 PRO 500GB PCIe NVMe Gen4 SSD M.2 + Samsung 970 EVO Plus 1TB PCIe NVMe M.2 (2280) Gen3 | Cooler Master V850 Gold V2 Modular | Corsair iCUE H115i RGB Pro XT | Cooler Master Box MB511 | ASUS TUF Gaming VG259Q Gaming Monitor 144Hz, 1ms, IPS, G-Sync | Logitech G 304 Lightspeed | Logitech G213 Gaming Keyboard |

PCPartPicker 

Link to comment
Share on other sites

Link to post
Share on other sites

No installation ... just unzip  php in a folder.  Jesus...  

 

Go to https://windows.php.net/download/ and scroll down to PHP 7.4 (8 is kinda alpha version, too new and not needed for this basic stuff) ... unpack the zip ( any version from that list, thread safe or not thread safe, doesn't matter) in a folder, ex c:\temp\php  , or c:\php or c:\programs\php or whatever (i avoid program files as that requires administrative rights)

Worst case scenario you may need to also run the Visual C++ runtime as php is written in C++ : https://aka.ms/vs/16/release/VC_redist.x64.exe

 

The point is that instead of hoping Microsoft never changes commands and crap and instead of relying on other programs like robocopy and gci command, you can depend strictly on your own code  (copy is a built-in function, opendir, readdir are built-in functions, your script doesn't depend on anything to work) 

 

as to how you'd use it in a batch file?  instead of saying robocopy ... blah,  you say c:\path\to\php.exe  c:\path\to\script.php 

 

that's all ... the folders are saved inside the .php script 

 

And if you're curious, maybe you look in the php documentation at the image functions and for example, you could open each image, determine the resolution (width x height )   and ignore any picture that's less than 800x600 or whatever ... so you don't copy over thumbnails or some other crap  - can't do that in batch files, can you? 

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, mariushm said:

 

And if you're curious, maybe you look in the php documentation at the image functions and for example, you could open each image, determine the resolution (width x height )   and ignore any picture that's less than 800x600 or whatever ... so you don't copy over thumbnails or some other crap  - can't do that in batch files, can you? 

 

thank you so much, so i know i'm asking too much, i looked at the resolution link you gave and was again overwhelmed, how do i do the delete less than 800x800 resolution etc? 

all the comments on that link only talks about resizing the images and stuff, i did find one to get image resolution but i still don't know how to incorporate that into the above script you wrote for me 


 

<?php
$im = imagecreatetruecolor(100, 100);
imageresolution($im, 200);
print_r(imageresolution($im));
imageresolution($im, 300, 72);
print_r(imageresolution($im));
?>


again, thank you for your time on this but it would be really helpful. 

 

Spoiler
Spoiler

AMD 5000 Series Ryzen 7 5800X| MSI MAG X570 Tomahawk WiFi | G.SKILL Trident Z RGB 32GB (2 * 16GB) DDR4 3200MHz CL16-18-18-38 | Asus GeForce GTX 3080Ti STRIX | SAMSUNG 980 PRO 500GB PCIe NVMe Gen4 SSD M.2 + Samsung 970 EVO Plus 1TB PCIe NVMe M.2 (2280) Gen3 | Cooler Master V850 Gold V2 Modular | Corsair iCUE H115i RGB Pro XT | Cooler Master Box MB511 | ASUS TUF Gaming VG259Q Gaming Monitor 144Hz, 1ms, IPS, G-Sync | Logitech G 304 Lightspeed | Logitech G213 Gaming Keyboard |

PCPartPicker 

Link to comment
Share on other sites

Link to post
Share on other sites

Those are functions in the programming language, you use those functions to do stuff with them .

 

For example, you start by opening / loading a picture in memory using a suitable function. For a jpg image, php has the function imagecreatefromjpeg  : https://www.php.net/manual/en/function.imagecreatefromjpeg.php

 

That function receives as parameter a file name (complete with the path to it, unless the picture is in the same folder as the php file) and returns a resource id  which is like a tag, like a memory placeholder, a nickname for the picture. You use this code to perform things on the image from now on.

 

Then you can retrieve information about the image with functions like imagesx - https://www.php.net/manual/en/function.imagesx.php  -  and imagesyhttps://www.php.net/manual/en/function.imagesy.php   -  which tell you how many pixels are horizontally and how many vertically .... and that's your resolution. You can put those numbers in variables and do something with the picture only if those numbers match something you want.

You can define a variable  (store something in memory) simply by placing a $ in front of a word in php.

 

So here's how I would open a picture saved as C:\temp\picture.jpg and get the resolution and display a message using echo only if the image is between 1024 and 1920 pixels wide and between 600 .. 1080 pixels tall :

 

<?php 

$filename = 'c:/temp/picture.jpg';

// load $filename in memory and from now on we refer to it using the
// number the function puts in the $pic variable
$pic = imagecreatefromjpeg($filename);
// if there was an error opening the picture, then instead of a number
// we have the value FALSE in the $pic variable, so we can test and 
// close the script or skip this picture or whatever. Here, just show
// error message and die (stop the script from running instantly).
if ($pic===FALSE) {
	echo "this is a bad picture. can not continue.";
    die();
}
// if we reached this point, the picture is loaded in memory, so let's get
// the width and height : 
$x = imagesx($pic);
$y = imagesy($pic);
// now we want to check if x is between 1024 and 1920 , and y between 600..1080
// we do that in kinda backwards way, which works better with computers. 
// we create a variable which just says true if picture is good, false if not
// and at the beginning we assume the picture is right. THEN, we check x and y
// and if they're bad, we change that variable to FALSE;

// our variable is $picture_is_good - REMEMBER variable names are CASE SENSITIVE
// so make sure you write the name exactly the same every time
//
$picture_is_good = TRUE;  // no idea yet so we guess it's good

// now we check width 
if ( ($x < 1024) || ($x >1920) ) { // x less than , or x larger than, do this
  $picture_is_good = FALSE;  // 
}
// now we check height, and we can also write them separately 
if ($y<600) {
  $picture_is_good = FALSE;
}
if ($y>1080) {
  $picture_is_good = FALSE; 
}

// So at this point, if  x in 1024..1920 and y in 600..1080, picture_is_good remained TRUE
// you check if a variable equals to something with == , you put something in a variable with
// a single =  ...  and in PHP you sometimes have to use === when you want an exact comparison
// 
if ($picture_is_good == TRUE ) {
  // here we can do something with the picture, copy it, resize it, mess with it
  // whatever you want 
}

// now that we're done working with the picture, we can unload it from memory
// which is important if for example, you want to load 100 pictures one after another or something
// like this
//
// imagedestroy function removes the picture from memory and "releases" that location in memory to be
// reused. 
$reply = imagedestroy($pic); 
?>

 

 

You can have all this put in a function  which returns TRUE or FALSE  and then use that function every time  :

 

<?php

function  is_good_picture($filename) { 
	$pic = imagecreatefromjpeg($filename);
	if ($pic===FALSE) {
		// because this is a function now which may run on tens or
        // hundreds of pictures, we no longer want the script to die
        // so we just return FALSE, as if it's a bad picture
        return FALSE; 
	}
    // if we reached this point, the picture is loaded in memory
	$x = imagesx($pic);
	$y = imagesy($pic);
	
    $picture_is_good = TRUE;  // no idea yet so we guess it's good
	// now we check width 
    // if there's a single instruction between { } , we can skip them
    // a beginner should ALWAYS use the brackets even if there's a single
    // instruction 
	if ( ($x < 1024) || ($x >1920) )  $picture_is_good = FALSE; 	
	if ( ($y <  600) || ($y >1080) )  $picture_is_good = FALSE;
	
    // now instead of doing something here with our determination,
    // we just close the picture and return the answer to the main 
    // part of the script. 
    $result = imagedestroy($pic);
    return $picture_is_good;
}

// this is the main part of the script 
// we can now use the function to test pictures : 

$filename = 'c:/test/picture.jpg';

if (is_good_picture($filename)==TRUE) {
	echo "yes, this is a good picture, do something with it"; 
}

// here we test multiple filenames by placing the filenames in an array 

$files = [  'c:/temp/picture1.jpg', 'c:/temp/picture2.jpg' ]; 

// arrays start from 0, and you can use count() to determine how many file names are in array 
for ($i=0; $i<count($files); $i++ ) {  // start at 0, go as long as i is less than count which is 2, every loop increase i by 1
  if ( is_good_picture($files[$i]) == TRUE)  {
     // dot joins variables and chunks of text together
     echo "File called " . $files[$i] . " is a good file. Do something with it"; 
  } else {
     echo "Oh bummer, file " . $files[$i] . " is a naughty file. Skipping it.";
  }
}

// or you can use foreach to achieve the same thing 
// $array_index will be 0, then 1   
// $name will be c:/temp/picture1.jpg, then c:/temp/picture2.jpg
foreach ($files  as $array_index => $name ) { 
  if ( is_good_picture($name) == TRUE) echo "yay, file ".$name."is a good picture!\n";
}
?>

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/9/2021 at 12:45 AM, mariushm said:

Those are functions in the programming language, you use those functions to do stuff with them .

 

For example, you start by opening / loading a picture in memory using a suitable function. For a jpg image, php has the function imagecreatefromjpeg  : https://www.php.net/manual/en/function.imagecreatefromjpeg.php

 

That function receives as parameter a file name (complete with the path to it, unless the picture is in the same folder as the php file) and returns a resource id  which is like a tag, like a memory placeholder, a nickname for the picture. You use this code to perform things on the image from now on.

 

Then you can retrieve information about the image with functions like imagesx - https://www.php.net/manual/en/function.imagesx.php  -  and imagesyhttps://www.php.net/manual/en/function.imagesy.php   -  which tell you how many pixels are horizontally and how many vertically .... and that's your resolution. You can put those numbers in variables and do something with the picture only if those numbers match something you want.

You can define a variable  (store something in memory) simply by placing a $ in front of a word in php.

 

So here's how I would open a picture saved as C:\temp\picture.jpg and get the resolution and display a message using echo only if the image is between 1024 and 1920 pixels wide and between 600 .. 1080 pixels tall :

 



<?php 

$filename = 'c:/temp/picture.jpg';

// load $filename in memory and from now on we refer to it using the
// number the function puts in the $pic variable
$pic = imagecreatefromjpeg($filename);
// if there was an error opening the picture, then instead of a number
// we have the value FALSE in the $pic variable, so we can test and 
// close the script or skip this picture or whatever. Here, just show
// error message and die (stop the script from running instantly).
if ($pic===FALSE) {
	echo "this is a bad picture. can not continue.";
    die();
}
// if we reached this point, the picture is loaded in memory, so let's get
// the width and height : 
$x = imagesx($pic);
$y = imagesy($pic);
// now we want to check if x is between 1024 and 1920 , and y between 600..1080
// we do that in kinda backwards way, which works better with computers. 
// we create a variable which just says true if picture is good, false if not
// and at the beginning we assume the picture is right. THEN, we check x and y
// and if they're bad, we change that variable to FALSE;

// our variable is $picture_is_good - REMEMBER variable names are CASE SENSITIVE
// so make sure you write the name exactly the same every time
//
$picture_is_good = TRUE;  // no idea yet so we guess it's good

// now we check width 
if ( ($x < 1024) || ($x >1920) ) { // x less than , or x larger than, do this
  $picture_is_good = FALSE;  // 
}
// now we check height, and we can also write them separately 
if ($y<600) {
  $picture_is_good = FALSE;
}
if ($y>1080) {
  $picture_is_good = FALSE; 
}

// So at this point, if  x in 1024..1920 and y in 600..1080, picture_is_good remained TRUE
// you check if a variable equals to something with == , you put something in a variable with
// a single =  ...  and in PHP you sometimes have to use === when you want an exact comparison
// 
if ($picture_is_good == TRUE ) {
  // here we can do something with the picture, copy it, resize it, mess with it
  // whatever you want 
}

// now that we're done working with the picture, we can unload it from memory
// which is important if for example, you want to load 100 pictures one after another or something
// like this
//
// imagedestroy function removes the picture from memory and "releases" that location in memory to be
// reused. 
$reply = imagedestroy($pic); 
?>

 

You can have all this put in a function  which returns TRUE or FALSE  and then use that function every time  :

 

 

 

 

so i have some doubts, how do i not select the file but the entire folder? becasue if i use the "C/testpicture.jpg" i can't possible enter the file names manually, how do i tell it to open the files in the folder and check for the dimensions ? also is this how i can combine both scripts in one ?



 

<?php

// the folder where you want to look for files, use / instead of \, 
// or if you want to use \, escape it by writing \\ instead of \
$folder_search = "C:/Users/Rohith/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/";
// where to copy the files
$folder_destination  = "Q:/D Drive/D Drive/Wallpaper/Rohith/Temp/";

// is it a proper folder?
if (is_dir($folder_search)) {
	// try to open a handle to the folder so we can read contents 
    if ($folder_handle = opendir($folder_search)) {
    	// opened folder, now read all the folders and file names from the folder
        while (($filename = readdir($folder_handle)) !== FALSE) {
          // $filename holds the name of the file or folder
          // . and .. are "special" folder names that show up when you parse a folder 
          // they mean . = "refresh" folder, ".." = go back one folder 
          // so we ignore these two.
          if (($filename!='.') && ($filename!='..')) {
            // we only care about files, so ignore folders
            if (is_dir($folder_search.$filename)==true) {
             echo "skipping folder $filename ...\n";
            } else {
              echo "copying $filename... ";
              // copy the file to destination folder, add .jpg to the end of whatever filename we have
              copy($folder_search.$filename,$folder_destination.$filename.".jpg");
              echo "DONE \n";
            }
          }
        }
        // we're closing the folder handle because we're done with it.
        closedir($folder_handle);
    }
}

?>

<?php 

$filename = 'Q:/D Drive/D Drive/Wallpaper/Rohith/Temp/picture.jpg';

// load $filename in memory and from now on we refer to it using the
// number the function puts in the $pic variable
$pic = imagecreatefromjpeg($filename);
// if there was an error opening the picture, then instead of a number
// we have the value FALSE in the $pic variable, so we can test and 
// close the script or skip this picture or whatever. Here, just show
// error message and die (stop the script from running instantly).
if ($pic===FALSE) {
	echo "this is a bad picture. can not continue.";
    die();
}
// if we reached this point, the picture is loaded in memory, so let's get
// the width and height : 
$x = imagesx($pic);
$y = imagesy($pic);
// now we want to check if x is between 1024 and 1920 , and y between 600..1080
// we do that in kinda backwards way, which works better with computers. 
// we create a variable which just says true if picture is good, false if not
// and at the beginning we assume the picture is right. THEN, we check x and y
// and if they're bad, we change that variable to FALSE;

// our variable is $picture_is_good - REMEMBER variable names are CASE SENSITIVE
// so make sure you write the name exactly the same every time
//
$picture_is_good = TRUE;  // no idea yet so we guess it's good

// now we check width 
if ( ($x < 1024) || ($x >1920) ) { // x less than , or x larger than, do this
  $picture_is_good = FALSE;  // 
}
// now we check height, and we can also write them separately 
if ($y<600) {
  $picture_is_good = FALSE;
}
if ($y>1080) {
  $picture_is_good = FALSE; 
}

// So at this point, if  x in 1024..1920 and y in 600..1080, picture_is_good remained TRUE
// you check if a variable equals to something with == , you put something in a variable with
// a single =  ...  and in PHP you sometimes have to use === when you want an exact comparison
// 
if ($picture_is_good == TRUE ) {
  // here we can do something with the picture, copy it, resize it, mess with it
  // whatever you want 
}

// now that we're done working with the picture, we can unload it from memory
// which is important if for example, you want to load 100 pictures one after another or something
// like this
//
// imagedestroy function removes the picture from memory and "releases" that location in memory to be
// reused. 
$reply = imagedestroy($pic); 
?>

<?php

function  is_good_picture($filename) { 
	$pic = imagecreatefromjpeg($filename);
	if ($pic===FALSE) {
		// because this is a function now which may run on tens or
        // hundreds of pictures, we no longer want the script to die
        // so we just return FALSE, as if it's a bad picture
        return FALSE; 
	}
    // if we reached this point, the picture is loaded in memory
	$x = imagesx($pic);
	$y = imagesy($pic);
	
    $picture_is_good = TRUE;  // no idea yet so we guess it's good
	// now we check width 
    // if there's a single instruction between { } , we can skip them
    // a beginner should ALWAYS use the brackets even if there's a single
    // instruction 
	if ( ($x < 1024) || ($x >1920) )  $picture_is_good = FALSE; 	
	if ( ($y <  600) || ($y >1080) )  $picture_is_good = FALSE;
	
    // now instead of doing something here with our determination,
    // we just close the picture and return the answer to the main 
    // part of the script. 
    $result = imagedestroy($pic);
    return $picture_is_good;
}

// this is the main part of the script 
// we can now use the function to test pictures : 

$filename = 'Q:/D Drive/D Drive/Wallpaper/Rohith/Temp/picture.jpg';

if (is_good_picture($filename)==TRUE) {
	echo "yes, this is a good picture, do something with it"; 
}

// here we test multiple filenames by placing the filenames in an array 

$files = [  'Q:/D Drive/D Drive/Wallpaper/Rohith/Temp/picture1.jpg', 'Q:/D Drive/D Drive/Wallpaper/Rohith/Temp/picture2.jpg' ]; 

// arrays start from 0, and you can use count() to determine how many file names are in array 
for ($i=0; $i<count($files); $i++ ) {  // start at 0, go as long as i is less than count which is 2, every loop increase i by 1
  if ( is_good_picture($files[$i]) == TRUE)  {
     // dot joins variables and chunks of text together
     echo "File called " . $files[$i] . " is a good file. Do something with it"; 
  } else {
     echo "Oh bummer, file " . $files[$i] . " is a naughty file. Skipping it.";
  }
}

// or you can use foreach to achieve the same thing 
// $array_index will be 0, then 1   
// $name will be c:/temp/picture1.jpg, then c:/temp/picture2.jpg
foreach ($files  as $array_index => $name ) { 
  if ( is_good_picture($name) == TRUE) echo "yay, file ".$name."is a good picture!\n";
}
?>

 

 

Spoiler
Spoiler

AMD 5000 Series Ryzen 7 5800X| MSI MAG X570 Tomahawk WiFi | G.SKILL Trident Z RGB 32GB (2 * 16GB) DDR4 3200MHz CL16-18-18-38 | Asus GeForce GTX 3080Ti STRIX | SAMSUNG 980 PRO 500GB PCIe NVMe Gen4 SSD M.2 + Samsung 970 EVO Plus 1TB PCIe NVMe M.2 (2280) Gen3 | Cooler Master V850 Gold V2 Modular | Corsair iCUE H115i RGB Pro XT | Cooler Master Box MB511 | ASUS TUF Gaming VG259Q Gaming Monitor 144Hz, 1ms, IPS, G-Sync | Logitech G 304 Lightspeed | Logitech G213 Gaming Keyboard |

PCPartPicker 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Rohith_Kumar_Sp said:

 

 

 

so i have some doubts, how do i not select the file but the entire folder? becasue if i use the "C/testpicture.jpg" i can't possible enter the file names manually, how do i tell it to open the files in the folder and check for the dimensions ? also is this how i can combine both scripts in one ?

 

 

I showed you the code where all that is put into a function  called is_good_picture  that takes a parameter/argument which is the full file name with the path and everything and replies with TRUE or FALSE 

 

Think of everything between  function is_good_picture($filename)  and the ending }  as a block  with one input and one output which can be reused in lots of places in your script. 

The block takes the input  (a file name with the file path)  and this input is called $filename but it could be called anything, like for example we could call it $a or $input or $picture , it's just a label to make it easy to refer to it inside the block, don't get stuck on it. 

The output of the function is what send out that block using the return keyword. You can put that anywhere in a function and the function ends there. 

 

 

Right there in the code you pasted, where it says echo "copying $filename..." ;  

Between those  {  }   the file name with the path of each file is stored  in that $filename variable  - it just so happens the variable is named $filename, just like the parameter of the function, but it doesn't have to be the same. 

 

So you can paste the function anywhere in the script (can be at the top or all the way at the bottom) and then you can add in that section stuff like 

// is_good_picture  puts TRUE if the resolution is 1024...1920 wide, 600..1080 tall , FALSE otherwise 


$a_variable_name = is_good_picture($filename); 


if ($a_variable_name == TRUE ) {   

  //  do something with the file , copy it over, whatever 

}

 

-

 

There's also built in functions which can extract the folder, the name and the extension of a file from the whole file path and name  : https://www.php.net/manual/en/function.pathinfo.php

 

So for example, if you want to only copy files that already have the jpg or jpeg extension, you could do something like this:

$file_parts  = pathinfo($filename);

// pathinfo returns an array with the parts of the file path : 

//  'dirname' - folder part of the path 
// 'basename' - the name without extension  = picture.jpg  : picture  
// 'extension' - the extension  = picture.jpg  : jpg
// 'filename'  - name and extension combined


// because we only want to see if the extension is JPG or JPEG, I convert the extension to lowercase so that I don't have to check separately 
// if the extension is JPG or JPG or jPg or whatever  (the file names are case sensitive, but Windows ignores it, Linux doesn't)
// strtolower takes a string of text (the contents of the  $file_parts['extension']) and makes it lowercase
//

$extension_lowercase = strtolower($file_parts['extension']);

if ( ($extension_lowercase == 'jpg') || ($extension_lowercase == 'jpeg')) {
	// yes, this is a jpg picture, now i can do something with it here
}

 

 

So basically, you want your script to open a folder, go through each folder record inside (folders, files), and only pick files , and then you can check the file if it's a picture or not, if the resolution is according to your liking or not, whatever ... when everything is right about the file you can copy it over or do whatever.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×