Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question [SOLVED]Deleting temp files every day

    Hi guys!

    today i'm working on a VM with Windows Server 2008 R2, the role is File Services; File Server and File Server resource Manager are installed.
    I want to delete all Thumbs.db and other Temporary files , from the folder Test that i've created, not only today but, if possible, every day.
    What's the right way to do this?
    Thanks
    A

    My english is still rusty...wtf
    Last edited by Albiz; 2013-11-26 at 08:13. Reason: [SOLVED]

  2. #2
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,621
    Thanks
    2
    Thanked 627 Times in 560 Posts
    Create a batch file to do what you want and then use Task Scheduler to schedule a task to execute the batch file at a specific time.

    Joe

  3. #3
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Lightbulb

    Code:
    $Dir="C:\Test"
    foreach( $In in(Get-ChildItem $Dir -Recurse){
    if(!(Get-ChildItem -Path $In.FullName)){
    echo $IN.FullName;
    Remove-Item $IN.FullName
    }
    }
    Power shell...
    how can i schedule it?
    Thanks
    A

  4. #4
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,621
    Thanks
    2
    Thanked 627 Times in 560 Posts
    Click the Start button, type in Task Scheduler in the search bar, select Task Scheduler. Once the applet initializes create a new task. I use "Create task" and not "Create Basic task" as "Create task" has more options and flexibility. You just need to fill in the various tabs. See Using Windows Task Scheduler to execute Powershell Script frequently for a discussion.

    Joe

  5. The Following User Says Thank You to JoeP517 For This Useful Post:

    Albiz (2013-11-19)

  6. #5
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks it works!
    Last thing i swear... i need to do a report for duplicate files. The same file with different names must be recognized as a duplicate. How can i do this?
    Thanks
    A

  7. #6
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Albiz View Post
    Code:
    $Dir="C:\Test"
    foreach( $In in(Get-ChildItem $Dir -Recurse){
    if(!(Get-ChildItem -Path $In.FullName)){
    echo $IN.FullName;
    Remove-Item $IN.FullName
    }
    }
    There are some folders, like 00-Doc, starting with numbers. Those MUST NOT BE DELETED.
    I think this
    Code:
    if(Folder start with(list)){skip}
    How can i write it for powershell?
    $Dir="C:\Test"
    $Et1="0"
    $Et2="9"
    foreach( $In in(Get-ChildItem $Dir -Recurse)){ #per ogni cartella in $Dir
    if(!(Get-ChildItem -Path $In.FullName)){ #se non hai figli
    $Sav=Split-Path( $In ) -leaf; #prendi il nome della cartella
    if(!($Sav.StartsWith($Et1)) -and (!($Sav.StartsWith($Et2)))){ #se non fa parte delle eccezioni
    echo $In.FullName; #stampa
    #Remove-Item $In.FullName
    }
    }
    }
    Maybe...
    Last edited by Albiz; 2013-11-19 at 09:14. Reason: [SOLVED]

  8. #7
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Arrow Coding... again

    Code:
    #qui genero la funzione di ricerca dei file duplicati
    function Get-Double {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true,
                       Position=0,
                       ValueFromPipeline=$true,
                       ValueFromPipelineByPropertyName=$true)]
            [System.String]
            [Alias('PSPath')]
            $Path
        )
    
        if ((Test-Path -Path $Path) -eq $false) {
            throw New-Object System.IO.FileNotFoundException($Path)
        }
    
        try {
            $item = Get-Item -Path $Path -Force -ErrorAction Stop
        } catch {
            throw
        }
    
        if ($item -isnot [System.IO.FileInfo]) {
            throw New-Object System.ArgumentException('Specified path must refer to a File object.','Path')
        }
    
        try {
            $bytes = [System.IO.File]::ReadAllBytes($item.FullName)
        } catch {
            throw
        }
    
        $md5Hash = [System.Security.Cryptography.MD5]::Create()
        $hashBytes = $md5Hash.ComputeHash($bytes)
    
        $sb = New-Object System.Text.StringBuilder
    
        foreach ($byte in $hashBytes) {
            $sb.Append($byte.ToString("x2")) | Out-Null
        }
    
        Write-Output $sb.ToString()
    }
    #non ho capito come lavora ma so che lavora...
    
    
    $files = @{}
    $folder = 'C:\Test\'  # Il percorso su cui lavorare
    
    Get-ChildItem -Path $folder -Force -Recurse |  # Prende i figli della cartella
    Where-Object { $_ -is [System.IO.FileInfo] } |
    ForEach-Object {
        try {
            $hash = $_ | Get-Double #esegue la funzione per ogni figlio
        } catch {
            Write-Error -ErrorRecord $_
            return   # Can look confusing, but ForEach-Object "loops" use return, not continue
        }
    
        if ($files.ContainsKey($hash) -eq $false) {
            $files[$hash] = New-Object 'System.Collections.Generic.List[System.String]'
        } else {
            # In this sample, I'm just identifying duplicates by adding all their names to a list.
            # You can have the script delete duplicates here, if you wish, but keep in mind that
            # just because two files have identical hashes, that's not a guarantee that they have
            # identical contents;  it is possible for two different inputs to produce the same
            # hash code.
        }
    
        $files[$hash].Add($_.FullName)
    }
    
    # For example purposes, output the list of duplicates (since the script didn't delete them)
    
    foreach ($entry in $files.GetEnumerator()) {
        if ($entry.Value.Count -gt 1) {
            $drives = foreach ($path in $entry.Value) {
                $prophash = @{
                Nome= split-path $path -leaf
                Percorso = $path
                }
    
            #create a new object from the property hash
            New-Object PSObject -Property $prophash
            }
            #embed the style in the html header
    $head = @'
    <Title>Double Report</Title>
    <style>
    body 
    { 
     background-color:#FFFFFF;
     font-family:Verdana;
     font-size:12pt; 
    }
    td, th 
    { 
     border:1px solid blue; 
     border-collapse:collapse; 
    }
    th 
    {
     color:white;
     background-color:green; 
    }
    table, tr, td, th { padding: 5px; margin: 0px }
    table { margin-left:50px; }
    </style>
    '@
    
    #create an xml document from the HTML fragment
    [xml]$html = $drives | ConvertTo-Html -fragment
    
    
    #check each row, skipping the TH header row
    for ($i=1;$i -le $html.table.tr.count-1;$i++) {
      $class = $html.CreateAttribute("class")
    }
    
    #create the final report from the innerxml which should be html code
    $body = @"
    <H1>Double Report for $Folder</H1>
    $($html.innerxml)
    "@
    
    #put it all together
    ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i>" -body $body | 
    Out-File "C:\Test\Double.html" -Encoding ascii
    
    Invoke-Item "C:\Test\Double.html"
        }
    }
    this is what i've done (well maybe i should say what i've copied but doesn't matter).
    This code at first step find duplicate files, then create an HTML file with an Excel-like table; the values in this table are the name and the path of duplicate files.
    If there are only one set of files, this script works great(well maybe it could be write better but for me it's ok); i found a big problem when the sets of files are two or more sets. In effect if i have 3 sets of file (example set1 have 2 bmp, set2 have 3 txt and set3 have 2 pdf) at set1 a hmtl file is created, immediately after with set2 the html file previously created is overwritten and finally with set3 that file is definitely overwritten so i've got only 1 file with the set3 values.
    How can i have one html file with the values of all 3 sets?
    it would be nice if the file name contains the date...
    Thanks
    A

  9. #8
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,621
    Thanks
    2
    Thanked 627 Times in 560 Posts
    In your original question about duplicate files you stated that the files may have different names. That implies that you must examine file content to determine a duplicate. Very, very time consuming and probably very impractical.

    Joe

  10. #9
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JoeP517 View Post
    In your original question about duplicate files you stated that the files may have different names. That implies that you must examine file content to determine a duplicate. Very, very time consuming and probably very impractical.

    Joe
    Yes i know... we've got a lot of files, surely there will be some with identical names in different directories, but most of them have different names.
    The script seems to work but for me the language of powershell is unknown. I think we should just work about the organization of the creating process of html file (move out the creation from the for cycle) but for me it's difficult nowadays.
    Thanks
    A

  11. #10
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Lightbulb [solved]

    Code:
    function Get-Double {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true,
                       Position=0,
                       ValueFromPipeline=$true,
                       ValueFromPipelineByPropertyName=$true)]
            [System.String]
            [Alias('PSPath')]
            $Path
        )
    
        if ((Test-Path -Path $Path) -eq $false) {
            throw New-Object System.IO.FileNotFoundException($Path)
        }
    
        try {
            $item = Get-Item -Path $Path -Force -ErrorAction Stop
        } 
        catch {
            throw
        }
    
        if ($item -isnot [System.IO.FileInfo]) {
            throw New-Object System.ArgumentException('Specified path must refer to a File object.','Path')
        }
    
        try {
            $bytes = [System.IO.File]::ReadAllBytes($item.FullName)
        } catch {
            throw
        }
    
        $md5Hash = [System.Security.Cryptography.MD5]::Create()
        $hashBytes = $md5Hash.ComputeHash($bytes)
    
        $sb = New-Object System.Text.StringBuilder
    
        foreach ($byte in $hashBytes) {
            $sb.Append($byte.ToString("x2")) | Out-Null
        }
    
        Write-Output $sb.ToString()
    }
    
    $files = @{}
    $folder = 'C:\Test\'  # Il percorso su cui lavorare
    
    Get-ChildItem -Path $folder -Force -Recurse | 
    Where-Object { $_ -is [System.IO.FileInfo] } |
    ForEach-Object {
        try {
            $hash = $_ | Get-Double
        } catch {
            Write-Error -ErrorRecord $_
            return
        }
    
        if ($files.ContainsKey($hash) -eq $false) {
            $files[$hash] = New-Object 'System.Collections.Generic.List[System.String]'
        } else {
            # I'm just identifying duplicates by adding all their names to a list.
            # Keep in mind that if two files have identical hashes, they could have
            # different contents; it's possible for two different inputs to produce the same
            # hash code.
        }
    
        $files[$hash].Add($_.FullName)
    }
    
    $data=@()
    foreach ($entry in $files.GetEnumerator()) {
        if ($entry.Value.Count -gt 1) {
            $data=$data+$entry.Value
        }
    }
    
    #$data= $data | sort{Split-Path( $data ) -leaf} possibilità di ordinare per nome(Verificare)
    $drives = foreach ($path in $data) {
                $prophash = @{
                Nome= split-path $path -leaf
                Percorso = $path
                }
    
            #create a new object from the property hash
            New-Object PSObject -Property $prophash
            }
            #embed the style in the html header
    
    $head = @'
    <Title>Double Report</Title>
    <style>
    body 
    { 
     background-color:#FFFFFF;
     font-family:Verdana;
     font-size:12pt; 
    }
    td, th 
    { 
     border:1px solid blue; 
     border-collapse:collapse; 
    }
    th 
    {
     color:white;
     background-color:green; 
    }
    table, tr, td, th { padding: 5px; margin: 0px }
    table { margin-left:50px; }
    </style>
    '@
    
    #create an xml document from the HTML fragment
    [xml]$html = $drives | ConvertTo-Html -fragment
    
    
    #check each row, skipping the TH header row
    for ($i=1;$i -le $html.table.tr.count-1;$i++) {
      $class = $html.CreateAttribute("class")
    }
    
    #create the final report from the innerxml which should be html code
    $body = @"
    <H1>Double Report for $Folder</H1>
    $($html.innerxml)
    "@
    
    #put it all together
    ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i>" -body $body | 
    Out-File "C:\Test\Double.html" -Encoding ascii
    
    Invoke-Item "C:\Test\Double.html"
    EUREKA!!!!!
    yeah it works!!!!!
    Sorry for some italian notes...
    Thanks for all!!!

  12. #11
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,621
    Thanks
    2
    Thanked 627 Times in 560 Posts
    Glad you got it resolved. Thanks for posting your solution.

    Joe

  13. #12
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JoeP517 View Post
    Glad you got it resolved. Thanks for posting your solution.

    Joe
    Just one thing...
    Now i've got this code
    Code:
    ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i>" -body $body | 
    Out-File "C:\Test\Double.html" -Encoding ascii
    Launching the script Double.html is overwritten.
    How can I set the name in Double-DayMonth.hmtl (ex Double 22Nov.html)?
    I know i must use get-date but How can I assign values?
    Thanks
    A

  14. #13
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    20,621
    Thanks
    2
    Thanked 627 Times in 560 Posts
    Set a variable to "C:\Test\Double". Set another variable to ".html". Set a variable to the time. Then concatenate all the variables.

    Joe

  15. #14
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Arrow when the work look finished, the problems appear

    I've got a new problem
    I want to change some file properties.
    indeed i want to remove the hide property
    How can i make it?
    Last edited by Albiz; 2013-11-25 at 05:14.

  16. #15
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Maybe Solved

    Code:
    $Move=@()
    $Att=@{}
    $Today = Get-Date -Format "dd-MM"
    $Folder= "C:\Estinto\$Today"
    
    if(!(Test-Path $Folder)){
        New-Item -Path $Folder -ItemType directory
    }
    
    $Ent=Get-ChildItem "C:\Test" -Recurse -Force -include *.$YY$, *.tmp, *.temp, *.DS_Store, *.part, ~$*, Thumbs.db
    foreach($i in $Ent){
        if($i.mode -match "h"){
            $Att[$i]=$i.Attributes
            $Att[$i]=$Att[$i] | where{$_ -ne "Hide"} #questo non va
            $i.Attributes = $Att[$i]
        }
        $Move=$Move+$i.fullname
        Move-Item $i.fullname -Destination $Folder 
    }
    
    
    $Move= $Move | sort{Split-Path( $_ ) -leaf}
    
    $drives = foreach ($path in $Move) {
                $prophash = @{
                Nome= split-path $path -leaf
                Percorso = $path
                }
    
            #create a new object from the property hash
            New-Object PSObject -Property $prophash
            }
            #embed the style in the html header
    
    $head = @'
    <Title>Deleting Temp Report</Title>
    <style>
    body 
    { 
     background-color:#FFFFFF;
     font-family:Verdana;
     font-size:12pt; 
    }
    td, th 
    { 
     border:1px solid blue; 
     border-collapse:collapse; 
    }
    th 
    {
     color:white;
     background-color:green; 
    }
    table, tr, td, th { padding: 5px; margin: 0px }
    table { margin-left:50px; }
    </style>
    '@
    
    #create an xml document from the HTML fragment
    [xml]$html = $drives | ConvertTo-Html -fragment
    
    
    #check each row, skipping the TH header row
    for ($i=1;$i -le $html.table.tr.count-1;$i++) {
      $class = $html.CreateAttribute("class")
    }
    
    #create the final report from the innerxml which should be html code
    $body = @"
    <H1>Deleting Temp Report for $Folder</H1>
    $($html.innerxml)
    "@
    
    #put it all together
    $Name = "Deleting Temp " + $Today
    
    ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i>" -body $body | 
    Out-File "C:\Log\$Name.html" -Encoding ascii
    
    Invoke-Item "C:\Log\$Name.html"
    Only one problem... i set normal attributes, but if i want to remove only the hidden attribute how can i do it?
    Maybe i must save attributes foreach file, then reassign attribute-hide?
    Thanks
    A
    Last edited by Albiz; 2013-11-26 at 10:08.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •