Forum Home
Press F1
 
Thread ID: 36819 2003-08-21 05:14:00 Batch/NT Script file creation - can you do this for me? Mike (15) Press F1
Post ID Timestamp Content User
169195 2003-08-21 05:14:00 I need a batch file or NT script for a one-off job at work...

I have about 25 folders with a varying number of files. I need to search those folders for specific filetypes (.shp) and for EVERY .shp file it finds to copy a certain file (master.prj) from another folder and name it to be the same as the .shp, so where there are more than one .shp (there is usually about 3 or 4) the one file is copied for each .shp.

In other words, something like this:

Search Folder1. If folder1 contains *.shp copy \folder0\master.prj to \folder1\ naming that file to be the * from *.shp :)

file.shp and file1.shp get .prj files called file.prj and file1.prj

the .prj files are all copied from the one master.prj, so they're all identical, I just need one for each .shp.

Can it be done??? How? It's been a while since I've written a more complex batch file and I can't remember how to do this one (if it can in fact be done!).

It may be easier to not search the folders at the start, but just run the batch file from every folder, but if it can search the folders then that'd make it much quicker.

Any takers??? :D

Mike.
Mike (15)
169196 2003-08-21 05:18:00 Come to think of it, it wouldn't be a one-off job, as I'll probably be able to use the batch file for other very similar stuff I've got going at the moment and in the near future.

It would be much appreciated :)

Mike.
Mike (15)
169197 2003-08-21 06:22:00 Well I've got as far as dir /s *.shp just need to get it to do something with it. -=JM=- (16)
169198 2003-08-21 07:35:00 Are the .shp and .prj file names the same?

And do you need to search the entire hard drive for the .shp files or just a folder subset?
antmannz (28)
169199 2003-08-21 09:10:00 > Are the .shp and .prj file names the same?

NO :)

I have only ONE .prj file, I need to copy it many many times and renaming each copy to be the same as each .shp file.

> And do you need to search the entire hard drive for
> the .shp files or just a folder subset?

well I have to search a folder full of folders on a network drive :)

Mike.
Mike (15)
169200 2003-08-21 09:12:00 I don't think its possible using a DOS batch.
It has a for loop but its limited to files. eg

for %%f in (*.shp) do echo %%f


If installing cygwin is an option I could do it with ~5 lines of bash script.
bmason (508)
169201 2003-08-21 09:26:00 > If installing cygwin is an option I could do it with ~5 lines of bash script.


cygwin???

I forgot, I could do it from Unix (Sun Solaris), but I know my way around that far far far less than DOS :)

Mike.
Mike (15)
169202 2003-08-21 10:08:00 sources.redhat.com


The script would work on solaris if it has bash installed, but I think it only has csh by default.

If you want to have a play with it the following should work. The syntax isn't too different from DOS:

#!/bin/bash

# boilerplating
if [[ ! -f "$1" || ! -d "$2" ]]; then
echo "$0 usage: <master file path> <base of dirs to process>"
exit
fi

# Handle paths containing spaces
IFS=$'\n'

for i in `find "$2" -iname \*.shp`; do
# take the filename found and change the last shp to prj
# i forgot how to do ignore case properly.
FILENAME="`echo $i | sed -e 's/[sS][hH][pP]$/prj/'`"
# remove the echo to actually do the copy
echo cp -v "$1" "$FILENAME"
done


There will be nicer ways to do it but thats what I came up with.
bmason (508)
169203 2003-08-21 10:16:00 Mike,

How about something like:

@ECHO OFF
::
SET SRCHDIR=C:\
SET FILEXT2=SHP
SET MASTERFIL=C:\MASTER.PRJ
::
PUSHD %SRCHDIR%
::
FOR /D %%i IN (*) DO CALL :ISMYDIR "%%i"
POPD

GOTO FINI

::
:: For each directory process each %FILEXT2% file
::
:ISMYDIR
SET TXTDIR=%1
ECHO Searching for --)) %SRCHDIR%%TXTDIR%\*.%FILEXT2% ((--
FOR %%i IN (%SRCHDIR%%TXTDIR%\*.%FILEXT2%) DO CALL :ISUPDATE %TXTDIR% %%~di "%%~pi" "%%~ni"
GOTO :EOF

::
:: For each %FILEXT2% file modify accordingly
::
:ISUPDATE
SET TXTDIR=%1
SET DRVPART=%2
SET PATHPART=%3
SET FILEPART=%4
SET HDR=%TXTDIR%\%FILEPART%
SET TXTFILE=%DRVPART%%PATHPART%%FILEPART%.%FILEXT2%

::
:: For each %FILEXT2% file copy the %MASTERFIL% and rename it to the filename portion of the found file
::
ECHO Updating --) %TXTDIR% %TXTFILE%
COPY /Y %MASTERFIL% %DRVPART%%PATHPART%%FILEPART%.* >NUL

:FINI

Modify as you like as this example only does one level deep from the main %SRCHDIR% directory start point... help yourself.

Cheers, Babe.
Babe Ruth (416)
169204 2003-08-21 10:21:00 wow. bmason (508)
1 2