1

My conundrum is related to the q/a thread at the following link: How to append date to directory path in xcopy

I'm new to this forum, and I had the same kind of question, and I'm using Windows 10, so I used the answer given in that thread by foxidrive about how to use WMIC for this, and it works fabulously, except for one issue that I've not yet figured out...

I modified the script that foxidrive provided, as follows:

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,2%
set YY=%dt:~2,2%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
for /f "tokens=%dow%" %%a in ("Su Mo Tu We Th Fr Sa") do set day=%%a

set stamp=%YY%%MM%%DD%%HH%%Min%%day%

REM echo Today is %day%.

md "%stamp%MoreDirName"
xcopy %source% /E /y .\"%stamp%MoreDirName"

When I run the batch file from cmd.exe, I get the desired result, namely, a directory is created with the date format the way I want it, and the date and time stamp that I want includes the name of the day of the week. However, when I double-click on the batch file in windows explorer, the folder is created and folders/files are copied, but the name of the day of the week does not appear in the new folder name. I am confused by this behavior and I would like to know how to override it, please.

I would have researched the issue more, but I am not sure what to search for other than ``different behaviors of WMIC in command line and windows'', and such a search yielded no helpful results. But since my efforts were based specifically upon the referenced stack exchange q/a thread, it seems to me that this is an appropriate place to document this strange behavior and get explanations if possible, which might help me, and others later, to compose better script.

Community
  • 1
  • 1
ProfInsall
  • 13
  • 2

1 Answers1

2

I am confused by this behavior and I would like to know how to override it

There is no problem with wmic. The Issue is with your batch file, which contains two undefined variables).

for /f "tokens=%dow%" %%a in ("Su Mo Tu We Th Fr Sa") do set day=%%a

You don't set dow anywhere in your batch file.

xcopy %source% /E /y .\"%stamp%MoreDirName"

You also don't set source anywhere in your batch file.

What probably happened:

You have dow and source hanging about in your cmd environment from another batch file you have run that does not include the setlocal command (which prevents variables leaking into the parent cmd shell).

That means:

  • The batch file run from a cmd shell will work if dow and source are set in that instance of cmd.

  • The batch run from explorer won't work because it start a new instance of the cmd shell and dow and source are undefined.

Corrected batch file:

Here is a modified version of your batch file that will work when run from a cmd shell or from explorer, and correctly sets up Day of the Week.

rem @echo off
setlocal
set source=SomeSourceValue
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-6" %%g in (`wmic Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _hours=00%%h
  set _minutes=00%%i
  set _month=00%%j
  set _seconds=00%%k
  set _year=%%l
  )
rem pad with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
set _hh=%_hours:~-2%
set _mm=%_minutes:~-2%
set _ss=%_seconds:~-2%

rem get day of the week
for /f %%k in ('powershell ^(get-date^).DayOfWeek') do (
  set _dow=%%k
  )
set _stamp=%_year%%_month%%_day%%_hh%%_mm%%_dow:~0,2%
md "%_stamp%MoreDirName"
xcopy %source% /E /y .\"%_stamp%MoreDirName"
endlocal

Notes:

  • The batch file uses a more elegant way to retrieve the timestamp components from wmic.
  • Modify the above as appropriate to set source correctly.

Credits:


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
  • setlocal - Set options to control the visibility of environment variables in a batch file.
  • variables - Extract part of a variable (substring).
  • wmic - Windows Management Instrumentation Command.
Community
  • 1
  • 1
DavidPostill
  • 7,120
  • 9
  • 35
  • 51
  • even more elegant: `for /f %%g in ('wmic Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /value ^|find "="') do set _%%g` – Stephan Aug 15 '16 at 08:06
  • it works exaclty like yours. The leading zeros are the reason, why I personally prefer [wmic os get localdatetime](http://stackoverflow.com/a/18024049/2152082) and disassemble it to my needs. – Stephan Aug 15 '16 at 08:20
  • @Stephan Ah. Thanks. Cool hack :) – DavidPostill Aug 15 '16 at 08:23
  • Thank you, Davidl and @stephan. I have not tried Stephan's hack, but David's solution worked for me. Have a great day! – ProfInsall Aug 16 '16 at 15:44
  • @ProfInsall I'm happy it worked for you. Please don't forget to [accept my answer](http://superuser.com/help/accepted-answer). – DavidPostill Aug 16 '16 at 16:23
  • Now I've used @stephan's hack, and it works quite well. Again, thank you both! – ProfInsall Aug 16 '16 at 16:33
  • Sorry it took me a while, David... I just noticed the "accept my answer", and it then took me a while to figure out how to do so... – ProfInsall Mar 11 '17 at 05:43