Sid
Sid

Reputation: 195

Running the batch file based on the condition that a file arrives in folder

I am automating one of my Jobs in which i intend to zip the files in the folder as soon as they arrive in the folder.

Win zip script has been written in the batch file,

can you please help how it can be done through windows batch file or any other way?

Thanks, Sid

Upvotes: 0

Views: 4026

Answers (3)

Matt Williamson
Matt Williamson

Reputation: 7117

Here is a way to do it with a vbscript. Copy the following into a text file and rename it Folderwatcher.vbs

    'FolderWatcher.vbs

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent " _
    & "WITHIN 1 WHERE " _
    & "TargetInstance ISA 'CIM_DataFile' and " _
    & "TargetInstance.Drive='C:' and " _
    & "TargetInstance.Path='\\scripts\\'")

    Do
        Set objEvent = colMonitoredEvents.NextEvent
        Wscript.Echo objEvent.TargetInstance.FileName & "." & _
        objEvent.TargetInstance.Extension & " Created"
        wscript.sleep (10000) 'wait 10 seconds
        'Run your Unzip code here
        wscript.echo "Unzipping file now"
    Loop

Change TargetInstance.Drive and Path to the directory the files are being received into. If the files are larger, you can increase the wscript.sleep. It's 1000 per second. Also, if you run this with Wscript, it will popup windows for the creation and unzip events. If you don't want that, comment out those lines with a ' or just run it in a cmd window with

Cscript //nologo folderwatcher.vbs

where your cmd prompt is at the location of folderwatcher.vbs. Ctrl_C in the cmd window it's running in will stop the script.

Upvotes: 1

foxidrive
foxidrive

Reputation: 41307

Without knowing the files and how they apply to the situation, you can use this bat file to monitor the last file in the set and when it arrives it will zip them up.

If the file is being copied or FTP'd then you will need to add a delay like the ping command shown, so that the last_file.exe is complete before proceeding with the ZIP process.

@echo off
:loop
if exist "last_file.exe" goto :zip
ping -n 15 localhost >nul
goto :loop
:zip
rem 60 second delay
ping -n 60 localhost >nul
rem your script goes here.

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20494

You can do it with Batch but then you have to keep the CMD window opened all day to monitor for file changes in "X" folder, or you can add a scheduled repetitive task to start a batch file every 1 minute which search for new files in the folder... but that is the same. You can do it but Batch is not the good way to do this.

Consider to use this application called Directory Monitor wich is Free and does what you need:

http://www.brutaldev.com/page/Directory-Monitor

enter image description here

Also you can download a portable version.

This are the steps:

1- Download/install the application

2- Open it and click in "directories" to add a folder to monitor and select the file changes that you want to monitor (in this case, newfiles).

3- In the same window you can see a "Execute" textbox where you can load winzip with some arguments or you can launch a batch script to compress the files like this:

Execute.bat

@Echo OFF
For /R "C:\MyNewfiles" %%# in ("*") Do (
    If /I "%%~x#" NEQU ".zip" (
        YourWinZIP Command to compress "%%a"
    )
)
Exit

4- Configure the settings of the application if you want to load it with user session.

I hope this helps.

Upvotes: 1

Related Questions