Select Page

If you have a path and filename and you want to extract the filename from the path then you can use the following regex: %~NXf

e.g.

for %f in (c:\temp*.pdf) do copy %f c:\pdffiles\%~nxf

This will append only the filename to the final path. This is a bit of a contrived example however the next is what I wnated to do that got me looking into how to do this.

Using ImageMagick I wanted to composite and tile a watermark image into every image in the originals folder and save it to the watermarked folder. To do this I did the following;

for %f in (art\ink\original*.gif) do composite -dissolve 15 -tile web_gradient_transparent.gif “%f” “art\ink\watermarked\%~nxf”

One line dozens of images watermarked into another location.

In fact here is the entire script I use to batch process all images with a watermark.

NB When using FOR in a command file it is necesary to use double percent (%%f) to refer to variables rather than a single as per use on the command line.

@echo off
cls

set SCRIPT_DIR=c:\.dev\alisonmcconnachie.com\production\scripts

set ADDR=alisonmcconnachie.com
set IMG_HEIGHT=100
set IMG_WIDTH=800
set TXT_SIZE=72
set TXT_FONT=Arial
set BASE_DIR=c:\.dev\alisonmcconnachie.com\images
set SRC_DIR=%BASE_DIR%\art\ink\original
set DEST_DIR=%BASE_DIR%\art\ink\watermarked

cd %SRC_DIR%

rem create the initial watermark
echo Creating watermark
convert -size %IMG_WIDTH%x%IMG_HEIGHT% xc:white -font %TXT_FONT% -pointsize %TXT_SIZE% -tile gradient: -annotate +28+68 %ADDR% mark_gradient.gif

rem make the white in the image transparent
convert -transparent white mark_gradient.gif mark_gradient_transparent.gif

rem watermark all the image files in SRC folder and output them to DEST folder
echo Watermarking images
for %%f in (*.jpg, *.gif, *.tif) do (
  echo ...processing %%~nxf...
  composite -dissolve 15 -tile mark_gradient_transparent.gif "%%f" "%DEST_DIR%%%~nxf"
)

del mark_gradient.gif
del mark_gradient_transparent.gif

rem Convert all non gif images to gif
echo Converting all images to GIF
cd %DEST_DIR%
for %%l in (*.jpg, *.tif) do (
  echo ...Converting %%~nxl...
  convert "%%l" -quality 9 "%%~nl.gif"
)

rem Tidy Up
echo Tidy up
for %%l in (*.jpg, *.tif) do del "%%l"
del mark_gradient.gif
del mark_gradient_transparent.gif

echo done
cd %SCRIPT_DIR%
%d bloggers like this: