Question

I need to check a file status (existing? or last modified date) on multiple remote Windows servers (in LAN). The files are accessible via UNC path. The remote PCs need a user name and password. It's best to be some sort of script.

I am not system admin, but asked to do this by my boss because each of these PCs are host of SQL Server and the file is a backup of database. I am a SQL Server database developer. I was trying to do it using T-SQL, but just found it not geared to do this (although maybe doable).

Was it helpful?

Solution

Create a batch file like (say check.bat):

@echo off

set list=server1 server2 server3 server4 serverN

for %%s in (%list%) do (
    echo Logging in to %%s
    net use \\%%s\shared /user:mydomain\myuser password    

    echo Checking file existence on %%s
    if exist \\%%s\shared\file.txt (
        echo File exist on %%s
    ) else (
        echo File does NOT exist on %%s
    )

    echo Logging out
    net use \\%%s\shared /delete
)

For details on the net use commands, see the accepted answer for question Mapping a network drive without hardcoding a drive letter in a batch file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top