#1 08 May 2019 19:39

kteague
Member
Registered: 07 May 2019
Posts: 13

process files ending with a specific character differently

I have a FOR loop running against a set of PDF files in a single directory.  The task is to annotate each PDF file using PDFtk, but there are certain files that I need to process slightly different than the others.  The filenames are (almost) standardized and look like this:

ABC1234W10.SO9876.pdf

ABC represents the customer abbreviation and *should* always be 3 characters, but I've seen mistakes happen where it's only AB (2 characters), but this isn't that big of a deal as the part of the filename I need to key off of is at the end (see below).
1234 represents the site number which will be different from one file to the next.  It should, in most cases, be 4 digits long, but some store numbers are 5 digits long.
W10 represents the project.  This is static and always the same between all file names.
. is a delimiter that seprates the store data from the sales order data.
SO9876 is the sales order number.  The SO is static, the 9876 will change from one file to the next.

This is where things get tricky.  At times, we'll get a revised copy of the PDF file, and the resulting filename will be in one of two forms, depending on who's creating it.  Using the same filename example above, we come up with:

ABC1234W10.SO9876R.pdf
... or, more commonly...
ABC1234W10.SO9876.R.pdf

I need to annotate these the same as the others except I also need to add a "REVISED %DATE% %TIME%" stamp in the PDF as well.  Creating the revision data isn't a problem, nor is annotating the PDF files using PDFtk.  The problem I'm having is how can I differentiate these files based on their filename so that I can process the *.R.pdf and *R.pdf files with additional commands to add the REVISED annotation.  Also, in the end, the printouts (using PDFtoPrinter.exe) need to be in order based on filename (which it currently does based on the FOR loop I'm using).

Here's my code, so far:

@echo off

REM vars to define where my stamps are located along with their filename
set SOORIG=%USERPROFILE%\Documents\Adobe\ORIGINAL-SO.pdf
set SOCOPY=%USERPROFILE%\Documents\Adobe\COPY-SO.pdf
set PLORIG=%USERPROFILE%\Documents\Adobe\ORIGINAL-PL.pdf

if not exist processed md processed

for %%I in (*.pdf) do (
	pdftk.exe %%I multistamp %SOORIG% output processed\%%~nI-ORIG.pdf
	PDFtoPrinter.exe %%~dI\%%~pI\processed\%%~nI-ORIG.pdf
	pdftk.exe %%I multistamp %SOCOPY% output processed\%%~nI-COPY.pdf
	PDFtoPrinter.exe %%~dI\%%~pI\processed\%%~nI-COPY.pdf )

Offline

#2 08 May 2019 21:28

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

Forgive me if I'm missing something, but I don't see a way to edit my original post to change my code.  I've modified it a tad to process the print jobs; all originals first, all copies last, but still in order of filename.

@echo off

set SOORIG=%USERPROFILE%\Documents\Adobe\ORIGINAL-SO.pdf
set SOCOPY=%USERPROFILE%\Documents\Adobe\COPY-SO.pdf

echo Checking for directories -- creating them if not found ...
if not exist processed md processed
if not exist temp md temp
goto :chkdir

:chkdir
echo Checking for PDF files to process ...
if not exist *.pdf goto :notfound

echo.
echo Annotating ORIGINAL and COPY to PDF files ...
for %%I in (*.pdf) do (
	pdftk.exe %%I multistamp %SOORIG% output temp\%%~nI-ORIG.pdf
	pdftk.exe %%I multistamp %SOCOPY% output temp\%%~nI-COPY.pdf 
	echo DONE processing annotations! 
	echo Moving original PDF files to the processed\ directory ...
	move %%I processed\ )
	goto :aoriginals

:aoriginals
echo.
echo Processing annotated originals ...
REM  Print and delete annotated originals after processing.
for %%O in (temp\*-ORIG.pdf) do (
	echo Printing %%O ...
	PDFtoPrinter.exe %%~fO
	echo Deleting annotated original ...
	del %%O
	echo. )
	goto :acopies

:acopies
echo.
echo Processing annotated copies ...
REM  Print and delete annotated copies after processing.
for %%C in (temp\*-COPY.pdf) do (
	echo Printing %%C ...
	PDFtoPrinter.exe %%~fC
	echo Deleting annotated copy ...
	del %%C
	echo. )
	goto :done

:notfound
echo.
echo No PDF files found to process ... exiting!
goto :done

:DONE
echo.
echo DONE!

Offline

#3 08 May 2019 23:30

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013
Website

Re: process files ending with a specific character differently

You should be able to use the syntax on this page to replace the extra periods with a different character or an empty string to delete them.

https://ss64.com/nt/syntax-replace.html

Offline

#4 09 May 2019 22:38

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

Simon Sheppard wrote:

You should be able to use the syntax on this page to replace the extra periods with a different character or an empty string to delete them.

https://ss64.com/nt/syntax-replace.html

Thanks for chiming in.  However, I'm not looking to replace or delete any characters.  That is some good info, by the way, and I learned something new from it. 

What I'm looking to do is add some additional annotation data to PDF files ending with "R"; for example, somefile.r.pdf, or somefiler.pdf.  Going back to my other post, I learned something new that appears to be what I'm looking for... and came up with this:

First, I created two files, one named newfile.pdf and the other named newfile.r.pdf.  Then I punched in this for loop:

for %G in (*.pdf) do ( set fname=%~nG & echo %fname:~-2,1% )

... and this is what I got in return:

C:\Temp>(set fname=newfile   & echo r )
r

C:\Temp>(set fname=newfile.r   & echo r  )
r

So, for some reason, it's catching both and echoing R for both which it should only echo r for newfile.r.  Confusing...

Offline

#5 10 May 2019 19:37

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013
Website

Re: process files ending with a specific character differently

To set variables within a for loop you need to use either delayed expansion or a subroutine.
https://ss64.com/nt/delayedexpansion.html

Offline

#6 13 May 2019 09:47

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: process files ending with a specific character differently

What Simon said is correct.

However, if your file name is broken up with dots like in your example, you can leverage this to your advantage by nesting another FOR loop:

for %G in (*.pdf) do for %H in ("%~nG") (
  echo Original file:     %G
  echo Extension removed: %H
  echo Annotation:        %~xH
)

cmd | *sh | Ruby | Chef

Offline

#7 13 May 2019 22:13

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

bluesxman wrote:

What Simon said is correct.

However, if your file name is broken up with dots like in your example, you can leverage this to your advantage by nesting another FOR loop:

for %G in (*.pdf) do for %H in ("%~nG") (
  echo Original file:     %G
  echo Extension removed: %H
  echo Annotation:        %~xH
)

That, kind sirs, works like a charm.  By the way, you're missing a "do" in your nested for loop.  Here's the finalized for loop I came up with, based on your fine example, and adding SETLOCAL EnableDelayedExpansion as Simon indicated:

.....

echo.
echo Annotating ORIGINAL and COPY to PDF files ...
for %%G in (*.pdf) do for %%H in ("%%~nG") do (
	if %%~xH==.R (
		pdftk.exe %%G multistamp %SOORIG% output temp\%%~nG-ORIGT.pdf
		pdftk.exe %%G multistamp %SOCOPY% output temp\%%~nG-COPYT.pdf
		pdftk.exe temp\%%~nG-ORIGT.pdf multistamp %REVISED% output temp\%%~nG-ORIG.pdf
		pdftk.exe temp\%%~nG-COPYT.pdf multistamp %REVISED% output temp\%%~nG-COPY.pdf
		del temp\%%~nG-ORIGT.pdf temp\%%~nG-COPYT.pdf
	) else (
		pdftk.exe %%G multistamp %SOORIG% output temp\%%~nG-ORIG.pdf
		pdftk.exe %%G multistamp %SOCOPY% output temp\%%~nG-COPY.pdf
	)
	echo DONE processing annotations! 
	echo Moving original PDF files to the processed\ directory ...
	move %%G processed\
)
goto :aoriginals

.....

One last question.  This last snippet of code appears to be case sensitive.  Is there a way to make it case insensitive or evaluate it for both R or r?

if %%~xH==.R

Thank you very much!

Last edited by kteague (13 May 2019 22:15)

Offline

#8 14 May 2019 08:14

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: process files ending with a specific character differently

Glad it's working.

Missing "do" -- oops!

Use if /i to ignore case.
Ref: https://ss64.com/nt/if.html

FYI -- In the code posted you don't have anything that needs delayed expansion.


cmd | *sh | Ruby | Chef

Offline

#9 15 May 2019 18:00

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

bluesxman wrote:

Use if /i to ignore case.
Ref: https://ss64.com/nt/if.html

Nice!  Modified in my script.

bluesxman wrote:

FYI -- In the code posted you don't have anything that needs delayed expansion.

I thought delayed expansion was required to set variables within a for loop.

Last edited by kteague (15 May 2019 18:45)

Offline

#10 17 May 2019 09:08

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: process files ending with a specific character differently

kteague wrote:

I thought delayed expansion was required to set variables within a for loop.

Ah, no -- you can set them all you like, it's when you retrieve (expand) them that you may encounter problems that need "enabledelayedexpansion" or related work arounds.  How I'd solve the expansion issue depends on the task at hand.

In your final script version above you aren't even working with vars inside the loop -- you are working purely with "for" tokens, which are not afflicted with the expansion problems specific to vars.

Last edited by bluesxman (17 May 2019 09:12)


cmd | *sh | Ruby | Chef

Offline

#11 17 May 2019 19:16

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

bluesxman wrote:
kteague wrote:

I thought delayed expansion was required to set variables within a for loop.

Ah, no -- you can set them all you like, it's when you retrieve (expand) them that you may encounter problems that need "enabledelayedexpansion" or related work arounds.  How I'd solve the expansion issue depends on the task at hand.

In your final script version above you aren't even working with vars inside the loop -- you are working purely with "for" tokens, which are not afflicted with the expansion problems specific to vars.

Interesting.  Let me make sure I'm understanding this correctly.

for %I in ...

%I isn't truly a variable?  It's a token?

But if I were to do, say,

for %I in (foo) do (
  set _newvar=buggers
  echo %_newvar% and %%I

I'd need EnableDelayedExpansion in this case, as I'm setting and calling on a variable set within the for loop?

Offline

#12 20 May 2019 08:14

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: process files ending with a specific character differently

Right ... a variable is denoted by surrounding percent symbols, like %name%, whereas "for" tokens are like %%t.  Like command line params (eg "%1") these are generally expecting to be file paths and the formatting options available reflect that; that said, you can use them for other purposes.

As for your code, with a minor change...

kteague wrote:
set _newvar=outside
for %%I in (foo) do ( REM I fixed a missing % on this line
  set _newvar=buggers
  echo %_newvar% and %%I

Above code would respond:

outside and foo

To make it say:

buggers and foo

You would need to do soemthing like:

setlocal enabledelayeexpansion
set _newvar=outside
for %%I in (foo) do (
  set _newvar=buggers
  echo:!_newvar! and %%I

You need to use the ! notation to utilise delayed expansion (variables using % are treated as per normal).

Digressing a little -- an alternative, without delayed expansion:

set _newvar=outside
for %%I in (foo) do (
  set _newvar=buggers
  call echo:%%_newvar%% and %%I

%% is the CMD way of escaping % symbols; when you use %% in for tokens in scripts you actually are using this escaping.  Similarly, when you're fudging a variable expansion using "call" you need to escape the % on the variable.

Last edited by bluesxman (20 May 2019 10:57)


cmd | *sh | Ruby | Chef

Offline

#13 31 May 2019 21:37

kteague
Member
Registered: 07 May 2019
Posts: 13

Re: process files ending with a specific character differently

Thank you for the thorough explanation.

Offline

Board footer

Powered by FluxBB