Thursday, August 28, 2008

Batch file to back up a folder and add a timestamp

Last week I needed to write a batch file to copy a directory to another location, but back up the existing target directory first, if it already existed.

I needed a unique name for the backup copy, so that the command could be run repeatedly.

Here it is. Notice the /s and /e parameters on the xcopy command. That's to include the subfolders, including the empty ones:

@echo off
ECHO *********************************************
ECHO * This command will:
ECHO *
ECHO * 1. rename the existing MyFolder directory
ECHO *
ECHO * 2. replace it with a fresh MyFolder.
ECHO *
ECHO *
ECHO * Press Ctrl + C to cancel execution.
ECHO *
ECHO **********************************************

PAUSE

set FileDate=%date:~10,4%_%date:~4,2%_%date:~7,2%
set FileTime=%time:~,2%%time:~3,2%%time:~6,2%%time:~9,2%

rename "C:\Target\MyFolder\" "MyFolder %FileDate% %FileTime%"

xcopy "C:\Source\MyFolder\*.*" /s /e "C:\Target\MyFolder\"

Wednesday, August 27, 2008

Enterprise Manager snap-in failed

Once upon a time, there were days when I'd swear I installed & uninstalled MSDE fifty times. And sometimes I would discover, next time I tried to use SQL 2000 Enterprise Manager, that it wouldn't work any more. I'd get this error:

Enterprise Manager snap-in failed

When this happens, the quickest solution is to re-install Client Tools. If the SQL Server 2000 installer is handy, grab it and run a Client Tools Only install.

But often, that doesn't run smoothly. I get this error:

Setup has detected an existing client tools only installation. Please use the maintenance mode to add client components

To get past this, edit the registry.

1. Run regedit.

2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools

3. Change the value of DefClientOnlyInstalled to 0.

4. Re-install Client Tools. It should work, and it will probably set the value back to 1.

Now you can use Enterprise Manager again.

I have also read of another solution, that doesn't involve installing Client Tools again. This is useful if you don't have a SQL Server 2000 CD handy, or don't have access to an installer locally or on a network drive.

1. Run Regedit.

2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\ClientSetup

3. On the right side, right-click and choose New-->String Value.

4. Name the new value SQLPath.

5. Double-click the SQLPath value to edit it. In the Value data field, enter:

C:\Program Files\Microsoft SQL Server\80\Tools

6. Click OK, then close Regedit.

7. At a command prompt, run:

regsvr32 "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqlmmc.dll"

I haven't had a chance to try this since I learned it, because it's been a while since I got a Snap-in failed to initialize error. If you try and it works for you, please let me know. Thanks.

Tuesday, August 26, 2008

ODBC--call failed. [Microsoft][ODBC SQL Server Driver]Timeout expired (#0)

In Microsoft Access, when you open a pass-through query to a SQL Server 2000 or SQL Server 2005 database, sometimes the query times out. Usually for me this happens when the database is very large.

The error message is:

ODBC--call failed. [Microsoft][ODBC SQL Server Driver]Timeout expired (#0)

You can set the timeout interval for ODBC connections in the registry. It is a setting for the Microsoft JET 4.0 engine, which is the database engine that Access 2003 uses.

(When you create a new ODBC DSN, and see all those drivers listed that are version 4.00 and contained in ODBCJT32.DLL -- those drivers are part of JET and likely came with MDAC.)

The QueryTimeout value lives in the registry at

HKEY_LOCAL_MACHINE\Software\Microsoft\Jet\4.0\Engines\ODBC

The default value is 60 seconds. You can increase the timeout setting, or change it to 0 so that queries will never time out.

Follow these steps:

1. Close Access.

2. Start --> Run

3. In the Open field, type regedit and click OK to run the Registry Editor.

4. Expand HKEY_LOCAL_MACHINE. Expand Software. Expand Microsoft.
Continue navigating to Jet\4.0\Engines\ODBC folder.


5. On the right side, double-click the QueryTimeout value to edit it.

6. Under Base, click the Decimal radio button.

7. In the Value data field, enter a new number of seconds. Or, enter 0 to allow unlimited time.


8. Click OK. Exit Registry Editor.

Now you can restart Access and try the query again.

Monday, August 25, 2008

What are the different versions of IIS?

A couple of weeks ago, He Who Is Colorful needed to document technical requirements for a new product. I got an IM from him asking what versions of IIS would be our "supported configurations."

Well, I don't think in terms of versions of IIS; I think in terms of operating systems. I know the app works on XP and 2003 Server, and I know I'm not going to bother testing 2000 until some customer needs it.

I bet I could make it work on Vista, but I'm not going to bother testing that either because I know our installer currently won't create virtual directories on Vista unless IIS is installed with Backwards Compatibility -- because of the new way IIS metabase information is stored. And besides, we're not going to support XP or Vista anyway, because they're not server operating systems, and won't support a realistic number of users hitting IIS at once. Not sure what you get with Vista, but with XP you only get 10 connections.

As for 2008 Server, it'll have the same limitation with our installer as Vista has.

Too late for a long story to be short, but anyway, I had to look up IIS version numbers for each OS. Don't want to have to do it again, so I'm documenting it here:

Windows 2000 Server: IIS 5.0
Windows XP: IIS 5.1
Windows 2003 Server: IIS 6.0
Windows Vista: IIS 7.0
Windows 2008 Server: IIS 7.0 also

Reference: Wikipedia

Thursday, August 14, 2008

How to run all SQL scripts in a folder

Suppose you have a set of SQL scripts for SQL Server that you want to run regularly. Place them all in a single folder, and then you can run a single command file to execute them all.

Here is an example:

for %z in (c:\Scripts\*.sql) do osql -S computername\instancename -U username -P password -d databasename -n -b -i %z

You can also do the same thing, except connect to the SQL Server using Windows Authentication instead of a SQL Server login using the -E parameter instead of -U and -P:

for %z in (c:\Scripts\*.sql) do osql -S computername\instancename -E -d databasename -n -b -i %z

Those dashed parameters (-S, -U, -P, -E, -d, -n, -b, -i) are for OSQL. Case matters. For example, -d is not the same as -D.

You can see all the possible OSQL parameters by running the command OSQL ?. For quick reference, here's a screenshot of what's returned when you run OSQL ?:


Wednesday, August 13, 2008

How do you know what version of Windows you have?

You can find out what version of Windows you have, including what service pack (for example, XP vs. XP SP2) by running the winver command.

1. Click the Start button, then click the Run menu option.

2. In the Open field, type winver and click OK.


Tuesday, August 12, 2008

Couldn't get process information from remote machine

Sometimes our customers install one of our end-user apps on Terminal Server, or Citrix. In these environments, individual users are not typically machine administrators.

There's functionality in our app that requires it to launch some external third-party applications, which we then integrate with -- syncing data back and forth.

Before our app launches the other program, it needs to know if that other program is already running. It looks for an already-running process with the other app's name.

Problem is, on Windows 2003 Server, standard users don't have the authority to look at other processes. We see an error:

Couldn't get process information from remote machine

When this happens, we advise our customer's IT department to add their end users to the Performance Monitor Users Group. This gives them the right to query running processes, without giving them the unwanted rights that making them machine administrators would.