PDA

View Full Version : Batch/NT Script file creation - can you do this for me?



Mike
21-08-2003, 04:14 PM
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
21-08-2003, 04:18 PM
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.

-=JM=-
21-08-2003, 05:22 PM
Well I've got as far as dir /s *.shp just need to get it to do something with it.

antmannz
21-08-2003, 06:35 PM
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?

Mike
21-08-2003, 08:10 PM
> 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.

bmason
21-08-2003, 08:12 PM
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.

Mike
21-08-2003, 08:26 PM
> 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.

bmason
21-08-2003, 09:08 PM
http://sources.redhat.com/cygwin/


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.

Babe Ruth
21-08-2003, 09:16 PM
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.

bmason
21-08-2003, 09:21 PM
wow.

antmannz
21-08-2003, 09:26 PM
Hmmm. I was working on a vbs script with an ini file to specify parameters. But I think I'll stop unless you want me to continue, Mike.

Mike
21-08-2003, 09:29 PM
> Hmmm. I was working on a vbs script with an ini file
> to specify parameters. But I think I'll stop unless
> you want me to continue, Mike.

I dunno yet - I have to wait until I get to work tomorrow and try it...

Mike.

Mike
21-08-2003, 09:32 PM
> wow.

wow is right :)

Mike.

Mike
21-08-2003, 09:38 PM
Thanks Babe - I'll test that out at work tomorrow and see how we go...

you're a legend :D

Mike.

-=JM=-
21-08-2003, 10:21 PM
You might also be able to use the "sendto" menu. Just select multiple folders and send to the script. That is a *might* though.

Mike
22-08-2003, 08:49 AM
> Thanks Babe - I'll test that out at work tomorrow and
> see how we go...
> you're a legend :D
> Mike.

Babe you're brilliant :D That's saved me hours :) - especially because I'd miscounted my folders... all up there were 145 folders (don't ask me how I miscounted... I'm not going to tell you!)

Cheers Babe.

Mike.

Babe Ruth
22-08-2003, 08:56 AM
Mike,

No problem. Glad to help...

Cheers, Babe.

Susan B
22-08-2003, 09:44 AM
Hey Babe, Mike's shout in Chat tonight, don't you reckon? ]:)

:p :D