Batch Command Question

Posted on

Batch Command Question – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about windows, batch-file, , , .

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.

Solution :

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.

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

Leave a Reply

Your email address will not be published.