Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to combine forfiles and for loop script to run the file  (Read 21671 times)

0 Members and 2 Guests are viewing this topic.

_unknown_

    Topic Starter


    Beginner

    • Experience: Beginner
    • OS: Windows 7
    How to combine forfiles and for loop script to run the file
    « on: October 27, 2014, 01:41:09 AM »
    How will I combine this forfiles and for loop script to achieve the following goal:

    • project all the level 2 HDF Files only
    • save to a new directory
    • every time that there are new level 2 HDF Files added to the directory, project it immediately

    Here are the two script:

    First script:

    set in_path=in_path
    set out_path=out_path

    md %out_path%
    cd /d %in_path%

    FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs EPSG:4326 -te 113.205 1.120 157.105 2.005 HDF4_SDS:hdf:@file:01 %out_path%\@fname.tif"*


    Second script:

    for /f "tokens=1,2,5" %a in ('dir /-b /o-d /tc') do @if %a==%date:~4% (if %b==%time:~0,5% (echo %c)

    _unknown_

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Windows 7
      -
      « Reply #1 on: October 27, 2014, 03:26:44 AM »
      -

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: How to combine forfiles and for loop script to run the file
      « Reply #2 on: October 27, 2014, 07:37:27 AM »

      • project all the level 2 HDF Files only


      You need to explain what you mean by level 2 HDF File and also what you think project means.

      It's not clear from your post.

      _unknown_

        Topic Starter


        Beginner

        • Experience: Beginner
        • OS: Windows 7
        Re: How to combine forfiles and for loop script to run the file
        « Reply #3 on: October 27, 2014, 11:30:38 PM »
        Hierarchical Data Format (HDF) is a set of file formats (HDF4, HDF5) designed to store and organize large amounts of numerical data. HDF4 is the older version of the format, although still actively supported by the HDF Group. It supports a proliferation of different data models, including multidimensional arrays, raster images, and tables.

        While projection/reprojection is to change the projection (or coordinate system) of spatial data with another projection like sinusoidal, longlat, utm and etc.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: How to combine forfiles and for loop script to run the file
        « Reply #4 on: October 28, 2014, 06:58:52 AM »
        Well that just confused me even more.
        I am not sure what your intended goal is.  I do not see any correlation between either of the two scripts.

        I have no idea why you are using this.
        Code: [Select]
        dir /-b /o-d /tcYou cannot use a minus symbol with the BARE option.  It will come out as bare format whether you use the minus symbol or not.

        _unknown_

          Topic Starter


          Beginner

          • Experience: Beginner
          • OS: Windows 7
          Re: How to combine forfiles and for loop script to run the file
          « Reply #5 on: October 28, 2014, 10:16:58 PM »
          Sorry if it confused you more. To explain further let's say I have downloaded numerous hdf files from an ftp site and I already have 500 hdf files in the local directory, 250 of them were level 1 hdf files and the rest were level 2. The level 1 format is: A2001031505000.L1A_LAC.sample.hdf and the level 2 format is: A2001017054000.L2_LAC.sample.hdf As what you will observe they differ in this part L1A_LAC, L2_LAC. Now, how will I do a batch process in projecting all the level 2 hdf files only? How will I filter these files and create a new directory for the filtered files? And the script below solve this problem.

          set in_path=in_path
          set out_path=out_path

          md %out_path%
          cd /d %in_path%

          FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs EPSG:4326 -te 113.205 1.120 157.105 2.005 HDF4_SDS:hdf:@file:01 %out_path%\@fname.tif"*

          But then I still need one thing; what should I add in this script so that every time that there are new level 2 HDF Files added to the directory, the script should project it immediately.
          considering that these hdf files from the ftp site are always delayed one day. This means that the files that should be projected today are those files yesterday. How will I achieve this goal? Thank you for your patience!

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: How to combine forfiles and for loop script to run the file
          « Reply #6 on: October 29, 2014, 06:16:55 AM »
          Still don't understand how your 2nd script is suppose to integrate with your first script.  Nor am I understanding why you keep using the word PROJECTED in a manner that is not normally used in the ENGLISH language.

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: How to combine forfiles and for loop script to run the file
          « Reply #7 on: October 29, 2014, 08:55:55 AM »
          using the word PROJECTED

          It's a scientific/math use, going by a previous message.


          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: How to combine forfiles and for loop script to run the file
          « Reply #8 on: October 29, 2014, 09:01:29 AM »
          This should check every 30 seconds, after the files have been processed, then move the files to the folder and process the next lot.

          Code: [Select]
          @echo off
          set "in_path=d:\in_path"
          set "out_path=e:\out_path"

          cd /d "%in_path%"

          :loop

          if exist "*.L2_LAC.*.hdf" (
             echo moving files @ %date% %time%
             move "*.L2_LAC.*.hdf" "%out_path%" >nul
                cd /d "%out_path%"
                   FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs EPSG:4326 -te 113.205 1.120 157.105 2.005 HDF4_SDS:hdf:@file:01 0x22%out_path%\@fname.tif0x22 "*
                cd ..
          )

          timeout /t 30 /nobreak
          goto :loop

          _unknown_

            Topic Starter


            Beginner

            • Experience: Beginner
            • OS: Windows 7
            Re: How to combine forfiles and for loop script to run the file
            « Reply #9 on: October 29, 2014, 07:41:12 PM »
            foxidrive is right. Projection is a systematic transformation of the latitudes and longitudes of locations. And I'm using it for scientific purposes.

            _unknown_

              Topic Starter


              Beginner

              • Experience: Beginner
              • OS: Windows 7
              Re: How to combine forfiles and for loop script to run the file
              « Reply #10 on: October 30, 2014, 01:36:17 AM »
              Hi again foxidrive tried your script but it doesn't even project/processed the .hdf files from the input directory rather it just moved those files to the output directory. How will I solve this?

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: How to combine forfiles and for loop script to run the file
              « Reply #11 on: October 30, 2014, 05:07:53 AM »
              I misunderstood what you need to do.

              This should process the new "*.L2_LAC.*.hdf" files
              by moving them to a staging temp folder so that new files are not confused with the current set
              Then it runs your FORFILES command on the files in the temp folder
              and moves the processed files to a another folder called processed files
              The output files go into your out_path location.

              Every 30 seconds it will check for new files and repeat but it will not check for more files until the current set of files have been processed

              The two sets of lines that are identical are there on purpose,
              to prevent files from being skipped in case the batch file is closed accidentally or on purpose.

              If you need to kill the batch file then do so while it is counting the 30 second timer (requires Vista or higher).

              untested: If you have issues with it then report what you see on the screen


              Code: [Select]
              @echo off
              set "in_path=d:\in_path"
              set "out_path=e:\out_path"

              set "temp_path=%out_path%\temp"
              set "processed_files_path=%out_path%\processed files"
              md "%processed_files_path%" 2>nul
              cd /d "%in_path%"

              :loop
              set now=%date% %time%
              if exist "*.L2_LAC.*.hdf" (
                 rd "%temp_path%"
                 if exist "%temp_path%\" echo An ERROR occurred as the "%temp_path%\" folder exists - may have unprocessed files within: & pause & goto :EOF
                 md "%temp_path%" 2>nul
                 echo moving files @ %now%
                 move "*.L2_LAC.*.hdf" "%temp_path%" >nul
                    cd /d "%temp_path%"
                       echo projecting files
                       FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs EPSG:4326 -te 113.205 1.120 157.105 2.005 HDF4_SDS:hdf:@file:01 %out_path%\@fname.tif"
                       move "*.L2_LAC.*.hdf" "%processed_files_path%" >nul
                    cd ..
                    rd "%temp_path%"
                    if exist "%temp_path%\" echo An ERROR occurred as the "%temp_path%\" folder exists - may have unprocessed files within: & pause & goto
              )

              timeout /t 30 /nobreak
              goto :loop
              « Last Edit: October 30, 2014, 05:37:18 AM by foxidrive »

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: How to combine forfiles and for loop script to run the file
              « Reply #12 on: October 30, 2014, 06:48:15 AM »
              foxidrive is right. Projection is a systematic transformation of the latitudes and longitudes of locations. And I'm using it for scientific purposes.
              He was basically regurgitating what you said.  You need to relate what you need to do without the scientific terms in plain english.  Basically what you are saying is you want to run a program depending on certain things that are happening with the data.  The word Projection should have never been used to describe what you were trying to do.

              _unknown_

                Topic Starter


                Beginner

                • Experience: Beginner
                • OS: Windows 7
                Re: How to combine forfiles and for loop script to run the file
                « Reply #13 on: October 30, 2014, 10:13:13 PM »
                @foxidrive Your script read the input path and created the output path and then projected the hdf files from the input path successfully. But I don't want to moved the hdf/processed files to the output path. I want the hdf files to remain in the input directory and then the bat file will just detect only if there are new files(based on the date) in the input directory to be project/processed.

                Thank you again for your patience!

                Squashman



                  Specialist
                • Thanked: 134
                • Experience: Experienced
                • OS: Other
                Re: How to combine forfiles and for loop script to run the file
                « Reply #14 on: October 31, 2014, 05:35:44 AM »
                then the bat file will just detect only if there are new files(based on the date) in the input directory to be project/processed.
                Which is why I asked how you 2nd script was suppose to integrate with your first.