Search code examples
batch-filewindows-10wmic

Invalid Namespace Error while executing batch file


I have been using a batch file for the past 1 year however suddenly I started getting below error while running it:

ERROR:Description = Invalid namespace

For below block of code:

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 FileDate=!FormattedDate:~-2,2!!FormattedDate:~-4,2!!FormattedDate:~-10,4!
            set Month=!FormattedDate:~-4,2!
        )
    )

I am not that familiar with bat files & was just using the above code to get current date in required format.

System: Windows 10

Please assist. Thanks in advance.

Edit:

On a more general note I am actually trying to get 3 things from system date:

  • File Date:23032018
  • Month:03
  • Date:23Mar2018

Solution

  • Try the following code to get the variables you require:

    @Echo Off
    Set "df=%TEMP%\~foo.ddf"&Set "tf=%TEMP%\~%Random%"
    Set /A "Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12"
    (   Echo .Set InfHeader=""&Echo .Set InfSectionOrder=""&Echo .Set InfFooter="%%2"
        For /L %%A In (1 1 4) Do Echo .Set InfFooter%%A=""
        Echo .Set Cabinet="OFF"&Echo .Set Compress="OFF"&Echo .Set DoNotCopyFiles="ON"
        Echo .Set RptFileName="NUL")>"%df%"
    MakeCab /D InfFileName="%tf%" /F "%df%">Nul
    For /F "UseBackQ Tokens=2,3,5" %%A In ("%tf%") Do (Set mmm=%%A
        Set /A "mm=%%A,dd=1%%B-100,yyyy=%%C")
    Del "%tf%" "%df%">Nul 2>&1
    Set "mm=10%mm%"&Set "DStr=%dd%%mmm%%yyyy%"
    Set "FileDate=%dd%%mm:~-2%%yyyy%"&Set "Month=%mm:~-2%"
    For %%A In (FileDate Month DStr) Do Call Echo %%%%%%A%%%% = %%%%A%%
    Pause
    

    The last two lines have been added just to show you the variables which were set; do not change any of the content before that point: