问题
I am using a Robocopy command on a Windows Server 2003 server to copy a series of EDB files to special folders from user workstations onto a server. I want to run the robocopy commands twice, once in the mid-morning and once in the afternoon.
The way I KNOW how to do this would be to write two independent batch files that are scheduled to run at different times. Each batch would copy the EDBs to different directories.
But it occurred to me I should be able to do this in one batch file by:
- Check the current time.
- Note whether it's before 1200pm or after 1200pm.
- If it's before 1200pm, run this set of Robocopy commands.
- If it's after 1200pm, run the other set of Robocopy commands.
I am going to implement this the way I know how to do it, with the two batch files. I'd like to learn how to do it in other ways. I am open to doing this in any way -- Powershell, Python, etc. Admittedly, I am leery of installing anything on this production server that I wouldn't normally have to install. For instance, I could install Python, but it would be for this job only and that seems a little like overkill. (Feel free to disabuse me!)
回答1:
There are probably several way to do what you are asking. The first part, running different codes depending on the time of day is pretty straight forward. Just use this:
if ( (Get-Date -UFormat %p) -eq "AM" ) {
<Code if doing before noon>
} #End if
else {
<code if doing after noon>
} #end else
You can run robocopy
commands in Powershell without any fancy tricks. Here is a link to a question about robocopy.
As far as scheduling the task, this link will show you how to schedule the powershell script with the task scheduler.
To get anything else, your going to have to do some trial and error, then come back with failures or roadblocks to get more help.
回答2:
Another possibility:
Switch ((get-date).tostring('tt'))
{
'AM' {'Morning script'}
'PM' {'Afternoon script'}
}
回答3:
You could use something like this
set t=%time:0,2%
if %t% lss 12 (
REM First set of robocopy commands here
) else (
REM Second set of robocopy commands here
)
来源:https://stackoverflow.com/questions/14630237/checking-time-and-executing-different-instrcutions-based-on-hour