1.

Solve : How do I write a batch file to use WinRAR to archive and delete the file?

Answer»

I need to write a dos batch file to archive my history data, the files have specific file NAME or file extension. I want to archive file older than 30 days. Is that possible?Why do you need to do this? What's WRONG with Windows Explorer?
winrar has a command line version called rar.exe
on the command line, type rar /? you will a list of options you can use.

Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\test"
strMonth=DatePart("m",DateAdd("m",-1,Date))
If Len(strMonth) <= 1 Then
strMonth = "0"&AMP;strMonth
End If
strYear = Year(Date)
If strMonth = "12" Then
strYear=strYear-1
End If
Set WshShell = CreateObject("WScript.Shell")
Set objFolder = objFSO.GetFolder(strFolder)
For Each strFiles In objFolder.Files
If InStr(strFiles.Name,"RBS") > 0 And DateDiff("d",strFiles.DateLastModified,Now) >= 30 Then
WScript.Echo strFiles.Name
strCmd = "C:\Program Files\WinRAR\rar a -tb " & strYear & "-" & strMonth & ".rar " & """" & strFiles.Name & """"
WScript.Echo strCmd
Set oExec = WshShell.Exec(strCmd)
Do While oExec.Status = 0
WScript.Sleep 100
Loop

End If
Next

save the above as script.vbs and on command line
Code: [Select]c:\test> cscript /nologo script.vbs
Hi ghostdog74,

Thanks so much for the code. It is working but do you mind if I want to take off the pop up screen for every document I archive? My folder is really really hugh. I just want to let it run itself without monitoring it and hit ok for every file.

Thanks!why do you need to hit anything? It doesn't happen to me. Its auto.I save the file in my network drive which I name it as auto archive.vbs and execute it by double click it and it pop up the file name every time run it using the command prompt.I didn't run it in command prompt because I am not quite sure what is "c:\test> cscript /nologo script.vbs" ....mmm... c:\test is where my .vbs file located, c:\test>cscript is ask the vv to execute, i guess.... how about /nologo?
c:\test is just my own DIRECTORY. cd to where your script is and run cscript from there...or else, from wherever you are, you can just give the full script path

eg say you are in c:\
Code: [Select]c:\> cscript /nologo c:\directory_where_my_script_is\script.vbs

as for nologo, its just cscript's option. type cscript /? for more infoMy script.vbs is in W drive, inside inbound folder. Should I use W:\>cscript /nologo W:\inbound\script.vbs ?I figured it out with your answer, forgot to say thanks to ghostdog74. Thanks a lot



Discussion

No Comment Found