Home > other >  Need to run GPSBabel command on txt files
Need to run GPSBabel command on txt files

Time:09-17

I'm using windows 10. My current project requires me to merge several TXT files into a single one. Problem is that the TXT files has all the same name and what differentiate them is their folder name.

For example:

Folder1

  • Folder with a 13 digits random name
    • gpsdata.txt
  • Folder with a 13 digits random name
    • gpsdata.txt - Folder with a 13 digits random name
    • gpsdata.txt

I tried using the following BATCH command but it overwrites the file and I end with only the track from the last gpsdata.txt file.

FOR /R C:\FOLDER1 %%i IN (metadata.txt) DO X:\GPSBabel\gpsbabel -t 
-i xcsv,style=C:\mystyle.style -f %%i -o gpx -F C:\FOLDER2\gpsdata.gpx

Is there a way to 'copy and merge' these txt files and transform then into gpx via .BAT using GPSBabel?

thanks

CodePudding user response:

The FOR loop does not seem to be producing what you want. Try constructing the FOR loop alone until %%i comes out as you expect.

FOR /R C:\FOLDER1 %%i IN (metadata.txt) DO (ECHO %%~i)

You might want the FOR loop to produce a path to every metadata.txt file. Does this produce what you are seeking?

FOR /F "delims=" %%i IN ('DIR /S /B C:\FOLDER1\metadata.txt') DO (echo %%i)

That might make the code look like:

FOR /F "delims=" %%i IN ('DIR /S /B "C:\FOLDER1\metadata.txt"') DO (
    X:\GPSBabel\gpsbabel -t -i xcsv,style=C:\mystyle.style -f "%%~i" -o gpx -F C:\FOLDER2\gpsdata.gpx
)

CodePudding user response:

Do not use for /R, neither do use for /F with dir /S, since you have got a flat directory structure. What you need is for /D, or for /F together with dir in order to control sorting.

  1. Approach using for /D (sub-directories are iterated in the order as returned by the file system):

     rem // Prepare an empty file to begin with:
     copy nul "C:\FOLDER1\metadata.txt" > nul
     rem // Iterate over the immediate sub-directories:
     for /D %%I in ("C:\FOLDER1\?????????????") do (
         rem // Check whether the current sub-directory contains the text file:
         if exist "%%~I\gpsdata.txt" (
             rem // Append the text file to the result file:
             copy /B "C:\FOLDER1\metadata.txt"   "%%~I\gpsdata.txt" "C:\FOLDER1\metadata.txt" > nul
         )
     )
    
  2. Approach using for /F and dir (sub-directories are sorted in alphabetic manner due to /O:N):

     rem // Change into target directory:
     pushd "C:\FOLDER1" && (
         rem // Prepare an empty file to begin with:
         copy nul "metadata.txt" > nul
         rem // Iterate over the immediate sub-directories:
         for /F "delims=" %%I in ('dir /B /A:D-L-H-S /O:N "?????????????"') do (
             rem // Check whether the current sub-directory contains the text file:
             if exist "%%I\gpsdata.txt" (
                 rem // Append the text file to the result file:
                 copy /B "metadata.txt"   "%%I\gpsdata.txt" "metadata.txt" > nul
             )
         )
         rem // Return from target directory:
         popd
     )
    
  • Related