Jump to content

Windows Script meeda help kavali


jefferson1

Recommended Posts

need to create a batch file for two things

1. Server B lo log files untai on a particular folder, file limit 50MB. once the file size reaches 50MB another file will be created to write logs. Need a script to  zip the 50mb file copy it to another server A and unzip it. 

2. delete the files from Server A if they are two days old.

Link to comment
Share on other sites

20 minutes ago, jefferson1 said:

ltt

You can write a powershell script which monitors the folder, and define an action what u want to do when new file gets created..

This script monitors a certain folder and writes a logfile. You can replace the action and do whatever you want e.g call an external tool

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\source"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\log.txt" -value $logline
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

How to use

  1. Create a new text file
  2. Copy & paste the above code
  3. Change the following settings to your own needs:
    • folder to monitor: $watcher.Path = "D:\source"
    • file filter to include only certain file types: $watcher.Filter = "*.*"
    • include subdirectories yes/no: $watcher.IncludeSubdirectories = $true
  4. Save and rename it to StartMonitoring.ps1
  5. Start monitoring by Right click » Execute with PowerShell

To stop monitoring, it's enough to close your PowerShell window

More Info: https://superuser.com/questions/226828/how-to-monitor-a-folder-and-trigger-a-command-line-action-when-a-file-is-created

 

  • Upvote 2
Link to comment
Share on other sites

38 minutes ago, Spartan said:

You can write a powershell script which monitors the folder, and define an action what u want to do when new file gets created..

This script monitors a certain folder and writes a logfile. You can replace the action and do whatever you want e.g call an external tool


### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\source"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\log.txt" -value $logline
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

How to use

  1. Create a new text file
  2. Copy & paste the above code
  3. Change the following settings to your own needs:
    • folder to monitor: $watcher.Path = "D:\source"
    • file filter to include only certain file types: $watcher.Filter = "*.*"
    • include subdirectories yes/no: $watcher.IncludeSubdirectories = $true
  4. Save and rename it to StartMonitoring.ps1
  5. Start monitoring by Right click » Execute with PowerShell

To stop monitoring, it's enough to close your PowerShell window

More Info: https://superuser.com/questions/226828/how-to-monitor-a-folder-and-trigger-a-command-line-action-when-a-file-is-created

 

thanks bro

Link to comment
Share on other sites

You can trigger this ps1 file using task scheduler, which works like batch job. It is recommended to move away from .exe type of batch scripts bro. 

Link to comment
Share on other sites

4 minutes ago, ShruteSastry said:

You can trigger this ps1 file using task scheduler, which works like batch job. It is recommended to move away from .exe type of batch scripts bro. 

on windows meeda cheyali... you have any script to do that?

Link to comment
Share on other sites

5 minutes ago, jefferson1 said:

on windows meeda cheyali... you have any script to do that?

windows lo ne..task scheduler ki velli..

add this to run in loop every 1 min or so...it will run this ps file

Link to comment
Share on other sites

14 minutes ago, Spartan said:

windows lo ne..task scheduler ki velli..

add this to run in loop every 1 min or so...it will run this ps file

nuvvu paina cheppina script event detection ki undi, kani zip and copy log file over network kavali

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...