none
deleting a file from multiple servers using IP address and folder RRS feed

  • Question

  • I am trying to delete a specific .log file before it reaches 2mb in size from 20 different machines with static IP addresses.

    Is it possible to use forfiles command to do this and how do you get past the space in folder names as in "Program Files"

    I have tried to put it in quotes but it still hangs up on the space, my temp trial will work with no spaces, but when I go to the actual file location it errors out, the only change is the space in the folder name.

    My first trial is to get the file to be removed from my temp location which works fine and it will delete the specific .log file.  The only thing that is changed between the 2 of them is the path but it won't do the second one.

    forfiles /M *.log /C "cmd /c del "C:\macs\temp\nsclient.log"

    but when I put in a different location with a space in it the program will error out with "invalid argument/option /C:is not allowed more than one time".  If I run it again it will error with "invalid argument/option ' 'C:\Program'".

    forfiles /M *.log /C "cmd /c del "C:\Program Files\NSClient++\nsclient.log"

    is it possible to modify this so I can put in a IP address of the server so it will delete the .log file in the specified location
    in the C:\Program Files\NSClient++\  folder, and is it possible to put in multiple IP addresses so it will go to different servers to delete the file, or is there a better command other than forfiles to do this process, all are
    windows computers/servers of some kind either win7, win10?

    Wednesday, November 20, 2019 2:58 AM

All replies

  • ... or is there a better command other than forfiles to do this process, all are

    Definitely. You should use Powershell instead of cmd. Assuming you work with an Active Domain environment you would Powershell remoting to accomplish that easy task. You could use servernames instead of IP addresses saved in a file (ServerList.txt for example) and your code could be something like this:
    Get-Content -Path C:\sample\ServerList.txt |
        ForEach-Object {
            Remove-Item -Path "\\$PSItem\c$\Program Files\NSClient++\nsclient.log"
        }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, November 20, 2019 1:53 PM
  • thank you, would this have to be run each time or could it be set to automatic when the file reached somewhere between 1-1.5mb?
    Thursday, November 21, 2019 9:12 PM
  • thank you, would this have to be run each time or could it be set to automatic when the file reached somewhere between 1-1.5mb?
    You may answer this question for yourself. Do you expect that all of the log files have the same size all the time? Does the code example I posted checks the size of the log files before it deletes them? ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, November 22, 2019 12:38 AM