Script to append date to folder and delete old folders.

For the purposes of this post lets assume todays date is 1/1/2010

Trying to create a batch file that does the following:
1. Creates a folder with todays date appended to the end something like this "Working_1.1.2010"
2. Create a variable so that I could address the folder (For example "cd %working_folder%"
3. Delete a folder more than X days old (Lets say 10 for the purpose of this post but this must be easily changed)

Any takers?
oft_iatsAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, here's a BAT script approach to your problem. The code would be a bit less or "simpler" in VBS, but since you wanted the new folder name in an environment variable I suspect you have some other BAT commands to carry out there?

Save this off as a BAT file in the folder where the folders will be created (and later deleted), and always run it from that directory.  

It currently uses a folder name format of "Working_YYYYMMDD" for ease of sorting in Explorer, etc, but we can modify that if that format doesn't work.  Made the code just a little simpler too.

~bp

@echo off
setlocal EnableDelayedExpansion

REM Define base for folders, prefix for new folder, and days to keep old folders
Set DirPrefix=Working_
set DaysToKeep=10

REM Get todays date (YYYYMMDD), convert to julian for age checks
set Today=%Date:~-4%%Date:~-10,2%%Date:~-7,2%
call :jDate jToday %Today%

REM Create name for new folder
set NewFolder=%DirPrefix%%Today%

REM Create new folder (with YYYYMMDD on the end)
mkdir "%NewFolder%"

REM Process all directories, delete if too old
for /D %%A in (%DirPrefix%*) do (
  set DirName=%%~A
  set DirDate=!DirName:~-8!
  call :jDate jDirDate !DirDate!
  set /A FileAge = !jToday! - !jDirDate!
  if !FileAge! GTR %DaysToKeep% rd /S /Q "!DirName!"
)

REM Switch to folder where files exist
pushd "%NewFolder%"

REM Your code could go here...
DIR

REM Switch back to prior directory, exit
popd
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(YYYYMMDD) 
  set DateStr=%~2
  set yy=%DateStr:~0,4%
  set /A mm=1%DateStr:~4,2%-100
  set /A dd=1%DateStr:~6,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

Open in new window

0
 
ThommyCommented:
Type in DATE to check your date format.

If it is dd/mm/yyyy then you can create folder with actual date as follows:


Set mm=%DATE:~3,2%
Set dd=%DATE:~0,2%
Set yyyy=%DATE:~6,4%

REM Variable to adress folder
set Folder_name=%yyyy%-%mm%-%dd%

REM Create folder
mkdir %Folder_Name%

REM change to folder
cd %Folder_Name%

Open in new window

0
 
ThommyCommented:
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ThommyCommented:
Here is the solution to append date to a folder prefix...
Set Folder_Prefix=Working_

Set mm=%DATE:~3,2%
Set dd=%DATE:~0,2%
Set yyyy=%DATE:~6,4%

REM Variable to adress folder
set Folder_name=%Folder_Prefix%%yyyy%-%mm%-%dd%

REM Create folder
mkdir %Folder_Name%

REM change to folder
cd %Folder_Name%

Open in new window

0
 
oft_iatsAuthor Commented:
This is what I get.  I echoed the mm, dd, yyyy so you could see what was being output.

C:\Users\Administrator\Desktop\New Backup F1F>Create_Folder_With_Date.bat

C:\Users\Administrator\Desktop\New Backup F1F>Set mm= 1

C:\Users\Administrator\Desktop\New Backup F1F>Set dd=We

C:\Users\Administrator\Desktop\New Backup F1F>Set yyyy=/17/

C:\Users\Administrator\Desktop\New Backup F1F>REM Variable to adress folder

C:\Users\Administrator\Desktop\New Backup F1F>set Folder_name=/17/- 1-We

C:\Users\Administrator\Desktop\New Backup F1F>REM Create folder

C:\Users\Administrator\Desktop\New Backup F1F>mkdir /17/- 1-We
The syntax of the command is incorrect.

C:\Users\Administrator\Desktop\New Backup F1F>REM change to folder

C:\Users\Administrator\Desktop\New Backup F1F>cd /17/- 1-We
The system cannot find the path specified.
C:\Users\Administrator\Desktop\New Backup F1F>echo %dd%
We

C:\Users\Administrator\Desktop\New Backup F1F>echo %mm%
 1

C:\Users\Administrator\Desktop\New Backup F1F>echo %yyyy%
/17/

C:\Users\Administrator\Desktop\New Backup F1F>

Open in new window

0
 
rogerardCommented:
Code to create folders
 
ParentFolder = "C:\PathTo\Folders\"
FolderStart = "Working_"
ArchiveOlderThan = 10
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder)
objFolder.NewFolder = FolderStart & Year(Date()) & "." & Month(Date()) & "." & Day(Date())

Open in new window

Code to set environment:
 
Set WShShell = CreateObject("WScript.Shell")
Set WshSystemEnv = WshShell.Environment("USER")
WshSystemEnv("WORKING_FOLDER") = ParentFolder

Open in new window



Working on the delete
0
 
rogerardCommented:
Using previous code, to delete the folders:
 
Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = ParentFolder ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -ArchiveOlderThan, Date)  

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
         fso.DeleteFile(file.Path)
      End If
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
End Function

Open in new window

0
 
ThommyCommented:
You first have to check your date format to correctly split up your date string...

What is the result, when you execute the command "DATE" at a dos prompt???

I think your date format is like that: WED 11/17/2010

Isn't it???

So you have to change positions for string extraction in the batch file...

Assuming your actual date string is "THU 11/18/2010"

SET mm=%DATE:~4,2% => this means to extract 2 characters starting at the 4th position in your date string (first character is position 0) => result for variable mm is "11"
SET dd=%DATE:~7,2% => this means to extract 2 characters starting at the 7th position in your date string (first character is position 0) => result for variable dd is "18"
SET yyyy=%DATE:~10,4% => this means to extract 4 characters starting at the 10th position in your date string (first character is position 0) => result for variable yyyy is "2010"



Set Folder_Prefix=Working_

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

REM Variable to adress folder
set Folder_name=%Folder_Prefix%%yyyy%-%mm%-%dd%

REM Create folder
mkdir %Folder_Name%

REM change to folder
cd %Folder_Name%

Open in new window

0
 
Bill PrewCommented:
==> 3. Delete a folder more than X days old (Lets say 10 for the purpose of this post but this must be easily changed)

Do you want to key off of the date in the folder name, or the system maintained created date for that folder?

~bp
0
 
oft_iatsAuthor Commented:
@Billprew : YES, I want to delete the folder based off the folder date.  Great question!
0
 
oft_iatsAuthor Commented:
@Thommy:  Your last post works for creating but dosnt address the deletion.
0
 
ThommyCommented:
Use the following batch script from
http://forums.techguy.org/attachments/142481d1228984593/delbydate.zip
to delete folders more than X days old.

I have tested it and it works fine.

The great benefit of this script is, that it reads the "International" settings from the registry and so always displays the correct results for the date, independent of your system date settings!!!

I have adapted the script, to pass the following parameters by command line:

parameter %1 = the days to keep and
parameter %2 = folder that contains the folders to check and delete

Call it as follows:  

DelFoldersXDays 2 c:\temp
deletes all folders and subfolders in c:\temp, which are older than 2 days


DelFoldersXDays.cmd
0
 
ThommyCommented:
To call it out of my last script:
Set Folder_Prefix=Working_

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

REM Variable to adress folder
set Folder_name=%Folder_Prefix%%yyyy%-%mm%-%dd%

REM Create folder
mkdir %Folder_Name%

REM substitute c:\temp by your base folder name
call DelFoldersXDays 2 c:\temp

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.