Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to make a batch file that designated as a folder name date of a previous day.How can I do?

Here is my batch file.

Set Tarih1=%date:.=%

d:\

cd \ Yeni Klasör

md %date:.=%

cd %date:.=%

thanks in advance.

share|improve this question
    
Any suggestions? –  Cell-o Feb 9 '11 at 14:34

2 Answers 2

up vote 8 down vote accepted

It's possible, but...

setlocal
set _today=%date:.=%
set _year=%_today:~0,4%
set _month=%_today:~4,2%
set _day=%_today:~6,2%

:: leap year?
set /a _yearm400=_year %% 400
set /a _yearm100=_year %% 100
set /a _yearm4=_year %% 4

if %_yearm400% equ 0 (set _leap=1) ^
else (if %_yearm100% equ 0 (set _leap=0) ^
else (if %_yearm4% equ 0 (set _leap=1) ^
else (set _leap=0)))

:: strip leading zeros
if %_month:~0,1% equ 0 set _month=%_month:~1%
if %_day:~0,1% equ 0 set _day=%_day:~1%

:: decrement day
set /a _day-=1

:: decrement month
if %_day% equ 0 (
    if %_month% equ 2 set _day=31
    if %_month% equ 3 set /a _day=28+_leap
    if %_month% equ 4 set _day=31
    if %_month% equ 5 set _day=30
    if %_month% equ 6 set _day=31
    if %_month% equ 7 set _day=30
    if %_month% equ 8 set _day=31
    if %_month% equ 9 set _day=31
    if %_month% equ 10 set _day=30
    if %_month% equ 11 set _day=31
    if %_month% equ 12 set _day=30
    if %_month% equ 1 set _day=31
    set /a _month-=1
)

:: decrement year
if %_month% equ 0 (
    set /a _year-=1
    set _month=12
)

:: format new date
if %_month% leq 9 set _month=0%_month%
if %_day% leq 9 set _day=0%_day%

echo %_year%%_month%%_day%

I suggest using a more capable programming languages - PowerShell, maybe Python or Perl.


PowerShell:

$yesterday=(get-date (get-date).AddDays(-1) -uformat %Y%m%d)
mkdir $yesterday

Python:

import os
import time
yesterday = time.strftime("%Y%m%d", time.localtime(time.time() - 86400))
os.mkdir(yesterday)

Also, when changing drives, you must use only the drive letter, without a path. d:\ should be replaced with d: ... Or just use cd /d D:\Yeni Klasör.

share|improve this answer
3  
I second POwerShell. It's available on Win 2003 and up, and runs on XP! –  sysadmin1138 Feb 9 '11 at 15:17
    
That batch file is crazy. :-) –  jftuga Feb 9 '11 at 21:16
    
Cool , Thanks for your all answers –  Cell-o Feb 10 '11 at 14:01

If you really don't want (or can't) use Powershell, then you could do it calling a .vbs (VBScript file)

File CreateYesterdayFolder.vbs

' yyyymmdd.vbs - outputs the current date in the format yyyymmdd
Function Pad(Value, PadCharacter, Length)
    Pad = Right(String(Length,PadCharacter) & Value, Length)
End Function

Dim yesterday, folderName

yesterday = Date() - 1
folderName = Pad(Year(yesterday), "0", 4) & Pad(Month(yesterday), "0", 2) & Pad(Day(yesterday), "0", 2)

Dim objFSO, objFolder, strDirectory
newDirPath = "c:\temp\" & folderName ' example 
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.CreateFolder(newDirPath)

In your batch file call it this way:

cscript CreateYesterdayFolder.vbs //NOLOGO
share|improve this answer
    
This just passes cscript, Yesterday.vbs and //NOLOGO as directory names to mkdir. –  grawity Feb 9 '11 at 18:31
    
Oops, copied it from an old script I had in a different context. I've edited my answer. –  splattne Feb 9 '11 at 20:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.