Solved

Script to append date to folder and delete old folders.

Posted on 2010-11-17
15
694 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:oft_iats
  • 6
  • 3
  • 2
  • +2
15 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 34156425
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34156452
0
 
LVL 19

Expert Comment

by:Thommy
ID: 34156486
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
 

Author Comment

by:oft_iats
ID: 34156616
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
 
LVL 7

Expert Comment

by:rogerard
ID: 34156658
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
 
LVL 7

Expert Comment

by:rogerard
ID: 34156905
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34161999
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 34163443
==> 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
 

Author Comment

by:oft_iats
ID: 34239842
@Billprew : YES, I want to delete the folder based off the folder date.  Great question!
0
 

Author Comment

by:oft_iats
ID: 34239862
@Thommy:  Your last post works for creating but dosnt address the deletion.
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 34263975
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34264779
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34264783
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 34456608
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now