Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch programming  (Read 4889 times)

0 Members and 1 Guest are viewing this topic.

trips21

  • Guest
batch programming
« on: June 29, 2016, 02:52:31 AM »
Hi,

I have folders named like this 201601,201501,201602,201603 , basically yyyymm as the folder name. considering for today's month the folder will be 201606. I need to delete all the folders which are 6 months older than 201606. Also, I don't want to delete the folders by the modified date but by checking the month and year in the folder name and comparing it with current month's folder year and month and delete if the folder is more than 6 months old.

Salmon Trout

  • Guest
Re: batch programming
« Reply #1 on: June 29, 2016, 09:51:17 AM »
Are the folders all under one top folder?

trips21

  • Guest
Re: batch programming
« Reply #2 on: June 29, 2016, 10:07:21 AM »
Folders are inside E:\output ,
Basically like this:
 E:\output\201512
 E:\output\201601
 E:\output\201602
......
...
 E:\output\201606


Salmon Trout

  • Guest
Re: batch programming
« Reply #3 on: June 29, 2016, 10:09:19 AM »
Do you want to manually input the current month or have the script calculate it?

trips21

  • Guest
Re: batch programming
« Reply #4 on: June 29, 2016, 10:11:48 AM »
I get the current month yyyymm that is 201606 as a user specified input parameter to the batch script.

Salmon Trout

  • Guest
Re: batch programming
« Reply #5 on: June 29, 2016, 10:13:27 AM »
The user types (e.g.) 201606 at a prompt?

trips21

  • Guest
Re: batch programming
« Reply #6 on: June 29, 2016, 10:19:51 AM »
Yes.
Call the script : C:\Xyz.bat 201606

Inside the script
Set date= %1

Which sets date as 201606, and I use %date% throughout as a reference to user defined yyyymm input.

Salmon Trout

  • Guest
Re: batch programming
« Reply #7 on: June 29, 2016, 10:32:22 AM »
Better not to use %date%, as that is a reserved system variable (also %time%).

Salmon Trout

  • Guest
Re: batch programming
« Reply #8 on: June 29, 2016, 10:47:51 AM »
Batch/VBscript hybrid

There should NOT be ANY other folders except those named YYYYMM

This may need changing if your system date format is not dd/mm/yyyy

@echo off
setlocal enabledelayedexpansion
echo wscript.echo datediff("m", wscript.arguments(1),wscript.arguments(0)) > monthdiff.vbs
set thismonth=%1
set thismonthdate=01/%thismonth:~4,2%/%thismonth:~0,4%

echo This month: %thismonth%
echo Using date: %thismonthdate%

for /f "delims=" %%A in ('dir /b /ad') do (
      set foldername=%%A
      set folderdate=01/!foldername:~4,2!/!foldername:~0,4!
      for /f "delims=" %%B in ('cscript //nologo monthdiff.vbs "%thismonthdate%" "!folderdate!"') do set folderage=%%B
      if !folderage! gtr 6 (set action=delete) else (set action=keep)
      echo Folder %%A is !folderage! months old, action: !action!
      if "!action!"=="delete" (
                       echo deleting !foldername!
                        REM To avoid "Are you sure" prompt:
                        REM use rd /s /q "!foldername!"

                        REM Test script first, and if it works OK remove ECHO
                        ECHO rd /s "!foldername!"
         
                        )
   )
   echo Complete
pause

« Last Edit: June 29, 2016, 10:59:49 AM by Salmon Trout »

trips21

  • Guest
Re: batch programming
« Reply #9 on: June 29, 2016, 11:09:06 AM »
Hey,

Thanks a lot . This works..!!! :)

I have one more question similar to this for the files deletion based on dates in the file name. Will post in a while. :)

trips21

  • Guest
Re: batch programming
« Reply #10 on: June 29, 2016, 11:55:06 AM »
Extension: To delete the files in a specific folder :
File name is like this xyz_yyyymmdd.dat.gz

Say in a folder E:\ZippedFiles, I have files like these present
xyz_20160526.dat.gz     
xyz_20160627.dat.gz     
xyz_20160528.dat.gz
xyz_20160529.dat.gz     
xyz_20160630.dat.gz     
xyz_20160531.dat.gz
xyz_20160601.dat.gz
xyz_20160602.dat.gz
xyz_20160603.dat.gz



File xyz_20160529.dat.gz  is a Sunday File and xyz_20160531.dat.gz is the last day of the previous month file.

Expectations:
To move (not copy) the Sunday file and last day of previous month file to E:\Backup

Conditions:

1. Batch file should be configurable to pick a day of week for running the script and is dependent on a user fed variable no_of_days.
Lets say I am running the script on a Friday, so if I pass a variable no_of_days==5, it will backup sunday file.
if on a thursday , then no_of_days becomes 4
basically current_day - no_of_days= Sunday (current day minus no of days should return a sunday) and backup the sunday file.

2. Also, after moving files 20160529 and 20160531. It should not delete any files for the current week but it should delete all the files of the previous week except sunday file if present.
Example: files 20160530,20160602,20160603,20160604 are left in the current folder whereas 20160526,20160527,20160528 are deleted. files 20160529(sunday) and 20160531 are moved to back up folder.

3. after running the batch script expected:

New location: E:\Backup\xyz_20160529.dat.gz and E:\Backup\xyz_20160531.dat.gz

deleted files: E:\ZippedFiles\xyz_20160526.dat.gz, E:\ZippedFiles\xyz_20160627.dat.gz ,E:\ZippedFiles\xyz_20160528.dat.gz   

Not modified:  E:\ZippedFiles\xyz_20160530.dat.gz, E:\ZippedFiles\xyz_20160601.dat.gz, E:\ZippedFiles\xyz_20160602.dat.gz, E:\ZippedFiles\xyz_20160603.dat.gz,
   



Salmon Trout

  • Guest
Re: batch programming
« Reply #11 on: June 29, 2016, 12:01:12 PM »
So no_of_days is the day number, counting from Monday=1?

Salmon Trout

  • Guest
Re: batch programming
« Reply #12 on: June 29, 2016, 12:01:43 PM »
A quick question: is this stuff for your job?

trips21

  • Guest
Re: batch programming
« Reply #13 on: June 29, 2016, 12:09:04 PM »
Yes . I do have this one figured out to a certain extent but the script doesn't look very neat since I have just started with batch and Vb scripting.

Answering the other question:
No of days is passed to calculate the Sunday date.

C:\xyz.bat  today's_date   no_of_days  (2 parameters)

if today's date is 20160603 then no_of_days  =5  so that 20160603 - 5 (days)== 20160529 (sunday)
if today's date is 20160602 then no_of_days  =4  so that 20160602 - 4 (days)== 20160529 (sunday)



Salmon Trout

  • Guest
Re: batch programming
« Reply #14 on: June 29, 2016, 12:42:00 PM »
A script can know today's date, today's day number (1 to 7),  and the date of last Sunday without any user input.