Batch script for moving files doesn't work - batch-file

I tried to create a batch script which will move a folder created on a previous day from a directory to another.
I've looked at all the trends and tried to do it myself, and I managed to do something, but it still doesn't do what I would like.
In a directory, I will always have 2 folders. Every day I need to go and copy the folder created on the previous day to the new location at 3 am in the morning. I've tried all the option with the MAXAGE, MINAGE, MAXLAD, and MINLAD but probably I don't understand how this works!
#echo off
/MINAGE:1 :: MINimum file AGE - exclude files newer than 1 days/date.
set MyLogFile=%date%
set MyLogFile=%MyLogFile:/=-%
robocopy /log+:"C:\Users\Desktop\Test\%mylogfile%.txt" /TEE C:\Users\Desktop\Test\try1\n1 C:\Users\Desktop\Test\try2\n2 /MOVE /E /MINAGE:1
robocopy /log+:"C:\Users\Desktop\Test\%mylogfile%.txt" /TEE C:\Users\Desktop\Test\try3\n3 C:\Users\Desktop\Test\try4\n4 /MOVE /E /MINAGE:1
At the moment it moves all the files from try1/n1 to try2/n2 no matter how old or new is the folder.
The expected result would be from try1/n1 in which I have 2 folders (one created yesterday and one today) to move into try2/n2 only the folder created yesterday!

Related

batch file to copy files to newly created folder with date name

I'm making a system that our employees need to store files to that are ment to be processed. This process however deletes the source files and I want to make a backup prior to these files being processed.
The files need to be stored in an automatically created folder with name being today's date. The files vary in filetype.
The first part, creating a folder with today's date, I managed to figure out. The second part, copying files from a general folder to this newly created folder is something I can't get to work.
This is the code I have so far
#echo off
set date="%date:~-10,2%-%date:~-7,2%-%date:~-4,4%"
set "source=C:\b2g\2021\general"
set "destination=C:\b2g\2021\%date%"
mkdir "C:\b2g\2021\%date%"
robocopy "%source%" "%destination%" /mir /minage:%X%
exit /b
This is creating the folder, but is not copying any files to that folder.
any help is very kindly apreciated

Need a batch file to move directories based on date (CCYYMM) in directory name

I'm building batch files to (1) move directories older than 120 days to an archive location and (2) then eventually delete those folders from the archive location when they are 4 years old.
The names of the directories are in CCYYMM format (201601). The business rule is to move/delete based on the date in the directory name, not the modify or create date.
This is similar to Batch file logic to delete files based on date in filename, but since my scenario is for directories, not files, not certain if this would work, or how to modify for directories. The FOR command only appears to work for files(?)
Is there a method to loop through directories and perform a move or rmdir command in a batch file, comparing the month/year in the name to current month/year?
I did find that FORFILE works for directories based on modified date, but requirements are to use directory name.
Thanks for your consideration!
Edit 20160817
Since the directories are year/month labeled (201601 for January 2016), I'm taking the current year month (201608) and subtracting the directory value. For example 201608 - 201601 = 7. Then the batch would move any folder that has a difference of 4 or greater to meet the business requirements, using the FOR /D and only looking at directories beginning with 2:
#echo off
setlocal EnableDelayedExpansion
SET tempDATE=%DATE:~10,4%%DATE:~4,2%
for /d %%a in ("2*") do (
set /a numberOld=tempDATE - %%a
if !numberOld! geq 4 move /Y %%a ArchiveFolder
)
Using the same type of logic, I'm using 400 as the test value for the remove/delete directory script.
Appreciate everyone's insight.

Copy file before today date from one folder to another using command prompt

How can I copy all files from the folder except today's files to another folder.
So How can I find files date wise. because I want to those files what are before today. I don't want to copy my today's files.
ROBOCOPY C:\xampp\htdocs\a\images C:\xampp\htdocs\a\images1 /MIR
I used this command but it copied all files. So please tell me what should I do ahead?
According to the documentation of the robocopy command the parameter /minage should do the trick. The number after is the number of days, which you might have to adjust a little to fit your needs:
ROBOCOPY /minage:1 C:\xampp\htdocs\a\images C:\xampp\htdocs\a\images1 /MIR

Batch script to delete older backup files in a folder created by date with the exception of the newest 5 files

I have a program that creates a backup every 30 minutes, but does not delete older files. I am looking to make a script that will run on task scheduler that will look in the folder that is created by the program. It creates folder by date as follows /year/month/day/*name of file.zip. I have done a little research and come up with
#ECHO OFF
for /f "usebackq delims== tokens= 2" %%i in ( WMIC OS GET localdatetime /format:value ) do set localTime=%%i
set "YYYY=%localTime:~0,4%"
set "M=%localTime:~4,1%"
set "MM=%localTime:~5,1%"
set "D=%localTime:~6,1%"
set "DD=%localTime:~7,1%"
if %M%==0 set M=
if %D%==0 set D=
cd /d "~%~dp0"
FOR /F "tokens=*" %%X IN ('dir .\%YYYY%\%M%%MM%\%D%%DD%\Backup-serversave-%YYYY%-%M%%MM%-%D%%DD%--*.zip /b /o:-d') DO set FILENAME=%%~X
rem FILENAME now contains the last element
if "%FILENAME%" neq "" DEL "%FILENAME%"
pause
This script looks in the folder by todays date and than should delete the file that is oldest in the folder. But every time I run it, all I get is a "cannot find file" and it tells me the exact file that it says it can't find. So I need a little help to make it work. It would be also great if there is any way to make it only delete after five files are in the folder. If anyone can help me that would be amazing.
Update. Got a good answer but need a little more help. If anyone out there can find a way to make it delete yesterdays folder at the change of the day that would be amazing I am looking all over and so far have found nothing.
You did already a quite good job on writing the batch file, but made some small mistakes.
Using in first line ECHO ON helped to find those mistakes.
The following lines with some corrections for the batch file work.
#ECHO OFF
for /f "usebackq delims== tokens=2" %%i in ( `WMIC OS GET localdatetime /format:value` ) do set localTime=%%i
set "YYYY=%localTime:~0,4%"
set "M=%localTime:~4,1%"
set "MM=%localTime:~5,1%"
set "D=%localTime:~6,1%"
set "DD=%localTime:~7,1%"
if %M%==0 set M=
if %D%==0 set D=
cd /d "%~dp0"
for /F "usebackq tokens=* skip=5" %%X in ( `dir .\%YYYY%\%M%%MM%\%D%%DD%\Backup-serversave-%YYYY%-%M%%MM%-%D%%DD%--*.zip /b /o:-d` ) do del .\%YYYY%\%M%%MM%\%D%%DD%\%%X
pause
No file was not deleted ever because you used command dir to find the ZIP files in a subdirectory. The name of a found file was referenced with %%X, but without the path to this file. As the batch file did not change into the directory on which dir searched for the ZIP files, command DEL could not find the file to delete in the current working directory.
The first 5 lines returned by command dir containing the 5 newest files according to last modification date are ignored by using skip=5. Therefore only files with todays date in name and older than the first 5 files in directory listing are deleted and the newest 5 files are kept.
There was also a mistake in line with command cd and some other small mistakes all found by using ECHO ON in first line.
Deleting the previous day folders is difficult if the batch file should find out automatically which folders should be deleted and which folders should be kept.
Here is a solution which makes this task simple and which can be appended to the other lines in my other answer for deleting the older backup files.
if exist .\%YYYY%\%M%%MM%\%D%%DD%\Backup-serversave-%YYYY%-%M%%MM%-%D%%DD%--*.zip (
md TempBackup
copy /V .\%YYYY%\%M%%MM%\%D%%DD%\*.zip TempBackup >nul
for /F "usebackq" %%D in ( `dir /AD /B 20*` ) do rd /S /Q %%D
md %YYYY%\%M%%MM%\%D%%DD%
copy /V TempBackup\*.zip %YYYY%\%M%%MM%\%D%%DD% >nul
rd /S /Q TempBackup
)
First, a check is made if there are already ZIP files today stored to prevent deleting older backups if not at least one backup is created today.
Next a directory with name TempBackup is created into which are copied the currently existing backup files created today.
Then a dir command is executed which lists only the names of the directories starting with 20. Currently this is just the directory 2014, but on 2015-01-01 it will be additionally 2015. All those directories are recursively and silently deleted.
Next the directory tree for today is recreated and the ZIP files are copied back into this directory.
Finally the temporary backup directory for the ZIP files is deleted, too.
I'm wondering now why using a directory with current year as name containing a directory with current month as name containing a directory with current day of month as name if the ZIP files itself contain also year, month and day in name, and all ZIP files except the last 5 ZIP files saved today should be deleted?
It would be perhaps easier to manage to create all ZIP files in same directory and delete all ZIP files except the last 5 according to last modification date.
On the other hand the management with directories make it possible to keep backups from several days like one month and just delete the backup of previous month from time to time either manually or automatically for example on 15th of every month.

dos batchfile to zip folders based on timespan

I am looking for a way to zip folders based on a timespan of their lastmodified date and remove them afterwards
the structure is as follows
Folder_1 01.01.2012 12:00
- sub1
- sub2
- file1
- file2
Folder_2 01.01.2012 12:02
...
Folder_20202 05.05.2012 03:00
I want now:
to select all folders that are 3 months older than this month
(e.g. lastmodified is february when current is may). An importand point is that I want to use 1st to last of month and not -90 days
then zip the selected folders with all subfolders and files contained into one february2012.zip
delete the originals of the (now) zipped files
my best idea so far is to use forfiles.exe (which takes only one date), use a generated date (1st day of 3 month ago and use the older than modifier) and create a resultextfile. Then use the resultfile as import for 7zip.exe and somehow loop over the file and delete the folders afterwards (for part 2&3, I have not yet a working code)
-- UPDATE --
At the moment I am experimenting with:
FORFILES /P H:\Temp\2zip /M O* /D -31.4.2012 /C "cmd /c C:\Program Files (x86)\7-Zip\7z.exe a -t7z H:\temp\x2.zip #path
which seems to work - but just very slowly (I guess reason is mapped drive -> 3h for 150MB)
Be careful when working with last modified date of folders. On a FAT file system, the time stamp does not change when the contents of the folder change.
On a NTFS file system, the last modified date is updated whenever a file or folder is created or deleted within the folder. But it is not updated if a file is modified, or if a file/folder is created or deleted in a sub folder.
If you still want to move forward with your plan, then your basic strategy is pretty solid. Windows batch has very weak support for dealing with dates, so there is not much room for improvement.
The best way to parse the current date (and time if you want it) regardless of locale is to use WMIC:
for /f "skip=1" %%A in ('wmic os get localdatetime') do (
set currentDateTime=%%A
goto :exitLoop
)
:exitLoop
set year=%currentDateTime:~0,4%
set month=%currentDateTime:~4,2%
You should be able to use a move option of 7zip such that you don't need to delete the folder afterwards.
You should be able to eliminate the need for a temp file by processing the results of the FORFILES with a FOR /F. Or better yet, you may be able to specify the appropriate 7zip command with the FORFILES /C option.

Resources