Solved

Need to create a script with [week day] variable

Posted on 2011-02-15
15
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 55

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 55

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 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