批处理文件以查找当前日期(日-月)并相应地显示结果
Batch file to find current date (day-month) and showing result accordingly
我正在创建一个批处理文件,根据当前工作日复制特定文件。效果不错!
但是,我想添加另一个功能来检查当前是哪个月份和日期,并在执行复制时打印特定日期消息(例如当前生日)。我怎么能做到这一点?我不介意以我在不同工作日所做的方式为一年中的每一天添加行。
Set "_=mon tues wed thurs fri sat sun"
For /f %%# In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do (
Set DOW=%%#)
echo %DOW% >NUL
IF %DOW%==0 (goto sunday)
IF %DOW%==1 (goto monday)
IF %DOW%==2 (goto tuesday)
IF %DOW%==3 (goto wednesday)
IF %DOW%==4 (goto thursday)
IF %DOW%==5 (goto friday)
IF %DOW%==6 (goto saturday)
goto finish
:monday
echo Copying files for monday
copy test.file testmonday.file /b/v/y >NUL
goto finish
:tuesday
echo Copying files for tuesday
copy test.file testtuesday.file /b/v/y >NUL
goto finish
:wednesday
echo Copying files for wednesday
copy test.file testwednesday.file /b/v/y >NUL
goto finish
:thursday
echo Copying files for thursday
copy test.file testthursday.file /b/v/y >NUL
goto finish
:friday
echo Copying files for friday
copy test.file testfriday.file /b/v/y >NUL
goto finish
:saturday
echo Copying files for saturday
copy test.file testsaturday.file /b/v/y >NUL
goto finish
:sunday
echo Copying files for sunday
copy test.file testsunday.file /b/v/y >NUL
goto finish
:finish
echo Files copied succesfully!
timeout /t 2 /nobreak >nul```
重复的代码很难维护。最好使用循环并重新使用代码:
@echo off
setlocal
Set "Days=Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
For /f %%# In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do Set /a DOW=%%#+1
for /f "tokens=%DOW%" %%# in ("%Days%") do set "WeekDay=%%#"
echo copying files for %WeekDay%
ECHO copy /b/v/y "test.file" "test%WeekDay%.file"
for /f "tokens=2 delims==" %%# in ('wmic os get LocalDateTime /value') do set "dt=%%#"
set "DateMonth=%dt:~4,2%
set "DateDay=%dt:~6,2%"
echo Today is %WeekDay%, %DateDay%.%DateMonth%. Let me remind you:
findstr /lc:"%dateDay%.%DateMonth%." "Birthdays.txt"
输出:
copying files for Thursday
copy /b/v/y "test.file" "testThursday.file"
Today is Thursday, 21.01. Let me remind you:
21.01.2021 I answered a question on SO
(注意:我只是通过回显来解除 copy
命令。删除 ECHO
以启用它)
我敢肯定,您可以根据需要调整“Day.Month”字符串。
我正在创建一个批处理文件,根据当前工作日复制特定文件。效果不错!
但是,我想添加另一个功能来检查当前是哪个月份和日期,并在执行复制时打印特定日期消息(例如当前生日)。我怎么能做到这一点?我不介意以我在不同工作日所做的方式为一年中的每一天添加行。
Set "_=mon tues wed thurs fri sat sun"
For /f %%# In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do (
Set DOW=%%#)
echo %DOW% >NUL
IF %DOW%==0 (goto sunday)
IF %DOW%==1 (goto monday)
IF %DOW%==2 (goto tuesday)
IF %DOW%==3 (goto wednesday)
IF %DOW%==4 (goto thursday)
IF %DOW%==5 (goto friday)
IF %DOW%==6 (goto saturday)
goto finish
:monday
echo Copying files for monday
copy test.file testmonday.file /b/v/y >NUL
goto finish
:tuesday
echo Copying files for tuesday
copy test.file testtuesday.file /b/v/y >NUL
goto finish
:wednesday
echo Copying files for wednesday
copy test.file testwednesday.file /b/v/y >NUL
goto finish
:thursday
echo Copying files for thursday
copy test.file testthursday.file /b/v/y >NUL
goto finish
:friday
echo Copying files for friday
copy test.file testfriday.file /b/v/y >NUL
goto finish
:saturday
echo Copying files for saturday
copy test.file testsaturday.file /b/v/y >NUL
goto finish
:sunday
echo Copying files for sunday
copy test.file testsunday.file /b/v/y >NUL
goto finish
:finish
echo Files copied succesfully!
timeout /t 2 /nobreak >nul```
重复的代码很难维护。最好使用循环并重新使用代码:
@echo off
setlocal
Set "Days=Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
For /f %%# In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do Set /a DOW=%%#+1
for /f "tokens=%DOW%" %%# in ("%Days%") do set "WeekDay=%%#"
echo copying files for %WeekDay%
ECHO copy /b/v/y "test.file" "test%WeekDay%.file"
for /f "tokens=2 delims==" %%# in ('wmic os get LocalDateTime /value') do set "dt=%%#"
set "DateMonth=%dt:~4,2%
set "DateDay=%dt:~6,2%"
echo Today is %WeekDay%, %DateDay%.%DateMonth%. Let me remind you:
findstr /lc:"%dateDay%.%DateMonth%." "Birthdays.txt"
输出:
copying files for Thursday
copy /b/v/y "test.file" "testThursday.file"
Today is Thursday, 21.01. Let me remind you:
21.01.2021 I answered a question on SO
(注意:我只是通过回显来解除 copy
命令。删除 ECHO
以启用它)
我敢肯定,您可以根据需要调整“Day.Month”字符串。