Find a string in a filename

Dell / Optiplex 960
February 25, 2011 at 13:45:39
Specs: Microsoft Windows XP Professional, 2.992 GHz / 3325 MB

In c:\bunke6\ I have different TIFF World files with filenames like

where * = various text;
e.g. AR_Iddjajavri_2034-2_1983_50000_utm35_400.tfw and Jord_cwx109110_1975_10000_ngo3_100.tfw

I would like to have a batch-file (e.g. "make_prj.bat") that depending on the appearance of utm32, utm33, utm35, ngo1, ngo2,..., ngo8 in the filename of the tfw-file, copies a file utm32.prj, utm33.prj, ..., ngo1.prj, ... or ngo8.prj from the folder r:\projeksjoner\ to c:\bunke6\ and renames it to the same name as the corresponding tfw-file.

If c:\bunke6\AR_Iddjajavri_2034-2_1983_50000_utm35_400.tfw,
then copy utm35.prj from r:\projeksjoner\ to c:\bunke6\ and
rename utm35.prj to AR_Iddjajavri_2034-2_1983_50000_utm35_400.prj

I have tried different "for" and "findstr" approaches based on other posted questions and answers on, but without any luck so far.

I would appreciate any help.

Best regards
Anne, Norway

See More: Find a string in a filename

Report •

February 25, 2011 at 16:04:50
Thanks to this thread "batch script to parse filenames":

I did find a solution by modifying Mechanix2Go's reply #3.

This does the trick:

::The @ character suppresses the echoing of the current command on that line, and the echo off command supresses all subsequent echoing
@echo off
setLocal enableDELAYedeXpansion

::Delete old log-file
if exist prj.log (del prj.log)

::For each tfw-file
for %%f in (*.tfw) do (
::Get 5th and 6th string separated by underscore in each tfw-filename
for /f "tokens=5,6 delims=_" %%a in ("%%f") do (
::Searching for "utm" or "ngo" in the 5th token, sending the output to the null device which uses the special filename nul
echo %%a|findstr "utm ngo" > nul
::findstr returns errorlevel 0 if it finds the search term, and errorlevel 1 not found otherwise
if errorlevel 1 (
::If 5th token don't contain utm or ngo, then the 6th token (%%b) contains utm or ngo
set myvar=%%b
) else (
set myvar=%%a
::Copying and renaming the correct prj-file to have the same filname as the corressponding tfw-file
if exist R:\projeksjoner\shape\!myvar!.prj (
::prj-file exist
copy R:\projeksjoner\shape\!myvar!.prj %%~pnf.prj
) else (
::Non-existing projection, wrong name, the tfw-filename is saved in a log-file
echo %%f >> prj.log


Report •
Related Solutions

Ask Question