Better way to find a folder and list contents?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#16 Post by SIMMS7400 » 23 Aug 2016 13:17

Wow - thank you both for your responses!

I'm confirming both work just perfectly!!! Now to decide which one to use :) Perhaps both!

Lastly, is there a way to perform this same action on a remote server?

Otherwise, I'd need to adjust some paths some paths.

For instance, the latest script gives us the following path:

Code: Select all

sql=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER
MSSQL_PATH=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL


In order to copy over *.BAK files from source server, I'd need a way to replace the " : " in MSSQL_PATH to a " $ " symbol.

Is there an easy way to do that? Ultimately, I'd prefer to perform the search on the remote server too as you guys provided above.

Thank you again!!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Better way to find a folder and list contents?

#17 Post by aGerman » 23 Aug 2016 13:31

SIMMS7400 wrote:Lastly, is there a way to perform this same action on a remote server?

Not that easy. Try to use PSEXEC (as you already did). The script has to run on the remote machine in order to query the remote registry and file system.

SIMMS7400 wrote:I'd need a way to replace the " : " in MSSQL_PATH to a " $ " symbol.

Simple substitution
echo %MSSQL_PATH::=$%
But as I said that's probably not an option because you have to determine the MSSQL_PATH on the remote machine (unless you already know it).

Steffen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#18 Post by SIMMS7400 » 23 Aug 2016 13:51

Hi Steffen -

I had a feeling that was the case! I'll leverage PsExec.

Thank you again and have a great day!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Better way to find a folder and list contents?

#19 Post by foxidrive » 23 Aug 2016 18:18

Assuming the user has permissions to access the shares:

this should work for a single server

Code: Select all

@echo off
set "server=\\server1\share"
set "sql="
pushd "%server%"
for /d /r %%b in (MSSQL*.MSSQLSERVER) do set "sql=%%~pnxb"
   if defined sql echo Server "%server%" Found "%sql%"
   if defined sql if exist "%server%%sql%\MSSQL\" echo Server "%server%" Found "%sql%\MSSQL"
popd


and to check multiple servers this code should provide a list of those found, and not found.

Code: Select all

@echo off
set "sql="
for %%a in (

"\\server1\share"
"\\server2\share"
"\\server3\share"
"\\server1\share2"
"\\server1\share3"

) do (
   pushd "%%~a"
   for /d /r %%b in (MSSQL*.MSSQLSERVER) do set "sql=%%~pnxb"
   if defined sql echo Server "%%~a" Found "%sql%"
   if defined sql if exist "%%~a%sql%\MSSQL\"  echo Server "%%~a" Found "%sql%\MSSQL"
   if defined sql if not exist "%%~a%sql%\MSSQL\" echo Server "%%~a" MSSQL not Found
   if not defined sql echo Server "%%~a" MSSQLSERVER not Found
   popd
   set "sql="
  )

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#20 Post by SIMMS7400 » 23 Aug 2016 18:32

Hi Fox -

This is wonderful!

Is there a way to be able to check the remote server for the drive letter, too?

For instance, using your script, I'd need to add a drive letter for it to work:

Code: Select all

@echo off
set "sql="
pushd "\\server\share\C$\" & for /d /r %%b in (MSSQL*.MSSQLSERVER) do set "sql=%%~pnxb" & popd
   if defined sql echo Server "%%~a" Found "%sql%"
   if defined sql if exist "%sql%\MSSQL\" echo  Server "%%~a" Found "%sql%\MSSQL"


Is there anyway to find that letter dynamically? I'd use the drive letter found in the previous code suggestions, but sometimes Microsoft SQL Server is installed on a different Drive on each server.

Thank you, Fox!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Better way to find a folder and list contents?

#21 Post by foxidrive » 24 Aug 2016 01:36

SIMMS7400 wrote:Is there a way to be able to check the remote server for the drive letter, too?
For instance, using your script, I'd need to add a drive letter for it to work:

Code: Select all

pushd "\\server\share\C$\"


Is there anyway to find that letter dynamically?


What do you see when you use this command?

Code: Select all

net view \\computername (or IP address)

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#22 Post by SIMMS7400 » 24 Aug 2016 04:42

Hi Fox -

I'm getting the following output:

There are no entries in the list

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Better way to find a folder and list contents?

#23 Post by foxidrive » 24 Aug 2016 05:25

SIMMS7400 wrote:I'm getting the following output:

There are no entries in the list

The computername of the remote pc you are connecting to should show a list of administrative shares i think.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#24 Post by SIMMS7400 » 24 Aug 2016 13:04

Hi Fox -

Getting back to this now. Ill poke around some and let you know what I discover, thanks!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#25 Post by SIMMS7400 » 24 Aug 2016 13:08

The Net View command actually lists all of the mapped shares to a user account...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Better way to find a folder and list contents?

#26 Post by foxidrive » 25 Aug 2016 00:23

SIMMS7400 wrote:The Net View command actually lists all of the mapped shares to a user account...

Did it show any of the C$ etc shares?

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#27 Post by SIMMS7400 » 25 Aug 2016 01:38

HI Fox -

I have an an additional ask where I need to search for a specific utility (it could be on different dives across each server). Unfortunately, this utility as is others have not been put in the PATH variable.

I'm trying to use the peice of code you porvided earlier in regards to a directory search, but unable to get it work properly:

Code: Select all

set "TD_PATH="
for %%a in (c d e f g h) do if not defined TD_PATH (pushd "%%a:\" & for /f "tokens=* delims=" %%a in ('WHERE /R "%%a:\" tablediff.exe') do set "TD_PATH=%%b" & popd)


Any ideas? Thank you very much!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#28 Post by SIMMS7400 » 25 Aug 2016 01:53

foxidrive wrote:
SIMMS7400 wrote:The Net View command actually lists all of the mapped shares to a user account...

Did it show any of the C$ etc shares?


Hi Fox -

It didn't show any shares as I dont think it had any mapped to it. I'm going to try something out and will report back with findings.

Thanks!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Better way to find a folder and list contents?

#29 Post by SIMMS7400 » 25 Aug 2016 04:33

I've been able to get it - no further action necessary. Unless you my code needs to be cleaned up?

Working as expected though:

Code: Select all

@echo off & setlocal EnableDelayedExpansion
for %%D in (c d e f g) do if exist %%D: (
    for /f "delims=" %%A in ('dir/b/s %%D:\tablediff.exe 2^>nul') do (
        SET TD_CMD_PATH=%%A
    )
)
ECHO "%TD_CMD_PATH%"



"c:\Program Files\Microsoft SQL Server\100\COM\tablediff.exe"

Post Reply