Find files, concat, rename, convert, store in new folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find files, concat, rename, convert, store in new folder

#16 Post by Squashman » 20 Oct 2014 17:38

foxidrive wrote:
I learned to read with comics. ;) It was a great way to get kids to read.

I grew up a God fearing Catholic. Lots of Old and New Testament. For fun I got to read and color in my Mom's human anatomy books.

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

Re: Find files, concat, rename, convert, store in new folder

#17 Post by dchall8 » 20 Oct 2014 17:50

It is working nicely. Thank you so much. :D :D :D

For now we do have some page numbers up to 20,000. Those are not real page numbers, but for ease of doing this, I set it up this way. Some of the "page numbers" are page numbers and some are document numbers. Those which are well over 1,000 are document numbers. I can deal with that naming issue later on. What do I need to change to use a 5-digit page number?

Your hobby is writing about DOS and batch files. My hobby is writing about lawn care with a specialty in organic lawn care. If you want to have the nicest lawn on the block, or on the planet, or even if you just want nicer looking weeds, I can set you up.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find files, concat, rename, convert, store in new folder

#18 Post by Squashman » 20 Oct 2014 17:55

I will take you up on that. I don't have too much problems keeping the weeds out. I just can't get that nice thick bed of green grass.

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

Re: Find files, concat, rename, convert, store in new folder

#19 Post by foxidrive » 20 Oct 2014 18:26

dchall8 wrote:It is working nicely. Thank you so much. :D :D :D

What do I need to change to use a 5-digit page number?


It's good to hear it.

change the 4 in this line to 3 (Then it will skip 3 characters from the start of the name)

Code: Select all

 set "pagenum=!pagenum:~4!"


My hobby is writing about lawn care with a specialty in organic lawn care. If you want to have the nicest lawn on the block, or on the planet, or even if you just want nicer looking weeds, I can set you up.


My brother is a lounge chair gardener - he loves the shows on TV.
I have some restrictions in bending and physical labour for more than 5 minutes (motorcycle accident) so I'm not a gardener either.

squashman wrote:For fun I got to read and color in my Mom's human anatomy books.


I have an interest in medical things, and anatomy etc. The other books you mentioned aren't on my reading list ;)

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

Re: Find files, concat, rename, convert, store in new folder

#20 Post by dchall8 » 21 Oct 2014 08:04

In looking at the code, I think I see where you use the md command to create a dst directory, but I don't see the dst directory anywhere. The files are pouring into the Destination folder. I can deal with that, but I thought you should know that the md "%dst%" 2>nul might not be working as you intended.

Best way to get hold of me for lawn care help is through the screen name dchall_san_antonio. I'm on three forums but mostly on GardenWeb.com and Aroundtheyard.com. At Arounttheyard you can pm me. On GardenWeb my profile has a link to my direct email. Been doing that since 2002, so if I can't help you, I know someone who can.

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

Re: Find files, concat, rename, convert, store in new folder

#21 Post by foxidrive » 21 Oct 2014 22:08

dchall8 wrote:In looking at the code, I think I see where you use the md command to create a dst directory, but I don't see the dst directory anywhere. The files are pouring into the Destination folder.


There is a line that sets the DST variable to c:\Destination and that is where the files should be going.
Are they doing something else?

The MD command you mentioned just ensures that the correct directory is created if it doesn't already exist,
but you need write permission to c:\ if you want to create the folder there.

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

Re: Find files, concat, rename, convert, store in new folder

#22 Post by dchall8 » 22 Oct 2014 10:59

No new folders (directories) are being created. The files are going into the Destination folder (directory). At this point it is water under the bridge, because I've converted 150,000 files (YES!!) :D :D :D :shock: The last batch is running right now.
But I am still interested in the batch syntax to create new folders and insert the files into the new folder.

Would you mind adding line by line comments to help me learn that? The comments you see in the file I posted were mine to help me learn/remember how it worked. The originator only put in the one that you noted was broken. I don't know what that was or was referring to.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Find files, concat, rename, convert, store in new folder

#23 Post by ShadowThief » 22 Oct 2014 16:59

I hope foxidrive doesn't mind that I've taken the liberty of commenting his code...

Code: Select all

:: Prevent these commands from being displayed while the batch file is running
:: Enable command processor extensions to ensure that for is able to use the /f switch
:: Enable delayed environment variable expansion so that variables will update inside of loops properly
:: Starting batch scripts with this line is not always necessary, but generally encouraged
@echo off & setlocal EnableExtensions EnableDelayedExpansion

:: Make a backup of the current PATH variable
set "oldpath=%PATH%"

:: Add C:\bin to the PATH so that you don't need to write C:\bin\tiffcp every time
set "PATH=%PATH%;c:\bin;"

:: Source location for the original files
:: %userprofile% is a short way of saying C:\Users\<your username>
set "SRC=%userprofile%\documents\Orginal Deeds"

:: Destination location for the PDF files (DST)
set "DST=c:\Destination"

:: Create the directory C:\Destination
:: 2>nul prevents any error messages from appearing on the screen during this
md "%dst%" 2>nul


:: Commands are only echoed until %DEB% ist set to nothing - this is broken
set DBG=ECHO/

::set "DBG="

:: Go into the directory indicated by the %SRC% variable
:: pushd keeps track of the directory you were just in so you can immediately return to it
pushd "%SRC%"

:: for /f processes the output of the command in parentheses line by line
:: "delims=" means each line gets process as a single string instead of being broken up by delimiters
:: %%z is the variable that the current line gets stored to
:: /b will make the dir command return nothing but names of files and folders
:: /s will recursively search any folders that are currently inside the directory
:: /ad will make dir only return folders
for /f "delims=" %%z in ('dir /b /s /ad') do (
   REM Go to the directory indicated by %%z
   REM Also, I've switched from double colons to REM because the two colons is technically a line label
   REM and line labels don't play nice with for loops for me even though other people have had luck
   pushd "%%z"

   REM If any files with the 001 extension exist...
   if exist *.001 (
      REM Process the list of all files with the 001 extension one file at a time, storing each filename in %%A
      REM /a-d will make dir return everything in the folder that isn't another folder
      REM /ONE is short for /ON /OE and sorts the files alphabetically first by name and then extension
      for /F "delims=" %%A in ('dir /B/A-D/ONE "*.001"') do (
         REM Set pagenum to %%A, using ~n to ensure that only the filename and not part of the path is included in the variable value
         set "pagenum=%%~nA"

         REM Remove all but the four leftmost characters of pagenum
         set "pagenum=!pagenum:~4!"
       
         REM Empty the PG variable
         set "PG="
         REM Take the list of files whose names are all %%A and create one long string containing all of those files
         REM ~n
            for /F "delims=" %%B in ('dir /B/A-D/ONE "%%~nA.*"') do set PG=!PG! %%~nxB

         REM Concatenate the files into the Source folder with a .TIF extension
         tiffcp -c lzw !PG! %%~nA.TIF

         REM Convert the TIF file to a PDF
         tiff2pdf -o "%dst%\Volume %%~nxz page !pagenum!.pdf" %%~nA.TIF

         REM Delete the TIF files
         DEL %%~nA.TIF
      )
     
      REM Wait until the user presses the enter key
      pause
   )
   
   REM Return to the directory that you were in before you went into %%z
   popd
)

:: Return to the directory that you were in before you went into %SRC%
POPD

:: Reset the path to the original path
set "PATH=%oldpath%"

:: Note that you don't have to use the exit command to stop the script
:: Since there is nothing left for the script to do, the program ends naturally
Last edited by ShadowThief on 22 Oct 2014 17:33, edited 1 time in total.

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

Re: Find files, concat, rename, convert, store in new folder

#24 Post by foxidrive » 22 Oct 2014 17:29

ShadowThief wrote:I hope foxidrive doesn't mind that I've taken the liberty of commenting his code...


Not at all, thanks.

Just commenting that %%z is case sensitive and your explanations use %%Z

This line should have source in it.
REM Concatenate the files into the Source folder with a .TIF extension


dchall8 wrote:No new folders (directories) are being created. The files are going into the Destination folder (directory).


The Destination directory is the only directory that is being created, if it doesn't exist (and you have write permissions). :)
That's what we discussed earlier - where the files would go.

foxidrive wrote:
dchall8 wrote:I suppose it is not critical to recreate the original folder structure, but the naming convention already in use makes it easy to find these files.

The PDF files can all go into a single folder in c:\ so that makes it a simpler script

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Find files, concat, rename, convert, store in new folder

#25 Post by ShadowThief » 22 Oct 2014 17:36

Thanks, I've edited my previous post. I always forget that for variables are case sensitive because other variables aren't.

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

Re: Find files, concat, rename, convert, store in new folder

#26 Post by dchall8 » 27 Oct 2014 17:16

Lifesaver is too strong a term, but you guys really improved our life here in the office. Thank you so much for the extensive help. -No way I could have done that. And thanks for the commenting, too. No way I could have done that either.

In the end my 800,000+ images were reduced to about 175,000 individual PDF documents. We have several very happy campers here, my workload is reduced by not having to help the people who had trouble viewing the individual pages, and I'm much more willing to read these documents instead of viewing the individual images. I moved the PDFs to our server so everyone can read them whenever they need to.

Thanks again.

Post Reply