I had ChatGPT write me a script that would convert gif and png files to jpg, would it actually work though?

Cryso Agori

V.I.P. Member
I heard ChatGPT could write code so I made it create a script that can find any png and gif files in a folder and convert those into jpg files while ignoring any jpg files already in the folder on Windows Powershell, question is though would it actually work?

Here's the code:

Code:
@echo off

rem Set the folder where the images are stored
set "E:\Images\NEneNe"

rem Loop through all files in the folder
for /f "delims=" %%i in ('dir /b "%folder%\*.gif" "%folder%\*.png"') do (
  rem Generate the .jpg file name
  set "jpg_file_name=%%~ni.jpg"

  rem Check if a .jpg file with the same name already exists in the folder
  if not exist "%folder%\%jpg_file_name%" (
    rem Convert the image to .jpg and save it in the folder
    magick convert "%%i" -colorspace RGB "%folder%\%jpg_file_name%"

    rem Check if there are duplicate files
    if exist "%folder%\%jpg_file_name%" (
      rem Add a -1 to the file name
      set /a cnt=1
      set "jpg_file_name=%%~ni-%cnt%.jpg"

      rem Check if there are still duplicate files
      if exist "%folder%\%jpg_file_name%" (
        rem Add 1 to the -1 in the file name
        set /a cnt=cnt+1
        set "jpg_file_name=%%~ni-%cnt%.jpg"
      )

      rem Convert the image to .jpg and save it in the folder
      magick convert "%%i" -colorspace RGB "%folder%\%jpg_file_name%"
    )
  )
)

@Ral
 
Last edited:
  • Like
Reactions: Ral

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
I heard ChatGPT could write code so I made it create a script that can find any png and gif files in a folder and convert those into jpg files while ignoring any jpg files already in the folder on Windows Powershell, question is though would it actually work?

Here's the code:

Code:
@echo off

rem Set the folder where the images are stored
set "E:\Images\NEneNe"

rem Loop through all files in the folder
for /f "delims=" %%i in ('dir /b "%folder%\*.gif" "%folder%\*.png"') do (
  rem Generate the .jpg file name
  set "jpg_file_name=%%~ni.jpg"

  rem Check if a .jpg file with the same name already exists in the folder
  if not exist "%folder%\%jpg_file_name%" (
    rem Convert the image to .jpg and save it in the folder
    magick convert "%%i" -colorspace RGB "%folder%\%jpg_file_name%"

    rem Check if there are duplicate files
    if exist "%folder%\%jpg_file_name%" (
      rem Add a -1 to the file name
      set /a cnt=1
      set "jpg_file_name=%%~ni-%cnt%.jpg"

      rem Check if there are still duplicate files
      if exist "%folder%\%jpg_file_name%" (
        rem Add 1 to the -1 in the file name
        set /a cnt=cnt+1
        set "jpg_file_name=%%~ni-%cnt%.jpg"
      )

      rem Convert the image to .jpg and save it in the folder
      magick convert "%%i" -colorspace RGB "%folder%\%jpg_file_name%"
    )
  )
)

@Ral
Only one way to find out :tupac
 

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
Had to troll a bit, sorry, forceful habit :naruramen

Judging by that syntax, that looks more like batch script if anything.

I'd be careful running that code on your machine, although it doesn't look like it would work even if you ran it in its current state. It also does not tell you that it requires another program to handle the conversion, which is ImageMagick in this case (https://imagemagick.org/)
 

Cryso Agori

V.I.P. Member
I see, so then do you know the actual way to write a script preferably on Windows Powershell or CMD Prompt, that can look through a folder and convert image files to jpg, while ignoring jpg files already in the folder? And adding a postfix to any duplicate file names so that the files doesn't get overwritten?

Because I've been trying to use local apps and online converters but they either run out of memory or crash.
 
  • Wow
Reactions: Ral

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
I see, so then do you know the actual way to write a script preferably on Windows Powershell or CMD Prompt, that can look through a folder and convert image files to jpg, while ignoring jpg files already in the folder? And adding a postfix to any duplicate file names so that the files doesn't get overwritten?

Because I've been trying to use local apps and online converters but they either run out of memory or crash.
I can muster something up.

1. How many files are you trying to convert at the moment?
2. Are they sorted by folders?
3. Do you want it to be recursive so that it finds, and converts said files within subdirectories of the parent folder?

You'll likely want an image converter like ImageMagick or I'm sure there are other image converter programs that we can use for this.
 

Cryso Agori

V.I.P. Member
I can muster something up.

1. How many files are you trying to convert at the moment?
Um. a lot.

2. Are they sorted by folders?
No, no subdirectories.

3. Do you want it to be recursive so that it finds, and coverts said files
Yes

within subdirectories of the parent folder?
No.

You'll likely want an image converter like ImageMagick or I'm sure there are other image converter programs that we can use for this.
If it's free sure.

Also sorry for asking for more, but is it possible to make it so the script can find .bin files and delete them? Cause there are a bunch in my folder cause downloading the files went wrong I think.
 
  • Like
Reactions: Ral

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
Um. a lot.


No, no subdirectories.


Yes


No.


If it's free sure.

Also sorry for asking for more, but is it possible to make it so the script can find .bin files and delete them? Cause there are a bunch in my folder cause downloading the files went wrong I think.
Gotchu.

It's fairly easy to work with dummy files as if you're searching by file extension, you can enable Windows to show you file extensions in the folder settings. From there you can pretty much create text files, change the extension to .bin, .gif, etc, and that's how you will go about testing if the script meets your needs.
 

Cryso Agori

V.I.P. Member
Gotchu.

It's fairly easy to work with dummy files as if you're searching by file extension, you can enable Windows to show you file extensions in the folder settings. From there you can pretty much create text files, change the extension to .bin, .gif, etc, and that's how you will go about testing if the script meets your needs.
What if an image has "bin" in its name? Not .bin as an extension. Would it think that as a bin file and delete it? Or would it just be looking through extensions?
 

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
What if an image has "bin" in its name? Not .bin as an extension. Would it think that as a bin file and delete it? Or would it just be looking through extensions?
Through extensions, as you'll most likely use a wildcard such as "*" before the extension as the example provided shows in the beginning of the for loop. I'll have some code for you by the end of the week if not sooner and if you need a breakdown of how I came to write it, let me know.

I think Python would be a good use-case for this too, but if you want Batch or PowerShell, those just require the ImageMagick exe to work.
 

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
I have not forgotten about you, my friend.

Life events are totaling on my free time, although, I have gotten to the point where it does the conversions, but the duplicate file part is a bit complicated. It's doable, but the order of operations matters in this case, and I will find a way around this challenge if it destroys and regenerates a few braincells along the way. Thank you for introducing such a challenge comrade, it will be defeated soon :catbuff
 

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
@Thegoldenboy2188 unfortunately, I have been giving it my all at work recently to end the year with a BANG and hopefully get a nice raise/promotion in January. The fruits of my labor include a simple project I created that took hours of research and using a new framework I have recently been learning about. You can check out the project here: https://github.com/Ralkage/api-talk-back

It behaves similarly to Postman-Echo but dumbed down.

I will be able to focus on this request and my other non-work-related projects beginning January, but I know you are a patient man, so I wanted to keep you in the loop as well as I didn't want you to think I forgot about you :cattrin
 

Ral

[SUBTRACTED]
Administrator
Pronouns
He/Him
I forgot to mention that I completed this request for @Thegoldenboy2188 a while back lol we continued our conversation privately and I was eventually able to get it done for him.
 
Back
Top