Need help batch processing a command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM2
Posts: 6
Joined: 23 Sep 2013 02:48

Need help batch processing a command

#1 Post by OM2 » 23 Sep 2013 02:55

I have this command:

Code: Select all

convert -brightness-contrast 15x15 abc.jpg abc.jpg

This changes the image abc.jpg and replaces it with the same image

1. I want to go through a directory and apply the change to all files that are .jpg

2. I want to recursively go through sub directories and do the same

I'm not sure where to begin with the coding and how to store the matching names in an array and then execute the command for all names in the array

I'm guessing that the above has been done many times before :)

Any help would be greatly appreciated

Thanks


OM

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help batch processing a command

#2 Post by foxidrive » 23 Sep 2013 03:25

Check out the help at the command line with this command:

FOR /?

For example this will provide a list of the JPG files in a folder tree.

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do echo "%%a"

OM2
Posts: 6
Joined: 23 Sep 2013 02:48

Re: Need help batch processing a command

#3 Post by OM2 » 23 Sep 2013 12:04

awesome!
i think i have a working script now
let me know if there's anything to improve upon:

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do convert -brightness-contrast 15x15 %%a %%a


"delims=" - what's this for?
the definition doesn't make sense to me: "delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab."

why is the a space before dir in ' dir *.jpg /s /b /a-d '

why %%? i understand that u use for batch - but why would someone need to do for a single file using just %?

also... if i didn't want the dos box to disappear after execution finished, what do i do?
i don't get to see any errors if they are shown

thanks!

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need help batch processing a command

#4 Post by Aacini » 23 Sep 2013 12:28

Perhaps you should use this simpler solution:

Code: Select all

@echo off
for /R %%a in (*.jpg) do convert -brightness-contrast 15x15 "%%a" "%%a"

FOR command processes several files, the ones selected by the wild-card (set) placed inside parentheses. /R switch indicate to recursively go through sub directories and do the same. The quotes around %%a are required if the path to any file have spaces.

FOR command use a percent-letter to indicate the replaceable parameter where it will replace the name of each file, so you should use the same percent-letter in the command; however, the percent character is used for other purposes in a Batch file, so you must use a double percent in FOR commands placed inside a Batch file (and just one if you use the FOR directly in the command line).

You may open a command-line DOS session and then execute the Batch file; this way, the DOS window remains open until you type EXIT command.

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help batch processing a command

#5 Post by foxidrive » 23 Sep 2013 17:47

OM2 wrote:"delims=" - what's this for?
the definition doesn't make sense to me: "delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab."

It allows you to use long filename elements like spaces in the filenames and paths.
tokens=* strips leading whitespace so changing the delimiters to none is the preferred method.
why is the a space before dir in ' dir *.jpg /s /b /a-d '

Purely to make reading the code easier.
why %%? i understand that u use for batch - but why would someone need to do for a single file using just %?

I don't understand your question. %% is used in a batch file and I proposed a batch file.
also... if i didn't want the dos box to disappear after execution finished, what do i do?
i don't get to see any errors if they are shown

Add pause as the last line.

OM2
Posts: 6
Joined: 23 Sep 2013 02:48

Re: Need help batch processing a command

#6 Post by OM2 » 23 Sep 2013 19:20

guys, thanks for both replies

@Aacini: big thanks for the quotes mention...
i just wasted 60 min trying to figure out why my script which previously worked didn't now work
in the end i figred out it was to do with having spaces in the path
but then i was scratching my head think how and where to put quotes

question: how do i execute 2 commands?
do i just put on separate lines or something?
do i need to put semi colons at the end of lines?

i have these 2 bits of code now:

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ') do convert -brightness-contrast 35x35 %%a %%a


Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do composite -gravity SouthEast logo2.png %%a %%a


i want to execute in one go

thanks
(i've missed out using the quotes above in the paths)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help batch processing a command

#7 Post by foxidrive » 23 Sep 2013 20:41

In the command use quotes as shown here:

Code: Select all

composite -gravity SouthEast logo2.png "%%a" "%%a"


Note that it's not usual to have the same name for source and target file - but some programs may handle that internally.


This would add -newfile to the filename.

Code: Select all

composite -gravity SouthEast logo2.png "%%a" "%%~na-newfile%%~xa"

OM2
Posts: 6
Joined: 23 Sep 2013 02:48

Re: Need help batch processing a command

#8 Post by OM2 » 24 Sep 2013 16:29

@foxidrive, thanks for that - really helpful - the commands i'm using, allow for using the same filename

if i did want to replace the same filename, how best would i do that?

i've tried combining both my scripts into one...
not having much luck

this is what i'm doing:

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do convert -brightness-contrast 15x15 "%%a" "%%a" composite -gravity SouthEast logo2.png "%%a" "%%a"

what am i doing wrong?

thanks

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need help batch processing a command

#9 Post by Aacini » 24 Sep 2013 16:46

In order to execute several commands in IF and FOR you must either: separate the commands with & character:

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do convert -brightness-contrast 15x15 "%%a" "%%a" & composite -gravity SouthEast logo2.png "%%a" "%%a"
... or open a code block in parentheses and place each command in its own line (this is a clearer way):

Code: Select all

@echo off
for /f "delims=" %%a in (' dir *.jpg /s /b /a-d ')  do (
   convert -brightness-contrast 15x15 "%%a" "%%a"
   composite -gravity SouthEast logo2.png "%%a" "%%a"
)

Antonio

OM2
Posts: 6
Joined: 23 Sep 2013 02:48

Re: Need help batch processing a command

#10 Post by OM2 » 25 Sep 2013 14:13

antonio: you are awesome!
thanks

Post Reply