Computer Hope
Microsoft => Microsoft DOS => Topic started by: RonC on April 26, 2018, 05:19:34 PM
-
Hello all;
I am trying to write a backup batch file and I want the code to direct the backup to a specific directory based on the day of the week, Monday, Tuesday, etc.
I believe the coding should look something like:
if dayname = Monday then
copy *.* f:\monday /y
else
if dayname = Tuesday then
copy *.* f:\tuesday /y
etc, etc for Monday through Friday with an end if at the end.
Can anyone provide me with the proper nomenclature? I volunteer for a non-profit and I'm trying to get them to make sure and backup their files.
Thanks in advance for all the help.
Regards,
Ronc
-
I just read the readme file for this forum and need to add; I am using Windows10 and will create my bat file using Notepad then save it as a .bat file. The program will be very straightforward; change to the proper directory, etc then use copy (or xcopy) to copy the files to the backup directory.
Thanks again.
RonC
-
Open up a cmd prompt and type: IF /?
This will show you the syntax for the IF command.
-
I had syntax on my waffles this mornin...
-
You don't need else in batch to do this. End If and Then are not keywords in batch. IF tests can be one line.
Variable names have percent signs before and after and it is often preferred to quote both sides of an IF test...
The equality test operator is a double equals sign ==
The IF test is case sensitive unless you use the /i switch. That is:
if "%dayname%" is "Monday":
if "%dayname%"=="monday" will FAIL
if /i "%dayname%"=="monday" will PASS
if "%dayname%"=="Monday" copy *.* f:\monday /y
if "%dayname%"=="Tuesday" copy *.* f:\tuesday /y
...etc...
HOWEVER!!! If you have already have five dayname folders, and %dayname% is only ever one of the five days of the work week, why not just do this? This method does not care about case.
copy *.* f:\%dayname% /y
-
8)
-
Variable names have percent signs before and after
Sometimes they have exclamation points instead !varname! but that is a more advanced topic.
-
HOWEVER!!!
Salmon's motto: think about what you want the script to do and then write the code. I have to say that I often fail to see a simpler way of doing something until I have already done it a complicated way.
-
I should make your motto a sticky for the DOS Forum....
-
I don't think there's anything wrong with thinking it out as you go along, but it's good to take a good look at your code and see if you can reduce the number of lines, as long as you can still remember what it does in 6 months time!
certainly
if %food%==bacon copy *.* d:\bacon
if %food%==eggs copy *.* d:\eggs
if %food%==ham copy *.* d:\ham
can all be collapsed into
copy *.* d:\%food%
as long as there are exactly the same number of folders as there are possible foods, and the food variable value will always be one of them. There is a site called The Daily *censored* where contributors share code that actually made into production where the long way around was taken, especially, it seems with dates. Also things like if number=4 then value="four"...
-
A big thank you to Salmon Trout and Patio. I'm a real novice at DOS and only use it for backups so you have been a tremendous help.
RonC
-
Keep in mind this is how we all learned...Salmon is a better student than i...
-
My problem has been solved, is there a way to close the thread and mark it as resolved? I've looked around and can't find the information.
Thanks,
RonC
-
It's fine as it stands...someone else may benefit from it.
-
Also things like if number=4 then value="four"...
I meant where you see
if number=1 then value="one"
if number=2 then value="two"
if number=3 then value="three"
(etc)
and some time later...
if value="one" then newnumber=1
if value="two" then newnumber=2
if value="three" then newnumber=3
(etc)
I have done this sort of thing myself.
-
Sorry people, I may have screwed up and my celebration was premature. I tried the coding you suggested and it didn't work. Here is the code for my bat file:
@echo off
cd\
cd\Users\RONALD\Desktop
if /i "%dayname%"=="Monday" copy current*.* f:\monday /y
if /i "%dayname%"=="Tuesday" copy current*.* f:\tuesday /y
if /i "%dayname%"=="Wednesday" copy current*.* f:\wednesday /y
if /i"%dayname%"=="Thursday" copy current*.* f:\thursday /y
if /i "%dayname% =="Friday" copy current*.* f:\friday /y
if /i "%dayname% =="saturday" copy current.* f:\saturday /y
When I run the bat file and then open the directory f:\saturday it is empty. Am I not properly identifying a variable? I can't figure it out.
Thanks,
RonC
-
Small S in Saturday ? ?
-
No, that didn't do it. The directory I'm copying too is small s (saturday) and I've also tried it with the /I but it still didn't work. The code below works fine:
cd\
cd\Users\RONALD\Desktop
copy current*.* f:\saturday /y
Thanks for the suggestion.
RonC
-
RonC, how does the batch script know what day it is? That is, how does the variable %dayname% get a value? I see nothing in your script that does that.
-
Gone quiet...
-
If anyone is interested, this will do it...
@echo off
echo wscript.echo weekdayname(weekday(now)) > "%temp%\wdn.vbs"
for /f "delims=" %%A in ('cscript //nologo "%temp%\wdn.vbs"') do set dayname=%%A
del "%temp%\wdn.vbs"
REM this line is optional:
echo Today is %dayname%
C:\Batch\Test>dayname.bat
Today is Saturday
-
The code below works fine:
cd\
cd\Users\RONALD\Desktop
copy current*.* f:\saturday /y
That's because it doesn't depend on the script knowing what day it is, which it will not be able to magically do.
-
What day is it ? ?.... :P
-
Thanks guys. I'll give it a go and let you know.
RonC
-
How did you think the script was going to know the day of the week? Curious to know.
-
Well guys, it worked for "Saturday". I'm going to set my alarm for 12:01am to verify that it works for "sunday".
;D
Thanks,
RonC
-
How did you think the script was going to know the day of the week? Curious to know.
-
Salmon Trout; the code you provided works perfectly. Thank you so much (and a big thank you to Patio and others for their comments also). Just so you know; I do volunteer work for the International Rescue Committee , a non-profit organization founded through the work of Albert Einstein, which responds to the world’s worst humanitarian crises and helps people whose lives and livelihoods are shattered by conflict and disaster to survive, recover, and gain control of their future. I'm retired and spend a lot of time writing small computer programs for them, primarily for administration support, using Microsoft Access. I use this and other forums for guidance and the availability and willingness of people like you all to provide that support has been a godsend.
Thanks again.
RonC
-
Thank You for volunteering...
-
It's good to know that there are still people like that.
-
Sorry people, I may have screwed up and my celebration was premature. I tried the coding you suggested and it didn't work. Here is the code for my bat file:
@echo off
cd\
cd\Users\RONALD\Desktop
if /i "%dayname%"=="Monday" copy current*.* f:\monday /y
if /i "%dayname%"=="Tuesday" copy current*.* f:\tuesday /y
if /i "%dayname%"=="Wednesday" copy current*.* f:\wednesday /y
if /i"%dayname%"=="Thursday" copy current*.* f:\thursday /y
if /i "%dayname% =="Friday" copy current*.* f:\friday /y
if /i "%dayname% =="saturday" copy current.* f:\saturday /y
When I run the bat file and then open the directory f:\saturday it is empty. Am I not properly identifying a variable? I can't figure it out.
Thanks,
RonC
This code should have given you an error. You are missing a double quote for the comparison on Friday and Saturday.
-
8)