Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, xnview

Post Reply
c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Wed Oct 31, 2018 4:50 pm

Say there are many different files each with the same file name @Profile.png but there are a thousand of them inside a thousand different subfolders. How can all @Profile.png files be converted to @Profile.jpg with output being exact same location as each individual @Profile.png file?

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Thu Nov 01, 2018 1:09 pm

c627627 wrote:
Wed Oct 31, 2018 4:50 pm
Say there are many different files each with the same file name @Profile.png but there are a thousand of them inside a thousand different subfolders. How can all @Profile.png files be converted to @Profile.jpg with output being exact same location as each individual @Profile.png file?
If it is possible for three files in three folders, I'm not sure without trying it, then it should be possible for thousands...

Could you please create a minimal test setup of a parent folder containing three folders, each containing one file, and upload in a ZIP.

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Thu Nov 01, 2018 1:47 pm

Absolutely. Attached please find a test folder tree with three files in random subdirectories that are all named @Profile.png but they are all different files.

The goal is to get @Profile.jpg files in the same locations.

Here is the attachment:
Attachments
TestnConv.zip
(208.63 KiB) Downloaded 40 times

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Thu Nov 01, 2018 8:16 pm

Good news and bad news:

The TestnConv folder of test files that you posted contains source PNG files at different folder levels, which I hadn't anticipated from your first post. After evidently only taking a quick look at the first folder and finding what I expected, I was well into the usual largely trial and error process of trying to come up with a working solution before I noticed, but happily I hadn't in fact wasted much time!

I established early on, or possibly rediscovered, that NConvert doesn't seem to accept input with a wildcard in the path as well as in the filename, C:X/*/*.png for example. The solution, or at least one solution, is to use a For / D loop, and after the usual experimentation I managed to come up with a solution that seems to work for the situation I had envisaged. For what it's worth:

Code: Select all

For /D %%Y In ("Files\*")  Do ( 

nconvert -out jpeg "%%Y\*.png"

)
That code run as a batch file placed the required JPEG in each first-level folder in my test files; if you wished to deleted the source PNG file after it has been read that should (or possibly may not in the For loop) be possible using the NConvert -D option.

I have attached my test folder of files conforming to what I had expected from your first post.

If you have some command line experience possibly you can find a way to elaborate that code for your more general case,
otherwise if it is important there are a number of coding sites where someone may be able to help.
Attachments
Files.zip
(145.83 KiB) Downloaded 34 times

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Thu Nov 01, 2018 11:59 pm

Just wondering why jpeg and not jpg in your code?
So this is just a matter of command code knowledge? I always wondered what the best forum is for that? Maybe I can start a thread on some forum that does that, it would be nice to know a code that can do that.

Back to the good part, is this correct: if there are 100 folders with @Profile.png inside the first level, the code would be:

For /D %%Y In ("Files\*") Do (

xnconvert.exe -out jpeg "%%Y\@Profile.png"

)

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Fri Nov 02, 2018 2:22 am

I have some knowledge of old DOS commands, but could you please copy/paste your modified code that would start program location
E:\XnConvert\xnconvert.exe

to convert all @Profile.png files inside all folders located in
E:\1

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Fri Nov 02, 2018 9:20 am

You posted under the NConvert forum heading: NConvert is a command line utility unrelated to the GUI program XnConvert.
c627627 wrote: Just wondering why jpeg and not jpg in your code?
That is the syntax specified in the NConvert help file when the -out option, for example, is used. If you have some command line experience, you could download NConvert and create a copy of the help file, if you might be interested in an NConvert solution.

The code I posted above when run in a batch file .bat should process the PNG files in any number of first level folders, while ignoring any other files in the folders such as the text files in the test folder example you uploaded. You may need to edit the code to use full paths (enclosed in double quotes if there are any spaces in the paths), rather than the relative addressing I used, depending on how you test.

Regarding XnConvert, it should be possible to configure it to process a folder containing multiple first level folders; you could test whether it will also handle more complex folder structures.

I'm not familiar with command line options for XnConvert, to the extent that they currently exist: if you wish to use XnConvert you could start a new thread under that forum heading.

Minor edit:

My code above will convert all PNG files in each first level folder to JPEG, if there could be other PNGs in a folder replace
"%%Y\*.png" by "%%Y\@Profile.png" to limit conversion to the files you specified.

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Fri Nov 02, 2018 1:54 pm

If you can forgive my basic questions but I am using Windows:
Where do I download the command version from, where do I place it and where do I place the folder filled with folders which have @Profile.png in their first level, before double clicking on the .bat file with your code?

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Fri Nov 02, 2018 2:57 pm

c627627 wrote:
Fri Nov 02, 2018 1:54 pm
Where do I download the command version from...
It can be downloaded from the link provided in the NConvert section of the forum; it can also be downloaded from this link from the xnview.com homepage which also provides some additional information about NConvert.

... where do I place it and where do I place the folder filled with folders which have @Profile.png in their first level, before double clicking on the .bat file with your code?
If you have little or no previous command line experience I can tell from my own experience that it can be a steep learning curve, and unforgiving when a problem arises as very often happens: as a long-term Windows user with minimal DOS experience, I never expected to use the command line until I discovered NConvert and realised that it could be useful for a project I had at the time.

To give a quick answer to your question, I would suggest in the first instance you reproduce my test setup exactly, although in some respects it may be an unusual way of working.

o Create a new folder somewhere, the location shouldn't matter.

o Place in the folder a copy of nconvert.exe copied from the download.

o Place in the folder a copy of my folder Files copied from the above ZIP.

o Copy my code above into a text file and save the file with a name such as script and a .bat extension, you can enter the extension in Notepad in place of a text extension. Then place the file in the folder with the other files. [Note: I have just edited the above code to make it suitable for your test, changing the name of nconvert from nconvert_7.20.exe to nconvert.exe]

o Then double-click the batch file and if it runs check if the JPEG files have been created as expected; if the script doesn't run it may be a Windows configuration issue.

Note that I've written this in a hurry as I have to go out, if you hit a problem you can't overcome don't be too put out!

It would be worth your taking a look at the posts in the NConvert section of the forum, particularly 'How to use NConvert', although it necessarily can't explain everything.

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Fri Nov 02, 2018 8:51 pm

It worked. Thank you cday.

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Fri Nov 02, 2018 10:40 pm

c627627 wrote:
Fri Nov 02, 2018 8:51 pm
It worked. Thank you cday.

With a little time to spare I've just done some research which may be useful to you if you wish to continue the command line approach, either now or if you feel motivated to continue later.

First I made a quick Google search using 'batch file to process files recursively' to illustrate how general coding forums can help, and identify a couple I recognise from previous searches. I'm posting the following links to potentially useful forums which displayed results that may have some relevance to your need: don't ask me to provide support on the answers as in general I can't!

In Windows, a batch file with a recursive for loop and a file name including blanks

Windows Batch File Looping Through Directories to Process Files?

It looks as if the general case presented by the test folder you uploaded may possibly be solved fairly easily using a For /R loop, which I've never had occasion to use myself, possibly simply with minor editing of my code above. The following link is one of many resources that describe its use:

Iterating with "For"

If you've decided that the command line isn't for you I entirely sympathise, but you may possibly find later that you wish to have another look and the information in this post may be helpful then.

cday
XnThusiast
Posts: 2188
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by cday » Sun Nov 04, 2018 8:56 am

I believe I now have a full solution to your original problem using For /R which processes files recursively:

Code: Select all

For /R "TestnConv" %%Y In (\"@Profile.png")  Do ( 

nconvert -out jpeg -q 80 "%%Y"

)
If you save the above code as a batch file .bat it should process all the PNG files in the folder TestnConv you uploaded above in the way that you requested.

I have added a -q term to the code to set the output JPEG compression level, quality 80 in the above code.

If you wish to delete the source PNGs after creating the JPEGs, you haven't indicated that, insert the option -D before the -out term, correct operation confirmed now.

When developing batch files it can be useful to insert Pause at the end of the code, which keeps the terminal window open after the code is run; close inspection of the output can sometimes provide useful clues when the code fails to run as expected.

Inspection of the terminal output when the above code is run in fact shows some error indications, seemingly because the recursive code fails to find any PNG files at certain folder levels, however as there are intentionally no files there that doesn't indicate a real problem.

Always test using copies of important files!

c627627
Posts: 50
Joined: Fri Jun 26, 2009 5:00 am

Re: Command line to convert all @Profile.png files inside many subfolders to @Profile.jpg

Post by c627627 » Sun Nov 04, 2018 9:06 pm

I am grateful for your post and hope that many others find it useful when search engines pick this up.
Thank you.

Post Reply