简体   繁体   中英

Batch file not creating valid date variables after migration to Windows Server 2012 R2

I was successfully using the following .bat batch file to run a .php script from a Scheduled Task on a Windows Server 2003 box:

@echo off

set phpdir=D:\PHP
set appclidir=D:\App\cli
set cronfolder=E:\CronLogs

pushd %cronfolder%

echo cronfolder is %cronfolder%

:: Start Time Function
Set "WT=%Time%"
Set time.HH=%WT:~0,2%
Set time.MM=%WT:~0,5%

Set time.AMPM=PM
If /I %time.HH% LSS 10 (
   Set time.HH=0%time.HH:~1,1%
)
If /I %time.HH% LSS 12 (
   Set time.AMPM=AM
   Set time.HH12=%time.HH%
)  Else (
   Set /a time.HH12=%time.HH%-12
)
If /I %time.MM% LSS 10 (
   Set time.MM=0%time.MM:~1,1%
)
:: End Time Function

:: Start Date Function
Set yyyy=%DATE:~2,2%
Set mm=%DATE:~5,2%
Set dd=%DATE:~8,2%

set currentdate=%yyyy%%mm%%dd%
echo currentdate is %currentdate%
:: End Date Function

:: Create a folder with todays date
if not exist %cronfolder%\%currentdate% (
    echo making folder %cronfolder%\%currentdate%
    mkdir %cronfolder%\%currentdate%
)
set filename=%currentdate%_%time.HH%.log
echo filename is %filename%

:: Run PHP cron script
%phpdir%\php.exe %appclidir%\cron.php | %appclidir%\wtee.exe -a %cronfolder%\%currentdate%\%filename%

popd

:: The End
pause
exit

It creates a folder with the current date as the folder name. Then it runs a .php cron script and echo's the output of the script to a text file that has the YYMMDD_HH.log format. A new .log file is created for each hour in the folder. (Not perfect as it is dependent on the server regional settings, but that's manageable...)

After moving the batch file to Windows Server 2012 R2 the YYMMDD_HH.log text files are not being created correctly. Instead the script is generating a one letter file (with no .log file extension) in the %cronfolder% directory (D:\\CronLogs).

Instead of D:\\CronLogs\\171103\\171103_08.log I am getting random file names like D:\\CronLogs\\u\u003c/strong> or D:\\CronLogs\\e , so there's clearly something wrong with the Time Function code that generates the date/time values.

  1. Can anyone spot the problem?
  2. Is this something that can be easily converted to a PowerShell script?

On Server 2012 R2 the value of the variable %DATE% includes the day of the week, so %DATE:~2,2% won't give you the value you expect:

C:\> 
Fri 11/03/2017
C:\> 
_i _

You can avoid the issue by assigning the last 10 characters of the date string to a temp variable first:

set "TODAY=%DATE:~-10%"
set "yyyy=%TODAY:~8,2%"
set "mm=%TODAY:~0,2%"
set "dd=%TODAY:~3,2%"

And yes, date calculations can be done far easier in PowerShell:

$currentdate = (Get-Date).ToString('yyMMdd')

AFAICS you should be able to convert your batch code to PowerShell with minimal effort.

You can use this code to always get the date and time in the exact same format no matter what version of Windows or regional settings you are using.

The format will always be in this format: 20171103084739.006000-300 YYYYMMDDhhmmss.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM