Wednesday, November 16, 2005

Full of scripty goodness

This is one of the batch scripts that I wrote today, basically what it does is log in to a samba server that is doing NT Authentication with Active Directory, takes user input to specify the source folder on the remote server and the target folder on their local machine. To do this it creates a temporary mapped drive on their local machine that is destroyed when the script runs through, its not a persistent connection so if they screw up or kill the script it won't leave the mapped drive sitting on their machine.

The specification I was given for this was:

"I need to copy an entire folder down to my laptop so that if i am travelling it will have the latest documents in it,
I want to be able to specify the directory to copy the files into and I don't care if it overwrites the existing files/folders"

Why they couldn't just copy the files down through Windows Explorer I will never know, but it filled an hour or so and I know lots more about batch scripts than I did before! I probably would have written it so that it didnt ask for any user input apart from the username and password but that would only have worked if the target and source directories were constant, and obviously they won't be.

This would probably have been much simpler to write and much more powerful in Linux (rSync goodness abound!) but this way it minimises the amount of additional software and extra permissions that people need (cygwin on their local machine and a login on the remote linux box).

I might in the future set it up so that the user can specify the remote server as well and navigate through the directory structure.

There are some limitations on its behaviour, I'm still discovering some of the stranger things that it does, i.e. if you are a smart arse and feed it a file within a folder as the target (i.e. work/thisisafile.xls) it will create teh work folder and then another folder beneath it called thisisafile.xls and this folder will contain a file of the same name. This shouldnt be a problem but I would rather it didnt do it. It serves its purpose in the mean time and it may never be modified again but it is something that would annoy me immensely.

:: copydir.bat
:: Written by Daniel McLaughlin
ECHO Mounting Network Share as Local Drive
ECHO Enter your username and close by pressing Enter, F6, Enter.
ECHO You Will Be Prompted for your NT Password
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET USERNAME=%%A
NET USE t: \\Target IP Address OR Hostname\shared dir * /USER:
ECHO Directory Listing for
"Description of the target folder"
dir /A:D /W "t:\
Target folder\"
ECHO Type in the source directory path
ECHO *********************************WARNING*********************************
ECHO * Specifying a null directory will copy the ENTIRE folder *
ECHO * *
ECHO * If you accidentally specify the entire folder press CTRL +C *
ECHO * The Temporary Drive (t:) Created will be removed on your next reboot *
ECHO *********************************WARNING*********************************
ECHO End directory entry by pressing Enter, F6 then Enter again.
:: Only one single command line is needed to receive user input
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET SOURCE=%%A
:: Use quotes if you want to display redirection characters as well
ECHO Type in the ***full*** target directory path
ECHO i.e. c:\documents and settings\user\my documents
ECHO Files will be put in a subfolder with the same name as the target dir.
ECHO End directory entry by pressing Enter, F6 (or Ctrl+Z), Enter.
:: Only one single command line is needed to receive user input
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET TARGET=%%A
ECHO *********************************INFO*********************************
ECHO You Have Specified Source(remote) Folder %SOURCE%
ECHO You Have Specified Target(local) Folder %TARGET%
ECHO If you accidentally specify the wrong folder(s) press CTRL +C
ECHO The Temporary Drive (t:) Created will be removed on your next reboot
ECHO Otherwise

XCOPY /-Y /W /E "t:\
target folder\%SOURCE%\*.*" "%TARGET%\%SOURCE%" /s
ECHO File Copy Successful
ECHO Thank You For Using this utility
net use t: /d

Obviously you wont be an eejit and just copy this into notepad and expect it to run, there are a few parts that you need to edit in order for it to work for you, these are in italics and bold just so that they are easy to spot.

No comments: