Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts

    Batch script - recursing through folders

    I'm trying to write a batch script to do a lot of work for me by converting TIF files into PDFs. I've got an executable to do the conversion, but need some help with the script.

    Code:
    FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO (
        image2pdf -i "%%G\*.TIF" -o "%%G\output.pdf"
    )
    What that does is recurses through all sub-directories and calls image2pdf. That script, when run inside a folder, adds all images into one PDF. Right now the script works and creates output.pdf in all of the folders. What I'd like to do is be able to name the file by date according to the folder structure. Here's an example:

    I run the script in the top level folder, which sees this tree:

    Code:
    20090202
    ->55
    -->123456 (this folder has all of the files)
    20090203
    ->55
    -->123456 (this folder has all of the files)
    20090204
    ->55
    -->123456 (this folder has all of the files)
    etc.

    Is there some way I can identify that top level folder and save it as a variable so I can use the date information?
    Last edited by bacterozoid; 09-08-2009 at 08:08 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    As far as I know you split things simply with the "for" loop, which you are already familiar with. "cd" with no arguments will give you a string of the current directory.

    It may be better to loop through each of the dates and save the output pdf string then, while in each, loop through each of the subdirectories again to convert the images.

    some pseudo code:
    Code:
    for each date_folder (
      create the date\output.pdf string s
      for each recursive subdirectory (
        save to s
      }
    )
    If you are still having problems with that it might be time to change to a more powerful language such as vbs, power shell or a small executable written in a high level language.
    Last edited by ghell; 09-08-2009 at 06:16 PM.

  • Users who have thanked ghell for this post:

    bacterozoid (09-08-2009)

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    I'd like to avoid PS and VBA if possible - simple is better in this case. You have given me an idea, but I'm stuck once more.

    In the script below, I've managed to get the month, day, and year, which I can now use to create my filename.

    One improvement I'd like to make now, is to not create unnecessary files. My image2pdf program creates a PDF file regardless of what's in the folder. I want to look at the folder, and if there are no *.TIF files, not run the script.

    I understand the logic of the GOTO command, but need help with the condition. I tried counting the files in the directory, which works...but I don't know how to assign that to a variable (what I have doesn't work). My thought is to simply count the number of files in the current directory (non-recursive)...if that's 0, then don't create the PDF. That way I can output my PDF files all to one folder and not have to move them all manually.

    Code:
    @echo off
    
    FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO (call :makePDF "%%G")
    GOTO :done
    
    :makePDF
     set data=%1
     
     :: Remove quotes
     for /f "useback tokens=*" %%a in ('%data%') do set data=%%~a
     echo %data%
     
    
    
    
    
    ::::::::::::::Trying to get the number of files in the current directory
    
     SET num_files = dir "%data%" /b |find /v /c "::"
     echo Number files: %num_files%
    
    ::::::::::::::Trying to get the number of files in the current directory
    
    
    
    
    
    
    
     
     :: Get the last part of the string
     set data=%data:~-18%
     
     :: Get just the date info now
     set str=%data:~0,8%
     
     :: Get month, day, and  year
     set year=%data:~0,4%
     set month=%data:~4,2%
     set day=%data:~6,2%
     
     echo year:  %year%
     echo month: %month%
     echo day:   %day%
     
    
     
    GOTO :eof
    
    :done
    pause

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    I didn't mention VBA at all. Visual Basic for Applications is something completely different to Visual Basic Script. VBS is a simple scripting language supported by most versions of Windows that can be run in windowed or console mode. You can also use JScript (.js files) but the result is about the same you just use a different syntax. VBA is the crappy language you get inside Microsoft Access and the like that is just a little bit more powerful than Microsoft Office macros.

    I will assume you meant that you didn't want to use Power Shell or VBS.


    For checking the existance you can use
    if not exist *.tif

  • Users who have thanked ghell for this post:

    bacterozoid (09-08-2009)

  • #5
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    I did mean VBS, sorry about that! (An acceptable typo, I hope, considering the QWERTY keyboard).

    Much simpler solution than what I was going for...it worked just fine. Thanks for your help!

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    I just thought I would make the distinction because VBS is commonly used for doing things just out of reach of batch (but all possible in power shell) while VBA is hated by absolutely everyone and you would be quite right never to use it.

    Glad it worked for you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •