Batch script to move compress and delete files - winrar

I have a batch script that does the following
#ECHO OFF
REM move files older than 2 days from the incoming directory to the incoming archive directory
robocopy D:\Agentrics\integration\download D:\Agentrics\integration\download\archive /MOV /MINAGE:2
REM Zip files in the Archieve directory that are older than one week
FOR %%A IN (D:\Agentrics\integration\download\archive\*.txt*, D:\Agentrics\integration\download\archive\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r -to7d D:\Agentrics\integration\download\archive\"%%~nA.zip" "%%A"
REM Delete Original files after they are zipped
forfiles /p D:\Agentrics\integration\download\archive /s /m *.txt* /d -7 /c "cmd /c del /q #path"
forfiles /p D:\Agentrics\integration\download\archive /s /m *.cpi* /d -7 /c "cmd /c del /q #path"
REM Delete files that are older than 6 months from the archive directory
forfiles /p D:\Agentrics\integration\download\archive /s /m *.zip* /d -180 /c "cmd /c del /q #path"
pause
Question 1:
When i run the script i get WinRAR diagnostic messages for some files. For example if there are files in the incoming directory that are not older than two days i get this message."WinRAR Diagnostic messages: No File To Add". Because of this message the scripts stops until i click on the close button of the dialogue box. I am using the free version of WinRAR and its not expired
Question 2: I have two seprate command in the script above. One is for zipping the files older than a week and the other one is deleting the original files after they are zipped. How can i link those two commands so that if some reason the files did not get zipped they should also not get deleted. Or is is there a command to break the script if the files did not get zipped? I just want to zipp the files first and then delete the original ones

I sugest to use
#ECHO OFF
REM Move files older than 2 days from the incoming directory to the incoming archive directory.
robocopy D:\Agentrics\integration\download D:\Agentrics\integration\download\archive /MOV /MINAGE:2
REM Move each file in the archive directory that is older than one week into a ZIP archive.
FOR %%A IN (D:\Agentrics\integration\download\archive\*.txt*, D:\Agentrics\integration\download\archive\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" m -afzip -ep -inul -to7d -y "D:\Agentrics\integration\download\archive\%%~nA.zip" "%%A"
REM Delete files that are older than 6 months from the archive directory.
forfiles /p D:\Agentrics\integration\download\archive /s /m *.zip* /d -180 /c "cmd /c del /q #path"
The entire process can be simplified by using command m which means move to archive instead of command a which means add to archive. WinRAR removes the file only after successful compression.
Using switch -afzip informs WinRAR explicitly to use ZIP instead of RAR compression.
The switch -ep results in removing path from the file names inside the archive.
The output of any error or warning message can be suppressed with switch -inul. This switch is mainly for the console version Rar.exe (not supporting ZIP copmression) for output to stdout and stderr, but may work also for WinRAR. I have never seen a diagnostic message to confirm on my tests using WinRAR.exe version 4.20 when the ZIP file was not created because the file was not older than 7 days. I have seen the warning on using Rar.exe creating a RAR archive and not using -inul, but also with no need for a key hit even without using switch -y.
I removed switch -r for recursive archiving as not needed here with always moving only 1 file to a ZIP archive.
The unmodified switch -to7d results in archiving only files older than 7 days.
Last the switch -y is added to assume Yes on all queries although I have never seen one on my tests.
One more hint:
On NTFS partitions the attribute compressed can be set on a folder resulting in an automatic ZIP compression of all files copied or created in this folder to save disk storage.

Related

Scheduled BAT - Error during XCOPY if file is in use

I have this script scheduled every hour:
#echo off
set path1="E:\Document\Backup"
set path2="E:\Document\NewDoc"
set path3="C:\ScanDoc"
forfiles -p %path1% -s -m *.pdf /D -30 /C "cmd /c del #path"
xcopy %path2%\*.pdf* %path1% /c
start /d %path3% ScanBatch.exe
Files in "NewDoc" folder are created by manual document scanning (PDF FORMAT), so sometimes documents are in use.
The Scanbatch program read files in "Backup" folder, so if PDF is copied from "NewDoc" to "Backup" while in use, it's result as corrupted and the Scanbatch go in error.
Is there a way to copy files ONLY IF NOT IN USE?
At the end the real problem wasn't xcopy, but "Scanbatch.exe" that crashes if found an opened file. Problem solved changing schedulation time.

bat script to unzip .gz file in a location and extract to another location

I need to know how to write bat script to unzip file in a location and save the extracted file to a different location, also I need to delete the original file after the extraction is done.
For example suppose my .gz file are in location C:\Users\jibin\Desktop\CDR\ and I need to extract all the files in this location to C:\Users\jibin\Desktop\CDR\CDR_Extract(destination).
This can easily be achieved using 7-zip, combined with forfiles, something like (not tested, don't kill me if it doesn't work immediately):
forfiles /M *.gz /C "cmd /c 7z x #path -oC:\Users\jibin\Desktop\CDR\CDR_Extract *.* -r"
Where:
x means "extract"
-o means "output directory"
*.* means "filename inside can be anything, extract every file"
-r means "recursive subdirectories"
Deleting the zipfile afterwards is obvious.
Without need to install third party software - you can use gzipjs.bat:
md "C:\Users\jibin\Desktop\CDR\CDR_Extract\" >nul 2>&1
for %%# in ("C:\Users\jibin\Desktop\CDR\*.gz") do (
call gzipjs.bat -u "%%~f#" "C:\Users\jibin\Desktop\CDR\CDR_Extract\%%~n#"
rem del "%%~f#" /q /f
)
Check if this is what you need and delete the rem word in the last file in order to activate deletion of the source files as you requested.
If you are using windows 10 with all the latest updates (from build 17063 and above) you can use TAR.
If you're using windows-10, version 1803+, (December 2017 to April 2018 - minimum 10.0.17063), then you may be better advised to use the built-in tar executable:
#If Exist "C:\Users\jibin\Desktop\CDR\*.gz" (
If Not Exist "%__AppDir__%tar.exe" Exit /B 1
PushD "C:\Users\jibin\Desktop\CDR"
If Not Exist "CDR_Extract\" MD "CDR_Extract" 2>NUL || Exit /B 1
For %%G In (*.gz) Do #("%__AppDir__%tar.exe" -xf "%%G" -C "CDR_Extract"
If Not ErrorLevel 1 Del "%%G")
PopD)

Batch job to delete folder older than 7 days on windows 7

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.
I have used the following commands which haven't worked out well for me
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
The second command which I referred from Batch file to delete files older than N days
also didn't work for me.
Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.
So your task is to delete older backup directories no longer needed.
The tricky solution would be working with "delete older than X days".
The simple solution is following:
#echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
rd /Q /S "%BackupDirectory%\%%D"
)
The command DIR returns a list which contains
because of /AD (attribute directory) just the subdirectories and
because of /B (bare format) just directory names and
because of /O-D ordered by date with newest at top and oldest last.
The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.
In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.
Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.
On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.
In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.
And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.
Remove old backup folders older than /d days. So use a minus value.
Oneliner...
forfiles /p y:\foldername /d -50 /c "cmd /c if #isdir==TRUE rmdir #file /s /q"
You want to leave off the forfiles "/s" if you only want to scan the top level folders and once you have that folder, you rmdir the entire folder.
The reason for using rmdir is because if you use del or erase, once it deletes a file, it can change the date on the holding folder. Then the command leaves folders behind.
/p - for path
/d - minus for 50 days old
/c - run a command
#isdir - if folder
Rmdir - better than del or erase
#file - what it was looking for
/s - subfolders
/q - quiet

Windows batch - Delete files of certain age and type and list them

I'm looking for a solution to allow users to delete certain file type older than X days from a specific path.
I managed to create this batch file so far:
#echo off
set /p FileAge= Delete files older than this many days:
set /p FileType= File type to delete. Seperate with comma. (mov, mxf...):
set /p FilePath= Full path of files location:
for %%G in (%FileType%,) do forfiles -p "%FilePath%" -s -m *%%G -d -%FileAge% -c "cmd /c echo #path"
Choice /M "These files will be deleted. Do you want to continue?"
If Errorlevel 1 Goto Yes
If Errorlevel 2 Goto No
:Yes
for %%G in (%FileType%,) do forfiles -p "%FilePath%" -s -m *%%G -d -%FileAge% -c "cmd /c del #path"
:No
exit
PAUSE
There are several things missing:
In line 9 - Choice /M "These files will be deleted. Do you want to
continue?" What would be the best way to count how many files are
going to be deleted?
I would like the batch to output a list of deleted files to a log file. Something like log_01312014.txt
Hello O_ren to Stack Overflow.
Assume you are using Windows 7 or later we will use a folder named "Files" to transfer desired file types inside "To_Delete" Folder + a log file with the name "Log_%date%.txt"
After that you can simply delete or keep your files inside "To_Delete" folder with del command.
in the example below this one liner solution will move ini files older than 7 days to our folder "To_Delete" and log the moved files.
robocopy "C:\Files" "c:\Files\To_Remove" /mov *.ini /minage:7 >C:\Files\Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.txt

Batch script to move file from scource to destination folder for files older than 30 day

i want to create the batch script which could copy the 30 days older files from one folder to another. And also deletes the 30 days old files from the source folder after copying to the destination folder
forfiles /p "c:\sourceFolder" /S /D -30 /C "cmd /c robocopy "C:\DestinationFolder" "C:\sourceFolder" /mir #file : date>=30 days=nul"
Nothing has happened, not even error message is displayed
robocopy sourceFolder targetFolder * /s /mov /minage:30 /l
If the list is correct, remove the /l (only list) from the command to do the move
This robocopy only moves a few files and deleted my rar packages? not working at all as I would like to. I just want it to work as user3302083 wants. Just move folder that is 2 days old no other changes. Now I tried and it moved just a few files and deleted.

Resources