Link to home
Start Free TrialLog in
Avatar of Luis_Romero
Luis_Romero

asked on

How to modify a BAT scrip to delete files so that files in folders that cannot be remove are not deleted when the screen is executed

I created a BAT file to delete files from folders using  https://www.experts-exchange.com/questions/27008284/Batch-Script-to-Delete-files-from-Folders-and-Sub-Folders-Older-than-2-months-old.html?anchorAnswerId=35689082#a35689082 . Now I am in need to modify the script since there are some folders under the main folder that contain files that cannot be removed. Also there are subfolders of subfolders that have files that will need to remain after the script is executed.

The modification can be name or path specific, I just need to make sure that the folders that have those files remain intact after I run the script.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis_Romero
Luis_Romero

ASKER

Tested the new script and I changed the name for the ExcludeDirs variables. When I ran the script, it still deleted the files even from those folders. I tested it with leaving the "" marks and replacing the name, and by just by replacing the name with no "". Here is what I have:

@echo off
setlocal EnableDelayedExpansion

REM Define base for folders, and days to keep old folders, and folders not to delete files from
set BaseDir=C:\Users
set DaysToKeep=11
set ExcludeDirs=OrderStatus, OrderAck, Processed

REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Process all Files in the directory, delete if old
for /R "%BaseDir%" %%A in ("*.*") do (
  REM Converty file date stamp to julian
  call :jDate jFile %%~tA

  REM Calculate the age of the file in days
  set /A FileAge = !jToday! - !jFile!

  REM Is it old enough to consider for delete?
  if !FileAge! GTR %DaysToKeep% (

    REM Get the name of the folder that this file resides in
    set MyDir=%%~dpA
    for %%B in ("!MyDir:~0,-1!") do set MyDir=%%~nB

    REM See if its folder is in the list to exclude
    set DoDelete=Y
    for %%C in ("%ExcludeDirs%") do (
      if /i "!MyDir!" EQU "%%~C" set DoDelete=N
    )

    REM If it wasn't in an exclude folder, go ahead and delete it
    if "!DoDelete!" EQU "Y" (
      ECHO File:[%%A] is [!FileAge!] days old and would be deleted
      del "%%~A"
    )
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY)
  set DateStr=%~2
  set yy=%DateStr:~6,4%
  set /A mm=1%DateStr:~0,2%-100
  set /A dd=1%DateStr:~3,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

The files were removed from the folders that I defined as folders that need it to be excluded.
I'll try and set up a test here and see if it works.

For a test there, can you add the following DEBUG line to the script you have, run it, and post up a handful of the DEBUG output lines please.

for %%C in ("%ExcludeDirs%") do (
  ECHO DEBUG:[%%~fA],[!MyDir!],[%%~C]
  if /i "!MyDir!" EQU "%%~C" set DoDelete=N
)

Open in new window

~bp
I added the line to the script and this is the results that I got. I omitted some of the folder names since they are from customers so the ...... mean that they are names of folders and files.

DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\.........._2010-11-09-11-00-15.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-10-11-00-22.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-11-11-00-18.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-12-11-00-25.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-13-11-00-14.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-14-11-00-22.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-15-11-00-16.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-16-11-00-24.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]
DEBUG:[C:\Users\.....\.....\Test FTP\FTP Test\....\OrderStatus\..........2010-11-17-11-00-22.csv],[OrderStatus],[OrderStatus, OrderAck, Processed]

I hope that this will help.
Yes, that helps, I'll dig in further tomorrow.

~bp
I see it, please change this line:

   for %%C in ("%ExcludeDirs%") do (

to:

   for %%C in (%ExcludeDirs%) do (

~bp
Thanks for all the help.
Welcome.

~bp