distributedlife

passionate about everything

Does Remote Directory Exist? Published by Ryan Boucher @ 11:55 pm

This code snippet is written in Vanilla C and HP Service Test C which adds a bunch of methods that make string manipulation and date type conversion just a little bit less painful. If any of you remember Vanilla C it was about as much fun as putting a pink sock in with the wedding dress.

Chdir doesn’t work with UNC paths so we need another option. I already had the SysInternals tools installed so it made sense to use them.

If you want to know what is going on: lr_save_string takes a char* pointer and drops it into a HP Parameter and lr_eval_string returns a char* pointer evaluting items in brackets into their values. It’s much simpler than mucking about with sprintf.


//Determine whether a remote directory exists
// - relies on psexec and xcopy and therefore Windows dependant
// - returns:
//      0 if path found
//      not zero if path is not found
int RemoteDirectoryExists (const char* server, const char* path)
{
    long FileHandle = 0 ;
    int Result = 1 ;

    lr_save_string (server, "__RemoteDirectoryExists_Server") ;

    //create batch file to be run on remote server that tries to chdir to the path
    FileHandle = fopen("dir.bat", "w") ;
    if (!FileHandle)
    {
        lr_error_message ("Unable to create the \"dir.bat\" file.") ;

        return Result ;
    }

    fprintf (FileHandle, "chdir %s", path) ;
    fclose (FileHandle) ;

    //xcopy batch file to remote server
    system (lr_eval_string ("xcopy /Y /Q \"dir.bat\" \\\\{__RemoteDirectoryExists_Server}\\c$\\temp\\*")) ;

    //psexec batch on remote
    Result = system (lr_eval_string ("psexec.exe \\\\{__RemoteDirectoryExists_Server} C:\\temp\\dir.bat")) ;

    return Result ;
}

Fairly simple really. It creates the batch file. Xcopies it to the remote server and then runs it locally via psexec. I use this in my automated deployment tests to check whether the install path has been removed after uninstall.

My Mug Ryan Boucher is a Software Inquisitor and is passionate about it. You can find a whole raft of articles and anecdotes about software testing and other topics he gets excited about.
Tags , , , , , , , , , ,