Solved

Script to append date to folder and delete old folders.

Posted on 2010-11-17
15
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
LVL 56

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 56

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 70

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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