Seek help on how to use this batch file...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Clutch Cargo
Posts: 1
Joined: 17 Apr 2015 13:57

Seek help on how to use this batch file...

#1 Post by Clutch Cargo » 17 Apr 2015 14:02

Hi there,
Yes a nube with very little experience so I am seeking help. Here's some background info:

I have a python script 'gdalcopyproj.py' (I did not write it), and it works great. Basically it takes a tiff geo-referenced image file and transfers that information into another tiff image that does not have geo-referenced data.

The script is written to apply this one file at a time "gdalcopyproj.py source_file dest_file". One uses it inside the command prompt of a program called OSGeo4W to implement the script. For example, it looks like so in the command window.

“C:\OSGeo4W>python gdalcopyproj.py SOCAL_CZ62.tif SOCAL_CZ62_LM.tif” (appears to need the text "python" ahead of the script in order to work).

My goal is to process a whole folder of files at once. I do have a batch file that has been created for that purpose. Here is that batch file:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
@for /f "delims=" %%i in ('2^>nul dir/a-d/b ??????.tif^| findstr/i "^......\.tif$"') do @(
for /f "delims=" %%j in ('2^>nul dir/a-d/b "%%~ni_*.tif"') do @(
gdalcopyproj.py "%%i" "%%j"
)
)

What I do not understand is how to use/setup this batch file. It appears to be seeking a certain type of file which has six characters and converts similarly named files which have custom filenames after the “_”. Is this correct?

Second, where does one put this batch file. I have tried in the same folder as the files. I have tried in the directory of OSGeo4W but no luck. I have tried running the batch directly or from inside a command window. Still no luck. Either I get an error message or simply nothing happens.

Can someone explain how to use this batch file?

Clutch

P.S. – not sure if you need the python script to help explain but here she is:


try:
from osgeo import gdal
except ImportError:
import gdal
import sys
if len(sys.argv) < 3:
print("Usage: gdalcopyproj.py source_file dest_file")
sys.exit(1)
input = sys.argv[1]
dataset = gdal.Open( input )
if dataset is None:
print('Unable to open', input, 'for reading')
sys.exit(1)
projection = dataset.GetProjection()
geotransform = dataset.GetGeoTransform()
if projection is None and geotransform is None:
print('No projection or geotransform found on file' + input)
sys.exit(1)
output = sys.argv[2]
dataset2 = gdal.Open( output, gdal.GA_Update )
if dataset2 is None:
print('Unable to open', output, 'for writing')
sys.exit(1)
if geotransform is not None and geotransform != (0,1,0,0,0,1):
dataset2.SetGeoTransform( geotransform )
if projection is not None and projection != '':
dataset2.SetProjection( projection )
gcp_count = dataset.GetGCPs()
if gcp_count != 0:
dataset2.SetGCPs( gcp_count, dataset.GetGCPProjection() )
dataset = None
dataset2 = None

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

Re: Seek help on how to use this batch file...

#2 Post by Squashman » 17 Apr 2015 20:06

Might help if you explain where you got that code from or shows a link to where you got the code. It my shed some light on what you are trying to do.

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

Re: Seek help on how to use this batch file...

#3 Post by foxidrive » 17 Apr 2015 21:44

Clutch Cargo wrote:I have a python script 'gdalcopyproj.py' (I did not write it), and it works great.
For example, it looks like so in the command window.

“C:\OSGeo4W>python gdalcopyproj.py SOCAL_CZ62.tif SOCAL_CZ62_LM.tif” (appears to need the text "python" ahead of the script in order to work).

My goal is to process a whole folder of files at once.


Test this: create c:\out-folder\ and change c:\folder\ to where you need it.

I didn't study your script closely - if the python script can handle paths then it should work.
It can be launched from anywhere, in theory.

Code: Select all

@echo off
pushd "C:\OSGeo4W"
for %%a in ("c:\folder\*.tif") do (
      call python gdalcopyproj.py "%%~a" "c:\out-folder\%%~na_LM%%~xa"
)
popd
pause


The call keyword may not be needed but try it with it anyway.

Post Reply