Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Didiron

[Resolvido] Como excluir todas as imagens de uma pasta?

Recommended Posts

Boa tarde pessoal.

Eu tenho um código no qual exclui uma imagem de uma pasta a partir de seu caminho físico.

Como esse:

 

Set fso = CreateObject("Scripting.FileSystemObject")
Set fileObject = fso.GetFile(Server.MapPath ("Galeria/"&imagem))
fileObject.Delete
Set fileObject = Nothing

Mas eu gostaria de saber se com FSO tem como excluir todas as imagens da pasta de uma só vez.

Alguém poderia me ajudar?

Abraços.

Compartilhar este post


Link para o post
Compartilhar em outros sites

existe um código, tipo explorer, onde você tem total acesso aos arquivos e pastas, podendo também selecionar vários

Compartilhar este post


Link para o post
Compartilhar em outros sites

Como estou bonzinho hoje fiz uma função simples aqui para tu, lembre-se de da nothing nos objetos criados e declarar as variáveis, pois essa parte eu fiquei com preguiça rsrsrs

 

<%
Function deletarImg(pasta) 

Set DEL = CreateObject("Scripting.FileSystemObject")

If DEL.FolderExists(pasta) Then

    Set PArq = DEL.GetFolder(pasta)
	
    quantidade = PArq.Files.Count

    For Each Arq In PArq.Files
        Arq.Delete True
    Next
	
    If quantidade = 0 Then
        Response.Write("Nenhuma imagem na pasta para excluir")
    Else
        Response.Write("Foram excluídas " & quantidade & " imagens")
    End If

End If

End Function

call deletarImg("C:\Inetpub\site\img")
%>

Compartilhar este post


Link para o post
Compartilhar em outros sites

olha este exemplo, onde você pode selecionar ou deselecionar tudo e apagar

 

<%    ' Declaration ###################################################################

Option Explicit

Response.AddHeader "Pragma", "No-Cache"                'No cache page
Response.CacheControl = "Private"                    'No cache page
Server.scripttimeout = 300                            'Time out (sec)

Dim objFSO, objFolder, objFile, objRS                ' Object declaration
Dim FNExt, mySelf, fullPath, pathURL, onlyPath, imgDir ,CurrentPath                               ' Directory info
Dim sortItem, sortOrient                            ' Sort item
Dim sngTimeStart, sngTimeFinish                        ' Timer
Dim FileSizeTotal, FileCountTotal                    ' Total file size
Dim fileDeleteList

sngTimeStart = timer                                ' Start timer

imgDir = "icon"                                                          ' Icons directory to be hide
'fullPath = "C:\"
'fullpath é /webdir22/index.asp
fullPath = LCase(Request.ServerVariables("SCRIPT_NAME"))                        ' myself.ASP with full path
mySelf = Right(fullPath, Len(fullPath) - InStrRev(fullPath, "/", -1, 1)) ' myself.ASP
'onlyPath é /webdir22/ 
'onlyPath = StripDirectory(fullPath)
 ' onlyPath ="/wwwroot/"                                
'onlyPath = "C:/inetpub/wwwroot" 
pathURL = LCase("http://" & Request.ServerVariables("SERVER_NAME") & onlyPath)  ' URL name without myself.ASP

' Get posted data or posted links variables
If Request.Form.Item("fileDelete") = "yes" then Response.Write deleteFiles
If Request.QueryString("sortItem") = "" then
    sortItem = "fileType"
Else
    sortItem = Request.QueryString("sortItem")        ' Sort item
End if
If Request.QueryString("sortOrient") = "" then
    sortOrient = "asc"
Else
    sortOrient = Request.QueryString("sortOrient")    ' Sort ascending or descending
End if

' Setup related objects
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")



'Set objFolder = objFSO.GetFolder(Server.MapPath(onlyPath))
Set objFolder = objFSO.GetFolder( "C:\inetpub\wwwroot\")
Set objRS = CreateObject("adodb.recordset")

' Prepare ADODB field
objRS.fields.append "FileIcon",200,255
objRS.fields.append "FileName",200,255
objRS.fields.append "FileType",200,255
objRS.fields.append "FileSize",3,255
objRS.fields.append "FileCreated",7,255
objRS.fields.append "FileModified",7,255
objRS.open

' Directory listing
For Each objFile In objFolder.SubFolders
    If (InStr(1, objFile, imgDir, 1) = 0) Then        ' Exclude my icon folder
        objRS.addnew
        objRS.fields("FileIcon") = getIcon("dir")
        objRS.fields("FileName") = objFile.Name
        objRS.fields("FileType") = objFile.Type
        objRS.fields("FileSize") = objFile.Size
        objRS.fields("FileCreated") = objFile.DateCreated
        objRS.fields("FileModified") = objFile.DateLastModified
        objRS.update
    End If
Next

' Files listing
FileCountTotal = 0
FileSizeTotal = 0
For Each objFile In objFolder.Files
    FNExt = LCase(Right(objFile.Name, Len(objFile.Name) - InStrRev(objFile.Name, ".", -1, 1)))
    if ( _
      (LCase(objFile.Name) <> mySelf) and _
      (FNExt <> "txt") and _
      (FNExt <> "bak")) _
      then
        objRS.addnew
        objRS.fields("FileIcon") = getIcon(objFile.Name)
        objRS.fields("FileName") = objFile.Name
        objRS.fields("FileType") = objFile.Type
        objRS.fields("FileSize") = objFile.Size
        objRS.fields("FileCreated") = objFile.DateCreated
        objRS.fields("FileModified") = objFile.DateLastModified
        objRS.update
        FileSizeTotal = FileSizeTotal + objFile.Size
        FileCountTotal = FileCountTotal + 1
    end if
Next

objRS.Sort = sortItem & " " & sortOrient            ' Sort the listing
%>

<HTML>
<HEAD>
<TITLE>Conteudo de <% = onlyPath %></TITLE>
<STYLE>
BODY {
    color:black;
    font:9pt Tahoma,Verdana,Arial}
TD {
    border-bottom:1pt solid silver;
    font:8pt Tahoma,Verdana,Arial}
TH {
    font:9pt Tahoma,Verdana,Arial}
A:link {
    color:black;
    font-weight:bold;
    text-decoration:none}
A:hover {
    color:royalblue;
    font-weight:bold;
    text-decoration:underline}
A:visited {
    font-weight:bold;
    text-decoration:none}
A.headerLink {
    color:white;
    font-weight:bold;
    text-decoration:none}
INPUT.btnDel {
    color:white;
    font-weight:bold;
    font-size:10px; 
    background-color:red;
    width:110px;
    height:19px}
</STYLE>
<script language="JavaScript">
function checkAll(val) {
    dml=document.fileList;
    len = dml.elements.length;
    var i=0;
    if (val == 0 || val == 1) {
        for( i=0 ; i<len ; i++) {
            if (dml.elements[i].name=='fileSelect'){
                dml.elements[i].checked=val;}}}
    if (val == 3) {
        for( i=0 ; i<len ; i++) {
            if (dml.elements[i].name=='fileSelect'){
                dml.elements[i].checked=!dml.elements[i].checked;}}}}

</SCRIPT>
</HEAD>
<BODY>
<FORM name=fileList method=post action='<% = mySelf %>?sortOrient=<% = sortOrient %>&sortItem=<% = sortItem %>'>
<!-- <FORM name=fileList action=''> -->
<input type=hidden name=fileDelete value="yes">

<CENTER>

<!-- Directory path -->
<TABLE BORDER="0" CELLSPACING="2" CELLPADDING="5" WIDTH="100%">
    <TR BGCOLOR="Navy">
        <TD Align=LEFT><FONT COLOR="White"><B>
                <A class="headerLink" HREF='<% = onlyPath %>'>  Conteudo de <% = onlyPath %></A></B></FONT></TD></TR></TABLE>

<!-- Directory header -->
<TABLE VALIGN=CENTER BORDER="0" CELLSPACING="2" CELLPADDING="2" WIDTH="100%">
    <TR VALIGN=CENTER BGCOLOR="Navy">
        <TH WIDTH="25%" Valign=center Align=center><FONT COLOR="White"><B>
            <%If sortItem = "fileName" Then Response.Write " <IMG Valign=center SRC=./" & imgDir & "/" & sortOrient & ".gif>"%>
            <A class="headerLink" HREF='<% = mySelf %>?sortOrient=<%if (sortItem="fileName" and sortOrient="asc") then Response.Write "desc" else Response.Write "asc"%>&sortItem=fileName'>Nome do Arquivo</A></B></FONT></TH>
        <TH WIDTH="25%" Valign=center Align=center><FONT COLOR="White"><B>
            <%If sortItem = "fileType" Then Response.Write " <IMG SRC=./" & imgDir & "/" & sortOrient & ".gif>"%>
            <A class="headerLink" HREF='<% = mySelf %>?sortOrient=<%if (sortItem="fileType" and sortOrient="asc") then Response.Write "desc" else Response.Write "asc"%>&sortItem=fileType'>Tipo de Arquivo</A></B></FONT></TH>
        <TH WIDTH="10%" Valign=center Align=center><FONT COLOR="White"><B>
            <%If sortItem = "fileSize" Then Response.Write " <IMG SRC=./" & imgDir & "/" & sortOrient & ".gif>"%>
            <A class="headerLink" HREF='<% = mySelf %>?sortOrient=<%if (sortItem="fileSize" and sortOrient="asc") then Response.Write "desc" else Response.Write "asc"%>&sortItem=fileSize'>Tamanho de Arquivo</A></B></FONT></TH>
        <TH WIDTH="20%" Valign=center Align=center><FONT COLOR="White"><B>
            <%If sortItem = "fileCreated" Then Response.Write " <IMG SRC=./" & imgDir & "/" & sortOrient & ".gif>"%>
            <A class="headerLink" HREF='<% = mySelf %>?sortOrient=<%if (sortItem="fileCreated" and sortOrient="asc") then Response.Write "desc" else Response.Write "asc"%>&sortItem=fileCreated'>Data Criação</A></B></FONT></TH>
        <TH WIDTH="20%" Valign=center Align=center><FONT COLOR="White"><B>
            <%If sortItem = "fileModified" Then Response.Write " <IMG SRC=./" & imgDir & "/" & sortOrient & ".gif>"%>
            <A class="headerLink" HREF='<% = mySelf %>?sortOrient=<%if (sortItem="fileModified" and sortOrient="asc") then Response.Write "desc" else Response.Write "asc"%>&sortItem=fileModified'>Data Modificação</A></B></FONT></TH></TR></TABLE>

<!-- Directory listing -->
<TABLE BORDER="0" CELLSPACING="5" CELLPADDING="0" WIDTH="100%">
<% getDirectory(objRS) %>
</TABLE>

</CENTER>
<BR>
<BR>

<!-- Delete Option -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
    <TR BGCOLOR="White">
        <TD Align=LEFT><FONT COLOR="Black"><B>
        <a href="javascript:checkAll(1)">Selecione</a> ou <a href="javascript:checkAll(0)">deselecione</a> todos </B>
        <INPUT class=btnDel name=btnDelete type=button value="Delete">
        (<a href="javascript:checkAll(3)">Reverse</A>)
        </FONT></TD></TR></TABLE>
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
    <TR BGCOLOR=White>
        <TD Align=LEFT><FONT COLOR="Black">

<% ' Footer #####################################################################
    Response.Write "<BR><B>Total file count: " & FileCountTotal & " file(s)</B><BR>"
    if FileSizeTotal < 1024 Then
        Response.Write "<B>Total file size: " & FileSizeTotal & " Bytes" & "</B><br>"
    ElseIf FileSizeTotal < 1048576 Then
        Response.Write "<B>Total file size: " & Round(FileSizeTotal/1024, 2) & " KB" & "</B><br>"
    Else
        Response.Write "<B>Total file size: " & Round((FileSizeTotal/1024)/1024.1, 2) & " MB" & "</B><br>"
    End if    
    response.write "<br>" & "<i> Last refreshed: " & now() & "<br>"
    sngTimeFinish = timer
    response.write " Refresh time was " & round(sngTimeFinish - sngTimeStart, 2) & " seconds.</i>"
%>
        </FONT></TD></TR></TABLE>

<BR>
<A HREF="../">Voltar</A>

<!-- Copyright ##################################################################### -->
<BR><BR>
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
    <TR BGCOLOR=White>
                <TD Align=CENTER><FONT COLOR="Black">
<br>Copyright © 2002.  Version 2.2<br>
</FONT></TD></TR></TABLE>

</FORM>
</BODY>
</HTML>

<script language="VBScript">
sub btnDelete_onClick()
    strConfirm = msgbox("Are you sure to delete the file?",vbYesNo,"Confirm")
    if strConfirm = 6 then    
        urlExec = "<% = mySelf %>?sortOrient=<% = sortOrient %>&sortItem=<% = sortItem %>"
        fileList.action = urlExec
        fileList.submit
    end if
end sub
</SCRIPT>

<% ' Functions ###################################################################
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

function StripDirectory(ByVal asPath)
    if asPath = "" Then Exit function
    if InStr(1, asPath, "/") = 0 Then Exit function
    asPath = Left(asPath, InStrRev(asPath, "/"))
    StripDirectory = asPath
End function

Function getIcon(withExtension)
    If withExtension <> "dir" Then withExtension = LCase(Right(withExtension, Len(withExtension) - InStrRev(withExtension, ".", -1, 1)))
    Select Case withExtension
        Case "asp":    withExtension = "asp"
        Case "bmp": withExtension = "bmp"
        Case "cd" : withExtension = "cd"
        Case "css": withExtension = "css"
        Case "dir":    withExtension = "dir"
        Case "doc":    withExtension = "doc"
        Case "gif":    withExtension = "gif"
        Case "htm", "html":    withExtension = "htm"
        Case "jpg", "jpeg", "jpe": withExtension = "jpg"
        Case "js" : withExtension = "js"
        Case "mdb": withExtension = "mdb"
        Case "mp3": withExtension = "mp3"
        Case "pdf": withExtension = "pdf"
        Case "psd": withExtension = "psd"
        Case "ppt": withExtension = "ppt"
        Case "txt": withExtension = "txt"
        Case "wav": withExtension = "wav"
        Case "xls": withExtension = "xls"
        Case "zip": withExtension = "zip"
        Case Else : withExtension = "misc"
    End Select
    getIcon = "<IMG SRC=""./" & imgDir & "/" & withExtension & ".gif"" BORDER=0>"
End Function

Function deleteFiles()
    Dim fDel, fList, objFSOdel, i
    fileDeleteList = Request.Form("fileSelect").Count
    For i = 1 To fileDeleteList
        fList = Request.Form.Item("fileSelect")(i)
        Set objFSOdel = Server.CreateObject("Scripting.FileSystemObject")
        Set fDel = objFSOdel.GetFile(Server.MapPath(fList))
        On Error Resume Next
        fDel.Delete
        If Err.number <> 0 then
                Response.Write Err.Description & " - " & fList & "<BR>"
                fileDeleteList = fileDeleteList - 1
        Else
                Response.Write fList & "<BR>"
        End if
        On Error Goto 0
    Next
    Set objFSOdel = Nothing
    deleteFiles = "<BR><B> " & fileDeleteList & " file(s) deleted successfully.</B><BR>"
End Function

Sub getDirectory(objRSet)
    Do While Not objRSet.eof
        Response.Write "<TR><TD WIDTH=25% ALIGN=Left>" 
        If objRSet.fields("FileType") = "File Folder" Then 
            Response.Write "<INPUT TYPE=checkbox NAME=fileSelect VALUE='" & objRSet.fields("FileName") & "' disabled> "
        Else
            Response.Write "<INPUT TYPE=checkbox NAME=fileSelect VALUE='" & objRSet.fields("FileName") & "'> "
        End If
        Response.Write objRSet.fields("FileIcon") 
        Response.Write " <A HREF='" & onlyPath & objRSet.fields("FileName") & "'>" & objRSet.fields("FileName") & "</A></TD>"
        Response.Write "<TD WIDTH=25% ALIGN=Left>" & objRSet.fields("FileType") & "</TD>"
        Response.Write "<TD WIDTH=10% ALIGN=Right>" ' Formatting the file size
        if objRSet.fields("FileSize") < 1024 Then
            Response.Write objRSet.fields("FileSize") & " Bytes"
        ElseIf objRSet.fields("FileSize") < 1048576 Then
            Response.Write Round(objRSet.fields("FileSize")/1024, 2) & " KB"
        Else
            Response.Write Round((objRSet.fields("FileSize")/1024)/1024.1, 2) & " MB"
        End if    
        Response.Write "</TD>"
        Response.Write "<TD WIDTH=20% ALIGN=Right>" & objRSet.fields("FileCreated") & "</TD>"
        Response.Write "<TD WIDTH=20% ALIGN=Right>" & objRSet.fields("FileModified") & "</TD>"
        Response.Write "</TR>"
        objRSet.movenext
    Loop    
    objRSet.close
End Sub
%>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.