Old Script on (relatively) new computer

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Old Script on (relatively) new computer

#1 Post by dchall8 » 20 Nov 2019 14:29

I hope someone can find a simple, Bozo mistake I'm making with this file.

Back on 2012 I had gotten some DOS help on another forum which is now inactive. I'm using a 2014 era, Win 7 Home Premium, 64-bit, computer. I have been using the batch file on a weekly basis since 2012 on various computers, and have converted well over 100,000 documents. But every time I set it up on a new computer, the computer needs some tweaks. It has been, now, 5 years since I tweaked a computer, and I'm having trouble tweaking my own :oops:

The purpose of the script is to take a list of image files with odd extensions, concatenate them by file name and page number, and produce a PDF file for each collection of images into a properly named file. Source file names look like this, 00254567.001, 00254567.002, etc. for the document numbered 00254567 going page by page by extension. The destination file name looks like this, 00254567.pdf. The next document would be 00254568.001 etc. The image files seem to be in the TIFF format.

To prepare to run this on my computer I have
  • downloaded and installed the GnuWin32 install package titled tiff-3.8.2-1.exe.
  • taken the folders found in the GnuWin folder and dropped them all into c:\bin so this script will pick them up,
  • created c:\test\src and c:\test\dest folders, and
  • seeded c:\test\src with a 5-page document and a 6-page document to run the test.
The batch file calls TIFFCP.exe and TIFF2PDF.exe from the GnuWin package. Here is the script...

Code: Select all

@echo off & setlocal EnableExtensions ENableDelayedExpansion
set oldpath=%PATH%
set PATH=%PATH%;c:\bin;

::Following sets source and destination folders, 
::normally the source would be on the CD (d:\folder name)
::normally destination would be a server
set SRC=c:\test\src
set DST=C:\test\dest

:: This turns on ECHO for debugging.  
set DBG=ECHO/

::Following turns off the ECHO function
::set "DBG="

::Following points the batch at the source directory
pushd %SRC%

::Following finds files with leading zeros and extension .001
::Then it strips off the leading zeros
::Strip zeros in directory /B simplifies file attributes
::/A-D shows all files but not directories
::-D removes directory names from file list
::/ONE sorts list by name and then file extension
::Two % signs are used because this is a batch file.  

for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (

::Following sets PG (page number?) to null
set "PG="

::Following collects image files with the 
::same name and different numbered extensions
::

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do 

::Set PG to contain multiple file names, not just one
set PG=!PG! %%~nxB

::Following converts collected files into one .tif file
tiffcp -c lzw !PG! %DST%%%~nA.TIF

::Following changes the .tiff file into a PDF 
::inside the destination folder
tiff2pdf -o %DST%%%~nA.PDF %DST%%%~nA.TIF

::Following deletes the single TIF file
DEL %DST%%%~nA.TIF
)
POPD
set PATH=%oldpath%
I hope I have that commented properly, but I could be wrong at any point. I don't really speak DOS.

Again, I have gotten this running on many computers since 2012. What I don't remember is whether I have had to tweak the batch file to make it run. I do have to change the source and destination folders for each computer and setup, but that's straightforward.

When I run the batch I see the CMD window open and immediately close. In the past I have solved this by making sure all the converter executables are inside the c:\bin folder. Unless I missed something, that is done on this computer, and I have rebooted. Normally what I see when I run the batch is the CMD window opens and shows progress in the form of a document by document ECHO of the progress interspersed with a TIFF conversion error message for every single document. I have ignored the error messages because the PDF documents come through the process just fine.

What am I missing?

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Old Script on (relatively) new computer

#2 Post by Eureka! » 20 Nov 2019 15:45

dchall8 wrote:
20 Nov 2019 14:29
What am I missing?
It looks like a couple of () for the second FOR command (and " " around filenames in case the include spaces or special characters.

Try it by:
  • start CMD.exe
  • CD /d c:\test\src
  • Execute your script
That way the CMD window will show you any possible erroirs.


New code:

Code: Select all

@echo off & setlocal EnableExtensions ENableDelayedExpansion
	rem not needed because of setlocal: set oldpath=%PATH%


	
::-------------------------------------------------------
::			SETTINGS
::-------------------------------------------------------

::	Following sets source and destination folders, 
::	normally the source would be on the CD (d:\folder name)
::	normally destination would be a server
	set SRC=c:\test\src
	set DST=C:\test\dest

::	This turns on ECHO for debugging.  
	set DBG=ECHO/

::	Following turns off the ECHO function
::	set "DBG="

::	Add c:\bin to path
	set PATH=%PATH%;c:\bin;

::-------------------------------------------------------
::			INITIALIZATION
::-------------------------------------------------------

::	Following points the batch at the source directory
	pushd %SRC%

::	Following finds files with leading zeros and extension .001
::	Then it strips off the leading zeros
::	Strip zeros in directory /B simplifies file attributes
::	/A-D shows all files but not directories
::	-D removes directory names from file list
::	/ONE sorts list by name and then file extension
::	Two % signs are used because this is a batch file.  



::-------------------------------------------------------
::			ACTION!
::-------------------------------------------------------

	for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (

	 ::	Following sets PG (page number?) to null
		set "PG="

	 ::	Following collects image files with the 
	 :: same name and different numbered extensions

		for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
			set PG=!PG! %%~nxB
		)

	 ::  Following converts collected files into one .tif file
		tiffcp -c lzw !PG! "%DST%\%%~nA.TIF"
			
	 ::	Following changes the .tiff file into a PDF 
	 ::	inside the destination folder
		tiff2pdf -o "%DST%\%%~nA.PDF" "%DST%\%%~nA.TIF"

	 ::	Following deletes the single TIF file
		DEL "%DST%\%%~nA.TIF"
	)

	rem not needed because of setlocal: POPD
	rem not needed because of setlocal: set PATH=%oldpath%
Last edited by Eureka! on 21 Nov 2019 12:28, edited 2 times in total.

dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Re: Old Script on (relatively) new computer

#3 Post by dchall8 » 20 Nov 2019 16:51

Thanks for taking a look. I entered CMD, changed directories to the source, and ran the batch from Windows. Nothing. I moved the batch file into the source directory to access it directly and renamed it a.bat. I checked the dir of the source and a.bat is there with a size of 515. I also am using a version with no comments. I can always add comments back later once it's running.

Here's the new file with quotes around the source and dest file names. From within the source directory I typed "a" (no quotes) into CMD and got a syntax error, "The syntax of the command is incorrect. I typed a.bat and got the same error.

Code: Select all

@echo off & setlocal EnableExtensions ENableDelayedExpansion
set oldpath=%PATH%
set PATH=%PATH%;c:\bin;
set SRC="c:\test\src\"
set DST="C:\test\dest\"
set DBG=ECHO/
pushd %SRC%
for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (
set "PG="
for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do 
set PG=!PG! %%~nxB
tiffcp -c lzw !PG! %DST%%%~nA.TIF
tiff2pdf -o %DST%%%~nA.PDF %DST%%%~nA.TIF
DEL %DST%%%~nA.TIF
)
POPD
set PATH=%oldpath%
So it appears (to me) CMD is not attempting to run the file.

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Old Script on (relatively) new computer

#4 Post by Eureka! » 20 Nov 2019 17:26

1. You missed the "(" and ")" again in your a.bat
2. You need quotes around %DST%%%~nA.PDF and similar : "%DST%%%~nA.PDF", not around set SRC="c:\test\src\" and set DST="C:\test\dest\"

replace @echo off (first line of script) with @echo on to follow in detail at which step of the script the error code is generated.

dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Re: Old Script on (relatively) new computer

#5 Post by dchall8 » 20 Nov 2019 18:46

I think I have all your suggestions now, but did I get the parentheses right?

Code: Select all

@echo on & setlocal EnableExtensions ENableDelayedExpansion
set oldpath=%PATH%
set PATH=%PATH%;c:\bin;
set SRC=c:\test\src\
set DST=C:\test\dest\
set DBG=ECHO/
pushd %SRC%
for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (
set "PG="
(
for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do 
set PG=!PG! %%~nxB
tiffcp -c lzw !PG! "%DST%%%~nA.TIF"
tiff2pdf -o "%DST%%%~nA.PDF" %DST%%%~nA.TIF
DEL %DST%%%~nA.TIF
))
POPD
set PATH=%oldpath%
Here is the result in CMD
a

c:\TEST\SRC>set oldpath=C:\ProgramData\Oracle\Java\javapath;
C:\Program Files (x86)\Java\jre1.8.0_45;
C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;
c:\Program Files (x86)\Intel\iCLS Client\;
c:\Program Files\Intel\iCLS Client\;
C:\windows\system32;
C:\windows;
C:\windows\System32\Wbem;
C:\windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files\Condusiv Technologies\ExpressCache\;
C:\Program Files (x86)\Windows Live\Shared;
C:\Android;
C:\Windows\System32;
c:\bin;
;
c:\bin;
c:\TEST\SRC>set PATH=C:\ProgramData\Oracle\Java\javapath;
C:\Program Files (x86)\Java\jre1.8.0_45;
C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;
c:\Program Files(x86)\Intel\iCLS Client\;
c:\Program Files\Intel\iCLS Client\;C:\windows\system32
;
C:\windows;
C:\windows\System32\Wbem;
C:\windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files (x86)\Intel\Intel(R)Management Engine Components\IPT;
C:\Program Files\Condusiv Technologies\ExpressCache\;
C:\Program Files (x86)\Windows Live\Shared;
C:\Android;
C:\Windows\System32;
c:\bin;
;
c:\bin;
;
c:\bin;

c:\TEST\SRC>set SRC=c:\test\src\

c:\TEST\SRC>set DST=C:\test\dest\

c:\TEST\SRC>set DBG=ECHO/

c:\TEST\SRC>pushd c:\test\src\
The syntax of the command is incorrect.

c:\TEST\SRC>for /F "tokens=*" %B in ('dir /B/A-D/ONE "%~nA.0*"') do
I broke the lines at every semicolon to make it easier to read.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Old Script on (relatively) new computer

#6 Post by aGerman » 21 Nov 2019 02:53

Compare yours

Code: Select all

(
for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do 
with the proposal of Eureka!

Code: Select all

		for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
and read his comments regarding parentheses.

Steffen

dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Re: Old Script on (relatively) new computer

#7 Post by dchall8 » 21 Nov 2019 10:08

BINGO! :D
Thank you for your patience on this.
Here's the working script...

Code: Select all

@echo on & setlocal EnableExtensions ENableDelayedExpansion
set oldpath=%PATH%
set PATH=%PATH%;c:\bin;
set SRC=c:\test\src\
set DST=C:\test\dest\
set DBG=ECHO/
pushd %SRC%
for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (
set "PG="

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
set PG=!PG! %%~nxB
tiffcp -c lzw !PG! "%DST%%%~nA.TIF"
tiff2pdf -o "%DST%%%~nA.PDF" %DST%%%~nA.TIF
DEL %DST%%%~nA.TIF
))
POPD
set PATH=%oldpath%
Can we look at the logic for a second? Here's an outtake from the ECHO text...
c:\TEST\SRC>(
set PG=!PG! 00233421.001
tiffcp -c lzw !PG! "C:\test\dest\00233421.TIF"
tiff2pdf -o "C:\test\dest\00233421.PDF" C:\test\dest\00233421.TIF
DEL C:\test\dest\00233421.TIF
)
TIFFReadDirectory: Warning, 00233421.001: unknown field with tag 33000 (0x80e8)
encountered.

c:\TEST\SRC>(
set PG=!PG! 00233421.002
tiffcp -c lzw !PG! "C:\test\dest\00233421.TIF"
tiff2pdf -o "C:\test\dest\00233421.PDF" C:\test\dest\00233421.TIF
DEL C:\test\dest\00233421.TIF
)
TIFFReadDirectory: Warning, 00233421.001: unknown field with tag 33000 (0x80e8)
encountered.
TIFFReadDirectory: Warning, 00233421.002: unknown field with tag 33000 (0x80e8)
encountered.

c:\TEST\SRC>(
set PG=!PG! 00233421.003
tiffcp -c lzw !PG! "C:\test\dest\00233421.TIF"
tiff2pdf -o "C:\test\dest\00233421.PDF" C:\test\dest\00233421.TIF
DEL C:\test\dest\00233421.TIF
)
TIFFReadDirectory: Warning, 00233421.001: unknown field with tag 33000 (0x80e8)
encountered.
TIFFReadDirectory: Warning, 00233421.002: unknown field with tag 33000 (0x80e8)
encountered.
TIFFReadDirectory: Warning, 00233421.003: unknown field with tag 33000 (0x80e8)
encountered.
It seems to write .001to 00233421.TIF and to 00233421.pdf. Then goes back and writes page .001 and .002 to 00233421.TIF and the pdf. Then it goes back and writes page .001, .002, and .003. And this goes on until the final page is reached. The result is I have nothing but 2 PDF files in the dest folder, so that's what I want, but I'm wondering if the process could streamlined by not writing the pdf file until all the tif images are collected for the full document? ...which makes me wonder if I have my parentheses in the BEST spot?

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Old Script on (relatively) new computer

#8 Post by Eureka! » 21 Nov 2019 12:29

I have edited the script I posted to show where I think the closing parenthesis of the second for-loop should be placed:

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
set PG=!PG! %%~nxB
)

dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Re: Old Script on (relatively) new computer

#9 Post by dchall8 » 21 Nov 2019 14:11

It is a thing of beauty to watch a true craftsman practicing his/her trade. This gives a much cleaner looking CMD ECHO. Thank you, THANK YOU!
I believe the parentheses and quotes are a significant improvement over whatever script I had working on my previous computers.

One thing that has always concerned me about this script is that it looks to me like it will only work for 99 pages. Maybe there is no way around this, but here's the script that concerns me

Code: Select all

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (set PG=!PG! %%~nxB)
It looks like it identifies a target image file by selecting those that have a name matching the pattern "name.0*" . That would eliminate basically any non-image files in that folder, but there are never anything but the image files in the disk. The disk is written by another computer, so accidents can't happen (!). It seems like a file named name.143, for example, would be excluded because it does not fit the pattern name.0*. Is that the way you see this working? That documents with more than 99 pages get terminated at page 99? I have seen 700 page plus documents in these records. One hundred plus is not that uncommon. The source disk I receive never has anything except the numbered image files on it.

For anyone following along with this, here is what I believe is the final, uncommented script (pending a possible revision as discussed in the previous paragraph).

Code: Select all

@echo on & setlocal EnableExtensions ENableDelayedExpansion
set oldpath=%PATH%
set PATH=%PATH%;c:\bin;
set SRC=c:\test\src\
set DST=C:\test\dest\
set DBG=ECHO/
pushd %SRC%
for /F "tokens=*" %%A in ('dir /B/A-D/ONE "*.001"') do (
set "PG="

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
set PG=!PG! %%~nxB
)
tiffcp -c lzw !PG! "%DST%%%~nA.TIF"
tiff2pdf -o "%DST%%%~nA.PDF" %DST%%%~nA.TIF
DEL %DST%%%~nA.TIF
)
POPD
set PATH=%oldpath%

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Old Script on (relatively) new computer

#10 Post by Eureka! » 21 Nov 2019 15:36

replace

Code: Select all

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.0*"') do (
set PG=!PG! %%~nxB
)
with:

Code: Select all

for /F "tokens=*" %%B in ('dir /B/A-D/ONE "%%~nA.*"') do (
set PG=!PG! %%~nxB
)
( "%%~nA.0*" replaced with "%%~nA.*" to enable up to 999 pages)

or even:

Code: Select all

for /F "tokens=*" %%B in ('dir /B/A-D/OE "%%~nA.*"') do set PG=!PG! %%~nxB
( parentheses are not necessary in this case and sorting by filename makes no sense as thos are all the same (%%~nA gives you the filename without extension) )

You can optimize further (without indentation this time):

Code: Select all

@echo on & setlocal EnableExtensions ENableDelayedExpansion
set PATH=%PATH%;c:\bin;
set SRC=c:\test\src\
set DST=C:\test\dest\
pushd %SRC%

for /F "tokens=*" %%A in ('dir /B/A-D/ON "*.001"') do (
set "PG="

for /F "tokens=*" %%B in ('dir /B/A-D/OE "%%~nA.0*"') do set PG=!PG! %%~nxB

tiffcp -c lzw !PG! "%DST%%%~nA.TIF"
tiff2pdf -o "%DST%%%~nA.PDF" %DST%%%~nA.TIF
DEL %DST%%%~nA.TIF
)

dchall8
Posts: 15
Joined: 17 Oct 2014 16:45

Re: Old Script on (relatively) new computer

#11 Post by dchall8 » 21 Nov 2019 17:54

That works, too. I'll use it.
Performance wise, the file converted 2,766 image files, measuring 119 MB, into 488 PDF files, measuring 118 MB. It is SUCH a relief to have this working.
Processing time was 131 seconds or about 21 images converted into 3.7 PDF documents per second (on average).
There were two documents where TIF2PDF failed. Those documents get created but are corrupted and cannot be opened in Acrobat. When that happens it requires manual intervention to close the TIF2PDF executable, so the time was slightly less, but not by more than a second or two. I was ready for the dialog boxes. TIF2PDF failures happen maybe once a month. This time it happened on two documents in a month's worth of images.

Yellow_13
Posts: 5
Joined: 26 Nov 2019 10:02

Re: Old Script on (relatively) new computer

#12 Post by Yellow_13 » 26 Nov 2019 23:41

Thanks for the explanations! I'm only beginning to work with DOS, and I'm only starting to code by myself... This helps me, A LOT. Placing the parentheses and typos are always that one thing that messes up my scripts...

Post Reply