#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Batch file to move/compress/delete files.. Please Help!!!


    Hello,
    I am trying to write a batch script that will run automatically daily to do the following.

    1. Move files older than 2 days to from the main directory(Jason) to archive directory.

    2. Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.

    3. I want to run this script from a different directory(not the directory that has the files).

    I wrote the following script but its not correctly working. Can you please help.


    REM move files older than 2 days to a archive directory
    robocopy D:\Agentrics\integration\incoming\Jason D:\Agentrics\integration\incoming\Jason\archive /MOV /MINAGE:2

    Question: How can i change the command below to zip files older than 1 week. Also i want to put this command in a batch file that will run from a different location? Is it possible that the zipped files can have the same creation date and time as the original files?
    REM zip all files in the backup directory
    FOR %%A IN (*.TXT*, *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
    FOR %%A IN (*.TXT,*.cpi) DO DEL "D:\Agentrics\integration\incoming\Jason\archive\.cpi*" "%%A"

    REM Delete all files in the backup directory that are older than 6 months
    forfiles /p D:\Agentrics\integration\incoming\Jason\archive /s /m *.* /d -500 /c "cmd /c del /q @path"

    Kind Regards,
    Jason
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    How can i change this script so that it only compresses the files that are older than 7 days.

    FOR %%A IN (D:\Agentrics\integration\incoming\Mansoor\backup\*.txt*, D:\Agentrics\integration\incoming\Mansoor\backup\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r D:\Agentrics\integration\incoming\Mansoor\backup\"%%~nA.zip" "%%A"


    FOR %%A IN (D:\Agentrics\integration\incoming\Mansoor\backup\*.TXT,D:\Agentrics\integration\incoming\Mansoor\ba ckup\*.cpi) DO DEL "D:\Agentrics\integration\incoming\Mansoor\backup\.cpi*" "%%A"

    Regards,
    Mansoor
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0
    The only way that comes to mind would be to use a FORFILES, like you did at the end of your script, rather than a FOR.

    Alternatively, I've never been a fan of NTFS compression, but many don't mind it. You could just create a NTFS compressed subfolder and robocopy your files into it.

    If I had this series of tasks to do I would choose something a little more flexible to write it. If you get creative enough you can patch together the write commands and utilities, but there are easier ways.

    For Windows scripting I like AutoIT. IMO it's easier to work with/install than the unix-born script.

    It has built-in functions for all sorts of things (e.g. modifying time stamps on files, selecting by age, etc.) and a simple way to run an external utility (e.g. robocopy) if the situation suits it.

    Below is an AutoIT script I wrote to monitor a folder for new TIFF files and, when present, convert to PDF with a name based on the timestamp. Email the PDF, then archive the TIFF and PDF to separate folders.

    Obviously this script won't work for you, but it shows how to do the kinds of things you need to do.
    Code:
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Res_Fileversion=1.5.1.1
    #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
    #AutoIt3Wrapper_Res_SaveSource=y
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    ;no spaces allowed in paths or file names (including in the names of the files being processed)
    $tiffIn = "C:\DATA\FAX\"
    $tiffOut = "C:\DATA\FAX_TIFF\"
    $pdfOut = "C:\DATA\FAX_PDF\"
    $tiff2PDFDir = "C:\scripts\libTIFF\bin\"
    $tiff2PDF = "tiff2pdf.exe"
    $blatDir =  "C:\scripts\blat262\full\"
    $blat = "blat.exe"
    $bTo = "fax@email.com"
    $bFrom = "fax@email.com"
    $bSubject = '"Incomming Fax"'
    $bBody = '"Incomming Fax"'
    $bServer = "localhost"
    $bLog = "C:\scripts\blat.log"
    $curDate = ""
    $lastDate = ""
    $lastSuffix = 0a
    $sleepTime = 60000 ;one minute
    ; Infininte loop, sleep for $sleepTime before repeating.
    While 1
    	While 1
    		; Shows the filenames of all files in the current directory.
    		If FileChangeDir($tiffIn) Then
    			$search = FileFindFirstFile($tiffIn & "*.TIF")
    		Else
    			Exit
    		EndIf
    		; Check if the search was successful
    		If $search = -1 Then
    			;MsgBox(0, "Error", "No files/directories matched the search pattern")
    			;Exit
    			ExitLoop
    		EndIf
    		While 1
    			$file = FileFindNextFile($search)
    			If @error Then ExitLoop
    			$curDate = FileGetTime($tiffIn & $file, 0, 1)
    			If @error Then ExitLoop
    			;wait to make sure file still isn't being written
    			If $curDate = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC Then Sleep(5000)
    			$newFile = "Fax-" & $curDate
    			If $curDate = $lastDate Then
    				$lastSuffix = $lastSuffix + 1
    			Else
    				$lastSuffix = 0
    			EndIf
    			If $lastSuffix <> 0 Then
    				$newFile = $newFile & "-" & $lastSuffix
    				$lastSuffix = $lastSuffix + 1
    			EndIf
    			$lastDate = $curDate
    			$pdfFullFile = $pdfOut & $newFile & ".pdf"
    			$exec = $tiff2PDFDir & $tiff2PDF
    			$params = "-p letter -f -o " & $pdfFullFile & " " & $tiffIn & $file
    			$result = ShellExecuteWait($exec, $params, $tiff2PDFDir, "open", @SW_HIDE)
    			$exec = $blatDir & $blat
    			$params = "- -body " & $bBody & " -t " & $bTo & " -f " & $bFrom & " -subject " & $bSubject
    			$params = $params & " -server " & $bServer & " -attach " & $pdfFullFile & " -log " & $bLog
    			ShellExecuteWait($exec, $params, $blatDir, "open", @SW_HIDE)
    			FileMove($tiffIn & $file, $tiffOut & $newFile & ".TIFF", 9)
    			;MsgBox(4096, "File:", $file & " - " & $result)
    			;ExitLoop
    		WEnd
    		; Close the search handle
    		FileClose($search)
    		ExitLoop
    	WEnd
    	; Sleep before checking for faxes again
    	Sleep($sleepTime)
    WEnd

IMN logo majestic logo threadwatch logo seochat tools logo