Creating filename with date in it from batch

I'm trying to create a file from a file, actually coping from another source (the commands work good with a fixed name) but i need the filename to contain de date in it, and i cant get this function to work, see code please:

SETLOCAL EnableDelayedExpansion
    for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
        IF NOT "%%~f"=="" (
            set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
            set FormattedDate=!FormattedDate:~-2,2!/!FormattedDate:~-4,2!/!FormattedDate:~-6,2!
        )
    )    
copy "File.xlsx"  "C:\Documents and Settings\%USERNAME%\Desktop\File_%!FormattedDate:~-2,2!%_%!FormattedDate:~-4,2!%_%!FormattedDate:~-6,2!%.xlsx" 

Open in new window


I tried without % % too

a simpler approach would be acceptable too
LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Frank McCourryV.P. Holland Computers, Inc.Commented:
This is a routine I use to name folders, but the principal is the same.  Just make the appropriate changes to apply it to your filename

::USE TIME AND DATE TO SET FOLDER NAME TO BE USE AS DESTINATION
::Fix leading 0 in time if the time is before 10:00 or after 12:00
set hh=%time:~0,2%
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set time0=%hh%%TIME:~3,2%%TIME:~6,2%
:: Set the Variable dirname as date and time, formatting in a way that allows it to be used as a folder name
set dirname="%date:~-4,4%-%date:~-10,2%-%date:~7,2%-%time0:~0,2%.%time0:~2,2%.%time0:~4,2%"
0
alicainCommented:
Hello!

As an alternative way of doing this, the following is derived from here: http://gallery.technet.microsoft.com/scriptcenter/0595e5d5-184c-44ab-847e-056c2db3c253


For /F "Tokens=1-7 Delims=/:. " %%d In ("%Date%/%Time%") Do Set Now=%%f%%e%%d-%%g%%h
Echo This would be the filename : %now%-filename.txt

Regards,
Alastair.
0
celticianAuthor Commented:
Thats good frank, based on your comment ivebeen able to write it, but i just need the year in the short format YY and not YYYY

how do i get this??

my current command is this and i get yyyy_mm_dd:

copy "File.xlsx"  "C:\Documents and Settings\%USERNAME%\Desktop\File_%date:~10,4%_%date:~4,2%_%date:~7,2%.xlsx" 

Open in new window


need yy_mm_dd instead
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Steven CarnahanNetwork ManagerCommented:
Re-writting your original code I came up with this:

@echo off
SETLOCAL EnableDelayedExpansion
    for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
        IF NOT "%%~f"=="" (
            set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
           set FormattedDate=!FormattedDate:~-2,2!/!FormattedDate:~-4,2!/!FormattedDate:~-6,2!
          set FormattedDate=!FormattedDate:~-2,2!_!FormattedDate:~-5,2!_!FormattedDate:~-8,2!
        )
    )    
copy "File.xlsx"  "C:\Documents and Settings\%USERNAME%\Desktop\File_!FormattedDate:~-2,2!_!FormattedDate:~-5,2!_!FormattedDate:~-8,2!.xlsx" 

Open in new window

0
Steven CarnahanNetwork ManagerCommented:
Or to shorten it:

@echo off
SETLOCAL EnableDelayedExpansion
    for /f "skip=1 tokens=1-6 delims= " %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do (
        IF NOT "%%~f"=="" (
            set /a FormattedDate=10000 * %%f + 100 * %%d + %%a
           set FormattedDate=!FormattedDate:~-6,2!_!FormattedDate:~-4,2!_!FormattedDate:~-2,2!
        )
    )    
echo !FormattedDate!
copy "File.xlsx"  "C:\Documents and Settings\%USERNAME%\Desktop\File_!FormattedDate!xlsx" 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
Pony i noticed the last command works just fine:
copy "File.xlsx"  "C:\Documents and Settings\%USERNAME%\Desktop\File_%date:~10,4%_%date:~4,2%_%date:~7,2%.xlsx"


Now i only need to enter the short year form, now written by the %date:~10,4% variable, that i cant get to understand fully.
0
Steven CarnahanNetwork ManagerCommented:
If you use the shortened version what is the name of the file after the rename?  Excuse me - I mean copy.
0
Steven CarnahanNetwork ManagerCommented:
The question you had though is what is the %date:~10,4% variable doing.

The first number - 10 - is counting forward to start at the 10th character and then reading 4 characters into the variable.

So if you want to read just the last two characters of the year then use 12,4.
0
Steven CarnahanNetwork ManagerCommented:
I meant 12,2
0
celticianAuthor Commented:
Im having some trouble with this, in some computer works, in others, it writes a /_ instead of just _, that "/" is coming from the date
0
Steven CarnahanNetwork ManagerCommented:
Whos/which solution are you using?  If you are using the single line that you have above then on the computer that isn't working do this:

1. open command prompt window
2. type "echo %date%" without the quotes
                My system returns "Fri 08/30/2013" (again without the quotes)

So counting from Left to right the two digit year would be in position 13 and 14. So you would want the command to be date:~12,2 - move to the 12 position and read the next 2 characters.

The month would be in position 5 and 6. So you would want the command to be date:~4,2 - move to the 4 position and read the next 2 characters

Finally the day would be in position 8 and 9 so you would move to position 7 and read the next 2 characters or date:~7,2
0
Steven CarnahanNetwork ManagerCommented:
Part of the issue with using the one line command (using %date% variable) is that it may not always return in the format you expect.  It uses the windows settings and is controlled by the user.

Your original code was close to what you want and is less likely to return different results.  That is why I focused on using your original code as I saw that you were headed in the proper direction.  :)

Again, if you use the second (shortened version) that I posted it should work just the way you want.
0
Steve KnightIT ConsultancyCommented:
Personally I would avoid %date% in any form, or indeed date /t as it generally breaks in some form for different OS, date format or the like.  Even different users on the same machine, e.g. schedule it as different user or local system and it uses different date format and it breaks...

So would suggest using your wmic approach tidied up or I generally prefer VBScript embedded in the batch as VBS as in my article here:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

I think you say you want yymmdd which would be:

@echo off
echo wscript.echo right(year(date) ,2) ^& right(100 + month(date),2) ^& right(100+day(date),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yymmdd=%%a

echo Today is %yymmdd% which you can use in your script

Open in new window


If not I give in the article above code to make any combination of date and time bits.

Steve
0
celticianAuthor Commented:
Out of all the options tried this seems to be the best, as its user-portable
0
Steve KnightIT ConsultancyCommented:
I use that too, though be careful on some machines where WMI isn't available, though same can be said for VBScript options...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.