locked
CHECK DISK SIZE AND CONTINUE COPYING RRS feed

  • Question

  • Hi guys, 

    i need to create a script Powershell that checks the sizes of my disk and if is greater than X it will start a robocopy, also, must be check until will arrive at Y size and in case will stop the robocopy.

    Should be something like this:

    $disk = Get-PSDrive C | Select-Object @{ E={$_.Used/1GB}; L='Used' }, @{ E={$_.Free/1GB}; L='Free' }
    $disk.Used 
    DO
    {
    robocopy C:\test c:\test2 
    } while ($disk.Used -gt "200")
    $disk2 = Get-PSDrive C | Select-Object @{ E={$_.Used/1GB}; L='Used' }, @{ E={$_.Free/1GB}; L='Free' }
    write-host your disk reached quota $disk2.used 

    Can you please helpe me?

    Wednesday, October 17, 2018 3:30 PM

All replies

  • If you "robocopy C:\test c:\test2" you are not going to free up any space on the C drive because your destination drive is the same as the source. You would at least need to "robocopy c:\test E:\test2 /mov". The default is to just copy and again that won't free up space. Hence the /mov switch. If you want to move the directories you would need to use the /move switch. If you kill the robocopy process in the middle of a file copy operation you could end up with a corrupted file in the destination folder. 

    You will be better off using get-childitem and moving individual files and test the disk space after each file move. Here is one example: https://stackoverflow.com/questions/14935076/powershell-script-to-move-files-and-folders-including-subfolders-from-one-locati

    Here is another https://blogs.technet.microsoft.com/heyscriptingguy/2013/04/19/use-powershell-to-copy-files-and-folders-to-a-new-location/

    You code would need to account for folders, duplicate names, and files that are in use and cannot be moved.  

    Wednesday, October 17, 2018 4:10 PM
  • No, thanks.

    I need to move a fileshare so I need to run robocopy. 

    My goal is to interrupt the robocopy everytime my disk will be near 100GB free space.

    Can you help me please guys?

    Wednesday, October 17, 2018 4:37 PM
  • You cannot stop RoboCopy at a size.  It does not work that way.  Also RC will not move files unless you ask it to.

    Take some time to rethink what you are trying to do and ask yourself if it makes technical sense.

    To learn how to use PowerShell and how to check disk space look in the Gallery for numerous scripts that do what you are trying to do.


    \_(ツ)_/

    Wednesday, October 17, 2018 5:34 PM
  • Hi, thanks for your answer but sorry if I don't agree.

    What I'm looking to do is, for example, just start a robocopy via task scheduler that move files from E:\ to F:\ 

    Then, via script i would like to check in real time the size of the Disk F: and, when it reaches a size, just stop the job. 

    it's something that I can do ?

    Wednesday, October 17, 2018 9:50 PM
  • That can cause issues.  I recommend that you learn to write PowerShell scripts and test your theory.  Your original question does not indicate that as your purpose and the script posted seems to be just a wild guess.

    You can also ask a question about a script that is not asking for someone to design a solution.


    \_(ツ)_/

    Wednesday, October 17, 2018 10:07 PM
  • Over the years, people have asked me a lot of questions. One time a guy asked me what the subnet mask was on our primary Unix based Oracle DB server. While I knew what it was, being a Windows sysadmin, I had to wonder why he was asking me that question. So I have learned to ask the question; "what's the real problem?". Because the user has gone down some path of trying to solve a problem and has hit a roadblock, like trying to use robocopy, when in fact a completely different solution becomes obvious once the user explains what's going on and what he is really trying to accomplish.

    If you don't agree with me or jrv that robocopy is probably not your best choice, then that's fine. Go right ahead and test robocopy and see if you can get it to work. We are not going to build a solution for you. 

    In order for anyone to truly help you, you would need to provide a lot more detail about the data and processes that create it. Have you looked at disk quotas? Are log files involved that could be purged instead of being moved to a different drive? Why don't you warn users ahead of time that the disk is reaching capacity? Ie at 200gig free, warn the user, at 150gig free, remove update access so no more files can be added to the file system.   

      
    Wednesday, October 17, 2018 10:52 PM
  • Use Treesize Free and analyze your disk space usage. Clean up your disk so that you don't have to write a script.

    https://jam-software.com/treesize_free/

    Thursday, October 18, 2018 10:44 AM