sjumu
asked on
How can I use Powershell or sql to check the existence of files from the file paths in a database table
I have a database table with a column called pathnames which stores the logical path of files managed by this DB.
I can create a temporary table with the physical paths of the files with something like this
select 'E:\NAS\Store\'+pathnames in #checkfiles from tbfilepath
How can I validate the existence of the files in windows using the file paths from the table contents or query results
using Powershell or SQL. I want to avoid using XP_CMDSHELL
Any ideas will be appreciated
Regards
SJ
I can create a temporary table with the physical paths of the files with something like this
select 'E:\NAS\Store\'+pathnames in #checkfiles from tbfilepath
How can I validate the existence of the files in windows using the file paths from the table contents or query results
using Powershell or SQL. I want to avoid using XP_CMDSHELL
Any ideas will be appreciated
Regards
SJ
ASKER
I want to test that the files actually exist after failed drive caused the data to be restored . So I want a list of file that do not exist in comparision to the file locations specified in the pathnames stored in the DB.
Are all these paths locally on one server ?
Or are they on separate servers ?
What are the column headers in the db for this particular table ?
Or are they on separate servers ?
What are the column headers in the db for this particular table ?
ASKER
These paths are local so my temp table will have a result like
E:\NAS\Store\2014\05\file1 .txt
The only relevant column is pathnames which holds the last part of the result 2014\05\file1.txt
E:\NAS\Store\2014\05\file1
The only relevant column is pathnames which holds the last part of the result 2014\05\file1.txt
Something like this should work for you:
$query="Select column from table"
$connection = new-object system.data.sqlclient.sqlconnection( "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=SSPI;”)
$adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)
$table = new-object system.data.datatable
$adapter.Fill($table) | out-null
$dirArray = @($table | select -ExpandProperty columnn)
return @($dirArray)
foreach ($dirname in $dirArray)
{
if (!(Test-path $dirname)) {write-host "Directory matching $dirname is not present on computer ..." -fore Red}
elseif (Test-Path $dirname) {write-host "Directory matching $dirname was found on computer ..." -fore Green}
}
$connection.close()
ASKER
Hi becraig,
Thanks for this...I think I am getting there. The script work well but does not seem to test the output. See below my implementation
$query="select pathnamesX from checkfiles "
$connection = new-object system.data.sqlclient.sqlc onnection( "Data Source=SQLLAB1;Initial Catalog=DBMAR1;Integrated Security=SSPI;”)
$adapter = new-object system.data.sqlclient.sqld ataadapter ($query, $connection)
$table = new-object system.data.datatable
$adapter.Fill($table) | out-null
$dirArray = @($table | select -ExpandProperty pathnamesX)
return @($dirArray)
foreach ($dirname in $dirArray)
{
if (!(Test-path $dirname)) {write-host "Directory matching $dirname is not present on computer ..." -fore Red}
elseif (Test-Path $dirname) {write-host "Directory matching $dirname was found on computer ..." -fore Green}
}
$connection.close()
Output
E:\NAS\Store\2008\04\24\22 22\22.txt
E:\NAS\Store\2008\04\24\22 22\23.doc
E:\NAS\Store\2008\04\24\25 99\1005.do c
E:\NAS\Store\2008\04\24\25 99\1002.do c
I am I missing something?
Thnaks again
SJ
Thanks for this...I think I am getting there. The script work well but does not seem to test the output. See below my implementation
$query="select pathnamesX from checkfiles "
$connection = new-object system.data.sqlclient.sqlc
$adapter = new-object system.data.sqlclient.sqld
$table = new-object system.data.datatable
$adapter.Fill($table) | out-null
$dirArray = @($table | select -ExpandProperty pathnamesX)
return @($dirArray)
foreach ($dirname in $dirArray)
{
if (!(Test-path $dirname)) {write-host "Directory matching $dirname is not present on computer ..." -fore Red}
elseif (Test-Path $dirname) {write-host "Directory matching $dirname was found on computer ..." -fore Green}
}
$connection.close()
Output
E:\NAS\Store\2008\04\24\22
E:\NAS\Store\2008\04\24\22
E:\NAS\Store\2008\04\24\25
E:\NAS\Store\2008\04\24\25
I am I missing something?
Thnaks again
SJ
Hmmm let's try moving the loop outside the sql block.
I cannot really test this at the moment unfortunately.
I cannot really test this at the moment unfortunately.
$query="select pathnamesX from checkfiles "
$connection = new-object system.data.sqlclient.sqlconnection( "Data Source=SQLLAB1;Initial Catalog=DBMAR1;Integrated Security=SSPI;”)
$adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)
$table = new-object system.data.datatable
$adapter.Fill($table) | out-null
$dirArray = @($table | select -ExpandProperty pathnamesX)
$connection.close()
$dirarray | % {
if (!(Test-path$_)) {write-host "Directory matching $_ is not present on computer ..." -fore Red}
elseif (Test-Path $_) {write-host "Directory matching $_ was found on computer ..." -fore Green}
}
ASKER
Becraig,
Apologies for the delay in reponse due to illness..... the script works afer you moved the loop outside the SQL. This correctly lists all tested file locations. How can I change the script output to only write when the file location is not found?
many thanks
SJ
Apologies for the delay in reponse due to illness..... the script works afer you moved the loop outside the SQL. This correctly lists all tested file locations. How can I change the script output to only write when the file location is not found?
many thanks
SJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Becraig,
thanks very much... thi is exactly what i need!!
thanks very much... thi is exactly what i need!!
Basically the result of this kind of check is useless, cause it is async.