Solved

Need to create a script with [week day] variable

Posted on 2011-02-15
15
672 Views
Last Modified: 2012-05-11
Hello, I need to create a script to copy files to a different path according to the day of the week.

for example, on monday, I need to copy c:\temp\1.txt to c:\backup\monday\
on tuesday  I need to copy c:\temp\1.txt to c:\backup\tuesday\

so, is there any system enviroment variable so I use something like %week_day%

Of corse I need to do this for a more complex script, the example is just to get a picture of the idea.

Thanks a lot
0
Comment
Question by:ISUY
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 34899937
It depends on the timezone setting of your localhost.  Run this command and post the result:

  date/t

In the U.S. (for example), the day-of-week can be determined with this macro:

  @echo  %date:~0,3%

0
 
LVL 13

Expert Comment

by:connectex
ID: 34899963
WeekdayName function in VBS will return the name for the current day of the week. Here's more information: http://msdn.microsoft.com/en-us/library/00201has(v=vs.85).aspx
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 34899971
For example, if the directory names were as follows:

   C:\backup\Sun
   C:\backup\Mon
   C:\backup\Tue
   C:\backup\Wed
   C:\backup\Thu
   C:\backup\Fri
   C:\backup\Sat

then the command (in the U.S.) would be simply:

   copy  "c:\temp\1.txt"  "c:\backup\%date:~0,3%"

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:IceCode
ID: 34899985
WScript.Echo Weekday(Now(),0)

1-Sunday
2-Monday
3-Tuesday
4-Wednesday
5-Thursday
6-Friday
7-Saturday
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 34900015
If you post the result of this date command, showing your local date format, I will derive the macro for you, which you can then plug into the command in my previous post.

  date/t

0
 
LVL 13

Expert Comment

by:connectex
ID: 34900077
To elaborate on my first post. Here's the code that will print the day of the week for the current system date.

iDay = WeekDay( Date )
WSCript.Echo WeekdayName( iDay )

It will return full day names (i.e. Monday, Tuesday, etc.). It should not have any regional issues.
0
 
LVL 14

Expert Comment

by:Psy053
ID: 34902001
The following is based on the example you provided. It will copy data from C:\Temp to C:\Backup\<weekday>  
(where <weekday> is whatever day it is>


Const OverWrite = True

strSource="C:\Temp\"
strDest="C:\Backup\" & WeekdayName(WeekDay(Now))

set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.CopyFolder strSource, strDest, OverWrite

Open in new window

0
 

Author Comment

by:ISUY
ID: 34910005
the result of running the command

  date/t


is:


16/02/2011

Thks
0
 

Author Comment

by:ISUY
ID: 34910150
I did try the example :
------------------------------------------------------------------
Const OverWrite = True

strSource="C:\Temp\"
strDest="C:\Backup\" & WeekdayName(WeekDay(Now))

set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.CopyFolder strSource, strDest, OverWrite
------------------------------------------------------------------


 but this will copy the contents of c:\temp\*.* to c:\backup\[week_day]\
how can I do to specify a File?, i did try the following:
----------------------------------------------------------------------------
Const OverWrite = True

strSource="C:\Temp\1.txt"
strDest="C:\Backup\" & WeekdayName(WeekDay(Now))

set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.CopyFile strSource, strDest, OverWrite
-------------------------------------------------------------------------------
but this will copy the file to c:\backup\[week_day]
I need to copy the file without renaming to c:\backup\[week_day]\1.txt

Thks again.

0
 
LVL 4

Expert Comment

by:IceCode
ID: 34910622
Const OverWrite = True

strSource="C:\Temp\1.txt"
strDest="C:\Backup\" & WeekdayName(WeekDay(Now)) & "\1.txt"

set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.CopyFile strSource, strDest, OverWrite
0
 
LVL 4

Assisted Solution

by:IceCode
IceCode earned 125 total points
ID: 34910639
Better yet...

sFilename = "1.txt"
Const OverWrite = True

strSource="C:\Temp\" & sFilename
strDest="C:\Backup\" & WeekdayName(WeekDay(Now)) & "\" & sFilename

set objFSO = CreateObject ("Scripting.FileSystemObject")
objFSO.CopyFile strSource, strDest, OverWrite


This way you can easily change the file name on line 1
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
ID: 34912801
Here's a BAT that should do the job:

@echo off

call :DayOfWeek
echo Today is a %DOW%
copy c:\temp\1.txt c:\backup\%DOW%\
exit /b

:DayOfWeek
  for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do set "%%a=%%A" & set "%%b=%%B" & set "%%c=%%C"
  set /a "yy=10000%yy% %%10000" & set /a "mm=100%mm% %% 100" & set /a "dd=100%dd% %% 100"
  set /A a = (14 - %mm%) / 12
  set /A y = %yy% - %a%
  set /A m = %mm% + 12 * %a% - 2
  set /A d = ((%dd% + %y% + (%y% / 4) - (%y% / 100) + (%y% / 400) + ((31 * %m% / 12))) %% 7) + 1
  for /F "tokens=%d% delims=," %%A in ("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday") do set DOW=%%A
  exit /b

Open in new window

~bp
0
 

Author Comment

by:ISUY
ID: 34919660
Ok both solutions works good, time to raise the bet:

is there a way to check the file was correctly copied?, I mean compare source and deststination, and show a message "file succefully copyed to xxx" or "file not copied, check for errors"

thanks a LOT!

0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 125 total points
ID: 34919982
==> Ok both solutions works good, time to raise the bet:

WHICH two solutions are you referring to?

Easy enough to check if the file is the same afterwards, try this:

fc c:\temp\1.txt c:\backup\%DOW%\1.txt | FIND "FC: no dif" > nul 
if ERRORLEVEL 1 goto :diff
echo Copy succesful
goto :rest
:diff
echo Copy unsuccesful
:rest

Open in new window

~bp
0
 
LVL 4

Assisted Solution

by:IceCode
IceCode earned 125 total points
ID: 34920592
'Define
	sFilename = "1.txt"
	sSource="C:\Temp\"
	sDest="C:\Backup\"
	sDestFolder = WeekdayName(WeekDay(Now)) & "\"

'Backup
	Set objFSO = CreateObject ("Scripting.FileSystemObject")
	If NOT objFSO.FolderExists(sDest) Then
		objFSO.CreateFolder(sDest)
	End If
	If Not objFSO.FolderExists(sDest & sDestFolder) Then
		objFSO.CreateFolder(sDest & sDestFolder)
	End If
	objFSO.CopyFile sSource & sFilename, sDest & sDestfolder & sFilename, TRUE 

'Compare
	Set oFile1 = objFSO.GetFile(sSource & sFilename)
	Set oFile2 = objFSO.GetFile(sDest & sDestfolder & sFilename)
	If oFile1.Size = oFile2.size Then
		WScript.echo "File " & sSource & sFilename & " copied successfully!"
	Else
		WScript.Echo "Files do not match!"
	End If
	WScript.Quit

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question