Solved

Need to create a script with [week day] variable

Posted on 2011-02-15
15
669 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now