Solved

Need to create a script with [week day] variable

Posted on 2011-02-15
15
668 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:ISUY
Comment Utility
the result of running the command

  date/t


is:


16/02/2011

Thks
0
 

Author Comment

by:ISUY
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 125 total points
Comment Utility
==> 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
Comment Utility
'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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

11 Experts available now in Live!

Get 1:1 Help Now