Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to create a script with [week day] variable

Posted on 2011-02-15
15
Medium Priority
?
675 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 500 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 57

Assisted Solution

by:Bill Prew
Bill Prew earned 500 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 57

Accepted Solution

by:
Bill Prew earned 500 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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