2

I want to do two things:

  • create folder name with yesterday's name - for example if today is 2014_07_18 then create folder name with 2014_07_17 under this directory "d:\test"

  • then I have some files with yesterday's date (2014_07_17) as modified date under "d:\test*.txt" which needs to transfer to newly created folder at "d:\test\2014_07_17"

but by running batch code today (2014_07_18)

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3854635
  • 21
  • 1
  • 2
  • 1
    And what you tried so far?... can we see your batch file and your aproach. Tell us what is not working in your attemp. [recommend reading](http://stackoverflow.com/help/how-to-ask) – gmo Jul 18 '14 at 22:15
  • possible duplicate of [How can I create a date stamp for yesterday](http://stackoverflow.com/questions/14897097/how-can-i-create-a-date-stamp-for-yesterday) for question #1. Once you've got that working, you can put some effort into copying the files, and then [edit] to ask a specific question about problems you're having doing so (and include your code to show your efforts). (As a tip: I found the duplicate by using the search expression `[batch-file] yesterdays date`, including the `[]` around the tag name, searching on this site.) Good luck. – Ken White Jul 18 '14 at 22:28
  • okay i am using below code to create folder with today's date like 2014_07_18 - BUT - i want to create folder name with yesterday's date by running code today... for /f "tokens=1-4 delims=/ " %%a in ('date /t') do ( set MyDayOfWeek=%%a set MyMonth=%%b set MyDay=%%c set MyYear=%%d) f: mkdir F:\test\%MyYear%_%MyMonth%_%MyDay% – user3854635 Jul 18 '14 at 23:21
  • Do you only create the folder if there are files modified yesterday? If so, you could the solution for reading a file's modified date: http://stackoverflow.com/a/2116420/2540156 – Adam47 Jul 19 '14 at 00:30
  • There is a solution here to get yesterdays date into a variable: http://stackoverflow.com/questions/20796749/re-naming-a-file-name-to-include-yesterdays-date-using-command-prompt/20798129#20798129 – foxidrive Jul 19 '14 at 05:52
  • Thanks a lot..."foxidrive" - it helped to create same...i just did add mkdir command into it ...and magic...:-) – user3854635 Jul 19 '14 at 18:34

2 Answers2

2
@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

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

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>

Sagar Sakre
  • 2,336
  • 22
  • 31