user3854635
user3854635

Reputation: 21

how to make folder name with yesterday's date?

I want to do two things:

but by running batch code today (2014_07_18)

Upvotes: 2

Views: 4616

Answers (2)

AndrewInCA
AndrewInCA

Reputation: 1

This was fun. Below is a brainstorm of how you could get to your goal -- there are some flaws that need to be fixed (they would also be fun to figure out), such as:

If it's the 1st of the month, you'll need to change the month value (e.g. 08012014 to 07312014). You'll also need to factor in months with varying total number of days (e.g. 08012014 to 07312014, 07012014 to 06302014, 03012014 to 02282014).

C:\example>echo %date%

Sat 08/16/2014

C:\example>echo %date:~-10,2%%date:~-7,2%%date:~-4,4%

08162014

C:\example>set /a today=%date:~-7,2%

16 C:\example>set /a yesterday=%today%-1

15

C:\example>echo %date:~-10,2%%yesterday%%date:~-4,4%

08152014

C:\example>set ydate=%date:~-10,2%%yesterday%%date:~-4,4%

C:\example>mkdir %ydate%

C:\example>dir

Volume in drive C has no label.

Volume Serial Number is 8E75-333E

Directory of C:\example

08/16/2014 07:29 PM .

08/16/2014 07:29 PM ..

08/16/2014 07:29 PM > > 08152014

           0 File(s)              0 bytes
           3 Dir(s)  938,853,076,992 bytes free

C:\example>

Upvotes: 0

npocmaka
npocmaka

Reputation: 57322

@if (@X)==(@Y) @end /* jsctipt comment 

    @echo off
    for /f "tokens=* delims=" %%d in ('cscript //E:JScript //nologo "%~f0"') do (
        set "yesterday=%%d"
    )
    echo %yesterday%
    md %yesterday% >nul 2>&1
    exit /b 0

end of jsccript comment */

var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var dd = yesterday.getDate();    
var mm = yesterday.getMonth()+1;                    
var yyyy = yesterday.getFullYear();    
if(dd<10){dd='0'+dd} 
if(mm<10){mm='0'+mm}
yesterday = yyyy+'_'+mm+'_'+dd;

WScript.Stdout.WriteLine(yesterday);

save this as .bat

Upvotes: 2

Related Questions