Garth Cooper
Garth Cooper

Reputation: 155

Run command on specific types of files

I'm currently trying to run a program on certain types of files. Below is my batch script to do the following:

  1. Clear a text file called "Projections.txt"

  2. Loop through all the files in the current directory

  3. If the file type .prj is found run the program gdalsrsinfo.exe and write it to a text file

Code is here:

break>projections.txt
for %%a in (*) do (
if exist *.prj (
echo ".prj file found"
echo %%a >> projections.txt
"C:\Program Files\GDAL\gdalsrsinfo.exe" -e proj4 %%a >> projections.txt
)
)

I can't seem to run the program on just the .prj files, it's putting every file in the directory through "gdalsrsinfo.exe".

My question is how do I run "gdalsrsinfo.exe" on just .prj files.

Thanks

Upvotes: 0

Views: 580

Answers (2)

Compo
Compo

Reputation: 38718

The following methodology would be quicker and more efficient.

@(For %%G In ("*.prj")Do @(Echo ".prj file found">CON
        Echo %%G
        "%ProgramFiles%\GDAL\gdalsrsinfo.exe" -e proj4 %%G 
   ))>"projections.txt"

It is quicker and more efficient because it does not create an output file, then open, write and close it twice for every .prj file found.

In a more simplified format:

@Echo Off
(
    For %%G In ("*.prj") Do (
        Echo ".prj file found">CON
        Echo %%G
        "%ProgramFiles%\GDAL\gdalsrsinfo.exe" -e proj4 %%G 
    )
)>"projections.txt"

Upvotes: 1

user7818749
user7818749

Reputation:

To fix your current code.

@echo off
break>projections.txt
for %%a in (*) do (
   if "%%~xa" == ".prj" (
       echo ".prj file found"
       echo %%a >> projections.txt
       "C:\Program Files\GDAL\gdalsrsinfo.exe" -e proj4 %%a >>projections.txt
   )
)

If however it is just .prj files you are interested in and not any other file you can explicitly filter for it in the for loop and eliminate the if exist part.

@echo off
break>projections.txt
for %%a in (*.prj) do (
       echo ".prj file found"
       echo %%a >> projections.txt
       "C:\Program Files\GDAL\gdalsrsinfo.exe" -e proj4 %%a >>projections.txt
   )

Upvotes: 1

Related Questions