1

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).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
thotwielder
  • 1,563
  • 7
  • 44
  • 83

1 Answers1

1

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.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks for the reply. is myuser the username, and password the password ? And you mean save this to a .cmd file then run? The screen disappeared immediately after run, so how can I know the result? I am really rookie on this – thotwielder Apr 16 '13 at 11:40
  • Username/password: Yes. Save to .cmd: Yes. Disapearing: You can put `pause` command at the end. But you probably want to do do some action, when file exists or not (instead of `echo File exists`). You have not specified this. Please, accept the answer, if it helped you. Thanks. – Martin Prikryl Apr 16 '13 at 11:49
  • I need it to loop through all remote servers so that I can get a list of some kind of which server has the file or not. – thotwielder Apr 16 '13 at 12:40
  • I have updated the answer to show how to re-use the batch for many servers. – Martin Prikryl Apr 16 '13 at 12:49
  • do you have some kind of script for the looping? There are more than one hundred servers. I really think there should be some way to do a looping of some kind? Can you tell me where I can find the reference to this (not sure the name) windows scripting? I will mark it as answer. – thotwielder Apr 16 '13 at 12:51
  • I have updated the answer to use looping. But you need to have a list of servers somewhere anyway. Or are the server numbered? – Martin Prikryl Apr 16 '13 at 13:07