Este código permite navegar por el arbol de directorios del servidor.
<%@ Language="VBScript" %>
<%
'----------------------------------------------------------------------------------------------
' BrowseDir.ASP
'
' By Herman Liu
'
' Note: This code is of no use to readers who do not run browser on host machine. However, it
' may be helpful to others, especially the intranet authors and administrators.
'
' (1) Normal uses: I use this single ASP whenever I am in the browser on host machine and want
' to find and view a particular file. For a web file such as .htm, .html or .asp, I have the
' option of opening it as text or rendering it in the browser. In addition, if the selected
' file is .asp or .vbs, I can choose to open it as text with line numbers optionally displayed
' for debugging purposes.
'
' (2) Possible side-line uses: An engineer used this code and discovered that they could view
' files of other customers of the same ISP (This somehow got his company into quite a trouble.
' His boss requested me to confirm to him that this code was not for hacking purposes and that
' the security methods of his ISP were at fault).
'
' If your home dir name is not "WWWROOT", change the value of mHomeDirName accordingly.
'----------------------------------------------------------------------------------------------
%>
<%
Option Explicit
Response.AddHeader "cache-control", "private"
Response.AddHeader "pragma", "no-cache"
Response.ExpiresAbsolute = #January 1, 1980 00:00:01#
Response.Expires = 0
Response.Buffer=True
Response.Clear
On Error Resume Next
DIM objFSO, objFolder, objFile, objFileTemp
DIM mTestDrive, mDrive, mFolder, mFile
DIM mDriveColl, mSubFolderColl, mFileColl
DIM mCurrDrive, mCurrDir, mCurrFileFilter
DIM mCurrDirString
DIM arrFilePattern()
DIM mExt
DIM mToRedirectTo, OfServerDir
DIM mFilespec
DIM mFileText
DIM mServerName, mHomeDirName
DIM mIndent, mIndentLen
DIM mForFileOnly
DIM mOrigFormat
DIM mRoot
DIM i, j, k
DIM S0, S1
DIM mWebFiles, mDatabaseFiles, mGlobalFiles, mTextFiles, mImageFiles, mAllFiles
DIM mXWebFiles, mXDatabaseFiles, mXGlobalFiles, mXTextFiles, mXImageFiles, mXAllFiles
DIM tmp
mHomeDirName = "WWWROOT"
'-------------------------------------------------------------------------------
' Find out the root dir name. The following will produce a virtual path e.g.
' "C:\Inetpub\wwwroot\DocFiles\Browsedir.asp",
' the first dir after drivename being the root dir name.
mRoot = Request.ServerVariables("PATH_TRANSLATED")
i = InStr(1, mRoot, "\")
IF i > 0 Then
j = i + 1
i = InStr(j, mRoot, "\")
IF i > 0 then
mRoot = MID(mRoot, j, i-j) ' In middle section
Else
mRoot = RIGHT(mRoot, LEN(mRoot) - (j-1)) ' In end section
End If
' We will check whether a filespec has the following string, if yes,
' then it is assumed that the file is within the root dir of server.
mRoot = mRoot & "\"
Else
mRoot = ""
End If
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
' If to run as a page, then redirect
mToRedirectTo = Request.QueryString("ToRedirectTo")
IF mToRedirectTo <> "" Then
' Check and take that portion of path pertaining to a server dir for redirect
If LEN(mRoot) > 0 Then
i = inStr(mToRedirectTo, mRoot)
IF i > 0 Then
' Get rid of drive letter, ":\" and mRoot (e.g. "Inetpub\")
' Below get e.g. "c:\Inetpub\wwwroot\DocFiles\Browsedir.asp",
mToRedirectTo = RIGHT(mToRedirectTo, Len(mToRedirectTo) -Len(mRoot)-3)
' Get rid of mHomeDirName
' Below get e.g. "\DocFiles\Browsedir.asp"
mToRedirectTo = RIGHT(mToRedirectTo, Len(mToRedirectTo) - LEN(mHomeDirName))
Response.clear
Response.redirect mToRedirectTo
Response.end
End If
End If
END IF
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
'Define file patterns. Each ext takes up 4-char length. (html covers shtml).
mWebFiles = " htm/html/ css/ inc/ asp/ js/ jsp/ csh/ cfm/ xml/shtml"
mDatabaseFiles = " sql/ dtq"
mGlobalFiles = " asa"
mTextFiles = " txt/ bat/ log"
mImageFiles = " gif/ jpg/ jpe/jpeg/ bmp" ' IE displays bmp
mAllFiles = "*.*"
mXWebFiles = "(Web) " & mWebFiles
mXDatabaseFiles = "(Database) " & mDatabaseFiles
mXGlobalFiles = "(Global) " & mGlobalFiles
mXTextFiles = "(Text) " & mTextFiles
mXImageFiles = "(Graphics) " & mImageFiles
mXAllFiles = "(All) " & mAllFiles
REDIM arrFilePattern(5)
arrFilePattern(0) = mXWebFiles
arrFilePattern(1) = mXDatabaseFiles
arrFilePattern(2) = mXGlobalFiles
arrFilePattern(3) = mXTextFiles
arrFilePattern(4) = mXImageFiles
arrFilePattern(5) = mXAllFiles
'-------------------------------------------------------------------------------
'Set up the FileSystem objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set mDriveColl = objFSO.Drives
'-------------------------------------
' User has just changed drive (POST)?
'-------------------------------------
mCurrDrive = Request("cboDriveName")
'----------------------------------------------------------
' If empty, user has clicked a listed directory name (GET)?
'----------------------------------------------------------
IF mCurrDrive = "" Then
mCurrDrive = Request.QueryString("CurrDrive")
'-------------------------------
' Then it must be a fresh start.
'-------------------------------
If mCurrDrive = "" Then
mCurrDrive = objFSO.GetdriveName(Server.MapPath("/"))
End If
Else
If Request("cboDriveName") <> Request("CurrDrive") Then
mCurrDrive = Request("cboDriveName")
' Change directory to new drive root accordingly
mCurrDir = mCurrDrive
End If
End If
' Test drive
SET mTestDrive = objFSO.GetDrive(mCurrDrive)
IF NOT mTestDrive.IsReady Then
Response.write "<BR><BR><B> Drive not ready </B>"
Response.write "<FORM NAME='Nothing' ACTION='BrowseDir.asp'>"
Response.write "<INPUT TYPE='SUBMIT' NAME='nothing' VALUE='OK' STYLE='Width=70; Height=22; Font-size:7; font-weight:bold;'>"
Response.write "</FORM>"
Response.end
End If
' If mCurrDir is not set earlier (Change effected if changing drive)
If mCurrDir = "" Then
'----------------------------------------------------------
' User has clicked a listed directory name (GET)?
'----------------------------------------------------------
mCurrDir = Request.QueryString("strPath")
IF mCurrDir = "" Then
mCurrDir = Request.QueryString("CurrDir")
If mCurrDir = "" Then
If Request.QueryString("ComboChange") <> "" Then
' It could be due to change of cboDriveName or cboFileFilter.
' Let use find out
If Request("cboDriveName") <> Request("CurrDrive") Then
' Change current dir to new drive
mCurrDir = Request("cboDriveName")
' Or, alternatively, mCurrDir=objFSO.GetAbsolutePathName(".")
ElseIf Request("cboFileFilter") <> mCurrFileFilter Then
' Don't change dir
mCurrDir = Server.MapPath(".")
Else
mCurrDir = Server.MapPath(".")
End if
Else
' Use directory currently we are in when we start
mCurrDir = Server.MapPath(".")
End If
End If
End If
End If
'------------------------------------------------------------------------
' See if user has clicked a file name. If yes, we show file content only
'------------------------------------------------------------------------
mForFileOnly = Request.QueryString("ForFileOnly")
mFileSpec = Request("FileSpec")
IF mFileSpec = "" Then
mFileSpec = Request.QueryString("FileSpec")
End If
mCurrFileFilter = Request("cboFileFilter")
IF mCurrFileFilter= "" Then
mCurrFileFilter = Request.QueryString("CurrFileFilter")
If mCurrFileFilter = "" Then
mCurrFileFilter = "(All) " & mAllFiles
End If
End If
mOrigFormat = Request("OrigFormat")
%>
<HTML>
<HEAD>
<TITLE>Files in current dir</TITLE>
<META http-equiv="Content-Type"; Content="text/html; Charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<%
'-------------------------------------------------------------------------
' If file of ASP/VBS AND for original format, apply the following and exit
'-------------------------------------------------------------------------
If mOrigFormat <> "" And LEN(mFileSpec) > 5 Then
Response.write "<CENTER>"
Response.write "<FORM NAME='frmOrigFormat' ACTION='BrowseDir.asp?CurrDrive=" & mCurrDrive & "&" & "CurrDir=" & mCurrDir & "&" & "CurrFileFilter=" & mCurrFileFilter & "'" & " METHOD='Post'>"
Response.write "<INPUT TYPE='SUBMIT' NAME='Back' VALUE='Back' STYLE='Width=70; Height=22; Font-size:7; font-weight:bold;'>"
Response.write "</FORM>"
Response.write "</CENTER>"
Response.write "<BR>"
Response.write "<FONT style='font-family:Verdana, Arial; font-size:8pt; font-weight:bold;' Color='#000080'>" & mFileSpec & "</FONT>"
Response.write "<HR>"
Response.write "<FONT style='font-family:Verdana, Arial; font-size:8pt; font-weight:normal;' Color='#000000'>"
Response.Write "<PRE>"
Set objFile = objFSO.OpenTextFile (mFileSpec, 1, False, 0)
DO While NOT objFile.AtEndOfStream
Response.write Server.HTMLEncode(objFile.ReadLine)
Response.write vbCRLF
Loop
Response.Write "</PRE>"
Set objFile = Nothing
Set objFSO = Nothing
Response.end
End If
'----------------------------------------------------------
' If file, in various formats, apply the following and exit
'----------------------------------------------------------
If mForFileOnly <> "" And LEN(mFileSpec) > 5 Then
Response.write "<CENTER>"
Response.write "<FORM NAME='frmDispFile' ACTION='BrowseDir.asp?CurrDrive=" & mCurrDrive & "&" & "CurrDir=" & mCurrDir & "&" & "FileSpec=" & mFileSpec & "&" & "CurrFileFilter=" & mCurrFileFilter & "'" & " METHOD='POST'>"
Response.write "<INPUT TYPE='SUBMIT' NAME='Back' VALUE='Back' STYLE='Width=70; Height=22; Font-size:7; font-weight:bold;'>"
' If ASP/VBS file, allow additional button
If Ucase(Right(mFileSpec, 3)) = "ASP" OR UCASE(right(mfilespec,3))="VBS" Then
Response.write " "
Response.write "<INPUT TYPE='SUBMIT' NAME='OrigFormat' VALUE='Orig Format' STYLE='Width=110; Height=22; Font-size:7; font-weight:bold;'>"
End If
Response.write "</FORM>"
Response.write "</CENTER>"
Response.write "<BR>"
Response.write "<FONT style='font-family:Verdana, Arial; font-size:8pt; font-weight:bold;' Color='#000080'>" & mFileSpec & "</FONT>"
Response.write "<HR>"
Response.write "<FONT style='font-family:Verdana, Arial; font-size:8pt; font-weight:normal;' Color='#000000'>"
Set objFile = objFSO.OpenTextFile (mFileSpec, 1, False, 0)
IF Ucase(Right(mFileSpec, 3))="HTM" OR Ucase(Right(mFileSpec, 4))="HTML" Then
mFileText = objFile.ReadAll
Response.Write mFileText
ElseIf Ucase(Right(mFileSpec, 3))="GIF" OR Ucase(Right(mFileSpec, 3))="JPG" OR Ucase(Right(mFileSpec, 3))="JPE" OR Ucase(Right(mFileSpec, 4))="JPEG" Then
Response.Write "<IMG SRC='" & mFileSpec & "'>"
ElseIf Ucase(Right(mFileSpec, 3))="ASP" Or ucase(right(mfilespec,3))="VBS" Then
i = 1
DO While NOT objFile.AtEndOfStream
Response.write "<B>" & cstr(i) & "</B>"
Response.write " "
Response.write Server.HTMLEncode(objFile.ReadLine)
Response.write "<BR>"
i = i + 1
Loop
Else ' .TXT files
Response.ContentType = "text/plain"
mFileText = objFile.ReadAll
Response.Write Replace(mFileText, vbcrlf, "<BR>")
End If
Set objFSO = Nothing
Set objFile = Nothing
Response.end
End If