Question

I am setting up a job to allow me to copy log files as a scheduled batch job on Windows boxes.

The current directory on machine A is:

Logs\   YYYYMMDD\   Logfile1.log
                    Logfile2.log
                    Logfile3.log

This machine normally has 3 days worth of logs kept on it at any time.

I have already setup a Robocopy job up as a scheduled task on another server (machine B) to copy the files over to machine B but not to move anything that has already been copied, using this command...

Robocopy "\\machineA\Logs" "\\machineB\LOGSTORAGE\machineA\Logs" /s /zb /r:2 /w:2 /eta /LOG+:\\machineB\LOGSTORAGE\logmove.log /TEE

What I want is to zip the files on machine A and then move them rather than copy them and to ensure I do not then have duplicate zip files created by the job after they have been moved.

I could possibly recreate the folder structure in another location and share this so that machine B has access to the folders as they are but with zip files as the contents, as below:

Logs\       YYYYMMDD\       Logfile1.zip
                            Logfile2.zip
                            Logfile3.zip

Ideally, I want the zip to take place using Windows, without the need to install 7Zip or some other compression software. This page mentions PowerShell to do the job but I don't know how I can edit this to my requirements.

As there are likely to be 3 days worth of logs on the machine at any time, is there a way I could ensure that the zip job only compresses files that haven't already been moved over to the final destination?

Can anyone help me with this?

Was it helpful?

Solution

It's been a long time but I ended up changing the requirement to zip the whole folder because just zipping the files meant that the file date was saved as the actual zip creation date rather than the original and for this task, the dates were all important. In the end, it was easier to just zip the folders without creating a temp directory and then have machine B grab the files using Robocopy.

This is what I used in the end

@echo off
for /d %%x in ("C:\Program Files\Logs\*.*") do start "C:\Program Files\7-Zip\7z.exe" /b /low /wait "C:\Program Files\7-Zip\7z.exe" a -tzip "%%x.zip" "%%x\"

OTHER TIPS

Well I am not familiar with Robocopy, however what you could do is create a batch to zip the files, copy them using robocopy, then delete them. This could be done by putting the Robocopy code in between like this (This uses 7zip, sorry):

@ECHO OFF
REM Adds all folders in the current directory to archives 
for /d %%a in (*) do (7za a -tzip %%~na.zip "%%a")
REM Insert Robocopy code here.
REM Deletes the remaining folders
for /d %%a in (*) do (if exist "%%a\" del "%%a")
pause

This answer isn't perfect, so if you don't understand something, ask me and I'll try and clarify.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top