Page 1 of 1 1
Topic Options
#198157 - 2010-03-24 09:37 AM Find and replace file based on date stamp?
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
I'm looking for some script which will do the following...

Check if screensaver.scr exists in a directory, check the date stamp. If it does not match the latest version, then overwrite it with the new version.

If the file doesn't exist at all, then copy the latest version to the directory..

Is there a function to check the date and timestamp on a file?

Cheers..

Top
#198158 - 2010-03-24 09:52 AM Re: Find and replace file based on date stamp? [Re: psykix]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4945
Loc: Leatherhead, Surrey, UK
 Originally Posted By: psykix
Is there a function to check the date and timestamp on a file?


Yup - look up GetFileTime() in the manual.

Here is a very simple way of doing what you want:
 Code:
$sSourceDir="\\server\share"
$sTargetDir="%WINDIR%"
$sFile="ScreenSaver.scr"

If GetFileTime($sSourceDir+"\"+$sFile) > GetFileTime($sTargetDir+"\"+$sFile)
	Copy $sSourceDir+"\"+$sFile $sTargetDir+"\"+$sFile
EndIf

Top
#198159 - 2010-03-24 09:56 AM Re: Find and replace file based on date stamp? [Re: Richard H.]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
Ah.. Have been doing some reading and I was gonna use the following..

 Code:
$result = CompareFileTimes ("\\fileserver1\ICT Software\Software\ScreenSaver\nwdass.scr", "C:\WINDOWS\System32\nwdass.scr")
	IF $Result=1 OR $Result = -3
		COPY "\\fileserver1\ICT Software\Software\Screensaver\nwdass.scr" "C:\WINDOWS\USER.INI" /h /s
	ENDIF


How can I test code without deploying it in my live script?


Edited by psykix (2010-03-24 09:58 AM)

Top
#198160 - 2010-03-24 09:59 AM Re: Find and replace file based on date stamp? [Re: psykix]
Mart Moderator Online   content
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4636
Loc: The Netherlands
You can test it by running it manually.
You need to use the correct path to kix32 and your script.

 Quote:

kix32 yourscript.kix
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198161 - 2010-03-24 10:10 AM Re: Find and replace file based on date stamp? [Re: Mart]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
I use the following code (dunno where the USER.INI came from above!!)

 Code:
$Result = CompareFileTimes ("\\fileserver1\ICT Software\Software\ScreenSaver\nwdass.scr", "C:\WINDOWS\System32\nwdass.scr")
	IF $Result = 1 OR $Result = -3
		COPY "\\fileserver1\ICT Software\Software\Screensaver\nwdass.scr" "C:\WINDOWS\System32\nwdass.scr" /h /s
	ENDIF


However, it is not overwriting the file - do I need a switch to overwrite it?

Not sure how to troubleshoot it, as the script operates silently...

Thanks for the help!

Top
#198162 - 2010-03-24 10:26 AM Re: Find and replace file based on date stamp? [Re: psykix]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
I changed to this :-

 Code:
$Result = CompareFileTimes ("\\fileserver1\ICT Software\Software\ScreenSaver\nwdass.scr", "C:\WINDOWS\System32\nwdass.scr")
	IF $Result = 1 OR $Result = -3
		DEL "C:\WINDOWS\System32\nwdass.scr"
		COPY "\\fileserver1\ICT Software\Software\Screensaver\nwdass.scr" "C:\WINDOWS\System32\nwdass.scr" /h /s
	ENDIF


It deletes the file, but doesn't copy the newer file... I'm wondering if it is a permissions issue?


Edited by psykix (2010-03-24 10:27 AM)

Top
#198163 - 2010-03-24 10:33 AM Re: Find and replace file based on date stamp? [Re: psykix]
Mart Moderator Online   content
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4636
Loc: The Netherlands
 Originally Posted By: psykix

...I'm wondering if it is a permissions issue?
...


Regular users cannot copy anything to the destination folder you are using.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198164 - 2010-03-24 10:42 AM Re: Find and replace file based on date stamp? [Re: Mart]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
 Originally Posted By: Mart
 Originally Posted By: psykix

...I'm wondering if it is a permissions issue?
...


Regular users cannot copy anything to the destination folder you are using.


Well I have local admin rights, and it's not working.. I have changed the code to the following so that they get the file from a local server :-

 Code:
$Result = CompareFileTimes (@LDRIVE+"\Screensaver\nwdass.scr", "C:\WINDOWS\System32\nwdass.scr")
	IF $Result = 1 OR $Result = -3
		COPY @LDRIVE+"\Screensaver\nwdass.scr" "C:\WINDOWS\System32\nwdass.scr" /h /s
	ENDIF


I basically want to change the screensaver on a regular basis, without having to change the GPO - hence using a script to just overwrite the screensaver file.

Any suggestions?

Top
#198165 - 2010-03-24 10:51 AM Re: Find and replace file based on date stamp? [Re: psykix]
Mart Moderator Online   content
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4636
Loc: The Netherlands
Maybe because it is a scr file (could be seen as some kind of script) it is blocked by your virus scanner.

Placing the code below directly after the copy command will provide you some extra info.

 Code:
?@error
?@serror
sleep 5
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198166 - 2010-03-24 11:03 AM Re: Find and replace file based on date stamp? [Re: Mart]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
Thanks,

I get :-

87
The parameter is incorrect.

Top
#198167 - 2010-03-24 11:23 AM Re: Find and replace file based on date stamp? [Re: psykix]
Mart Moderator Online   content
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4636
Loc: The Netherlands
Ok, I took a closer look at your code and two things come to mind.

1 You can drop the /s parameter. You are copying a file. The /s parameter is for copying folders including empty folders.

2 The @ldrive macro already has a trailing \ so you should remove the first \ after @ldrive.

Please give the code below a go and let us know the results.

 Code:
$Result = CompareFileTimes (@LDRIVE + " Screensaver\nwdass.scr", "C:\WINDOWS\System32\nwdass.scr")
If $Result = 1 Or $Result = -3
	Copy @LDRIVE + "Screensaver\nwdass.scr" "C:\WINDOWS\System32\nwdass.scr" /h
EndIf
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198168 - 2010-03-24 11:30 AM Re: Find and replace file based on date stamp? [Re: Mart]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
Thanks, that seems to work ok now - it was the extra trailing \

Cheers for that!

Top
#198170 - 2010-03-24 12:04 PM Re: Find and replace file based on date stamp? [Re: psykix]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
Hmm.. I have a problem as the script won't write that file into the C:\WINDOWS\System32 folder.

It works on my machine because I am an admin.

Is there any way to tell the script to use elevated privileges to copy the file?

Cheers..


Edit : from reading this forum it seems that if I run the script as a logon script it will run with elevated privileges.. can someone confirm this? (I have only tested it running locally so far)



Edited by psykix (2010-03-24 12:08 PM)

Top
#198171 - 2010-03-24 12:25 PM Re: Find and replace file based on date stamp? [Re: psykix]
psykix Offline
Fresh Scripter

Registered: 2008-07-09
Posts: 12
It's ok, have proved the above by using a test script on one user.

Works fine!

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 189 anonymous users online.
Newest Members
Steff, phanhuyen, Jeroen_Ruijter, Tienn, tmvngocdung42
17545 Registered Users

Generated in 0.082 seconds in which 0.03 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org