Solved

Script to append date to folder and delete old folders.

Posted on 2010-11-17
15
684 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

15 Experts available now in Live!

Get 1:1 Help Now