Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 ms dos scrip - help needed

Author  Topic 

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 07:23:59
Greetings

I need help with writing a .bat file.

I need to copy all files in a folder to a nother folder on a nother server but exclude this month and the previous month. Then I need to delete the copied files.

Now the problem for me is how do I write this? I've never done ms dos scrips before. The problem for me is the datetime. The files are named like ex070910.log but they have a created date if one checks the properties. This batch file I will have executed probably once a month.

Help would be very much appreciated!

Kristen
Test

22859 Posts

Posted - 2007-10-16 : 07:43:33
So you will copy files ONLY if they are older than 1st day of LAST month?

I would have a batch file that ran once a month that moved the files to a subfolder:

REM Create folders (in case not already existing - ignore errors!)
MD Month1
MD Month2
MD Month3

REM Move files
MOVE Month2\*.* Month3
MOVE Month1\*.* Month2
MOVE *.* Month1

and then I would have a routine to move, and then delete, anything in Month3 folder to the other machine:

XCOPY Month3\*.* \\RemoteMachine\RemoteShare\RemotefolderFOR %%I IN (Month3\*.*) DO IF EXIST \\RemoteMachine\RemoteShare\Remotefolder\%%~nI%%~xI DEL %%I

NOTE: This will delete anything in "Month3" folder which exists on Remote. This is not quite what you asked for, but it may be more robust that a Copy/Delete in case the copy fails. However, if a file exists on the Target (possibly ReadOnly) then the Copy may fail but the Delete will succeed [because target names already exists]. There is also no test that the file copy was successful.

You may be able to use RoboCopy functionality to do a better job than this

Kristen
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 08:23:01
No, files older then previous month. Now it's 16th/10/2007, so if the script ran today I'd copy files that are made in august or futher back in time. The log files I'm copying to a nother servers are IIS logfiles and one will always be in use and changes on the active directory is not an option. How they get placed on the destination of the copy does not matter.

Really appreciate your help, again! :-)
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 08:52:23
To clearify what I need help with is how to type the copying exluding the files not to be copied and copy the files to the destiation that are to be copied and remove the copied files. So a copy *.* wont do :/
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 09:48:46
I solved my own problem I belive, not the safest script atm but will fix that up later. For anyone who is interrested, this is wat I have. (save the script in a .vbs file)

Copy_Files_Dates


Sub Copy_Files_Dates()

on error resume next 'så att alla filer kopieras även om det fanns någon dublett, den kommer dock alltid ligga kvar.
'set FSO = createObject("Scripting.FilesystemObject")
'set FromPath = String
'set ToPath = String
'set Fdate = Date
'set FileInFromFolder = object()
Dim FromPath
Dim ToPath
Dim Fdate
Dim FileInFromFolder

FromPath = "C:\testBatfile\"
ToPath = "C:\testBatfile2\"

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If

Set FSO = CreateObject("scripting.filesystemobject")

'If FSO.FolderExists(FromPath) = False Then
' MsgBox FromPath & " doesn't exist"
'Exit Sub
'End If

'If FSO.FolderExists(ToPath) = False Then
' MsgBox ToPath & " doesn't exist"
'Exit Sub
'End If

For Each FileInFromFolder In FSO.getfolder(FromPath).Files
Fdate = Int(FileInFromFolder.DateLastModified)
'Copy files 'from 1-Oct-2006 to 1-Nov-2006
If Fdate <= Int(Now()-61) Then
FileInFromFolder.Move ToPath
End If
Next' FileInFromFolder

'MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 09:50:00
Darn no edit button anywhere in sight. sigh

Note: lots of out commented code that I havent got working atm.
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-16 : 09:51:42
It's my topic so I can spam it :p

Removed outcomented code.


Copy_Files_Dates


Sub Copy_Files_Dates()

on error resume next 'så att alla filer kopieras även om det fanns någon dublett, den kommer dock alltid ligga kvar.

Dim FromPath
Dim ToPath
Dim Fdate
Dim FileInFromFolder

FromPath = "C:\testBatfile\"
ToPath = "C:\testBatfile2\"

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If

Set FSO = CreateObject("scripting.filesystemobject")

For Each FileInFromFolder In FSO.getfolder(FromPath).Files
Fdate = Int(FileInFromFolder.DateLastModified)
If Fdate <= Int(Now()-61) Then
FileInFromFolder.Move ToPath
End If
Next

End Sub
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-16 : 10:17:38
There should be an Edit button (for you, as the author). And a Delete ... if you want them!

For a programmed solution you have lots of options to manipulate dates ... harder from a BATCH file.

"IS logfiles and one will always be in use and changes on the active directory is not an option"

MOVE would work OK in that scenario. It would fail to move the in-use file, and that would get moved next time. All the other files would be moved OK.

But its 1) a bit cheap and cheerful! but also 2) unsophisticated, and as such may work more reliably than a programmed solution. We use "10 folders and move down one once a day" type round-robin solutions to keep the last 10 days log files etc. and they work reliably without needing tweaking - for daylight saving, or leap years, or the other things that can spoil your day!

Kristen
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 02:42:10
My true problem was however that my boss wanted a batchfile, I wanted to write a short c# program to do it, that would have been so small, beautifull and easy but nono...
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 02:46:11
And oh, a clearification to anyone who did not notice, these rows:
quote:

Fdate = Int(FileInFromFolder.DateLastModified)
If Fdate <= Int(Now()-61) Then


Check those out. Uggly but small and works, I lower today with 61 days which is aprox 2 month. That means that all files that has a datelastmodified that is not older then 61 days will not be moved.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 04:02:20
I'm sorta sided with your boss. The "2 months" thing is hard with a Batch file. But a C# program (or anything like that) needs management of source code, the ability of someone to be able to find that, fix it, and all that stuff. So "shell scripts" are a better choice for maintainability. Provided they can do the job ... but there again your boss may well be happy with "approximately 2 months" which is why we use Directory Renaming for that type of housekeeping.
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 04:25:50
In a way I agree too since one can easily without recompiling anything make a change and have it working directly. However we have just installed tfs and are moving more and more from svn to tfs so it's easy to keep track on everything and not have different versions a little here and there.

The 2 month thinig is easy to change and it's done only so a server with low diskspace wont be filled. There is no need for anthing precise here for now, thou I'd like to log things a bit better and make some nice little program to spot hack and login attemnts from sertain ip's but what we have now is ok. The server will probably be replaced but untill then this will do nicely for the problem that arose.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 05:17:22
Isn't "tfs" effectively SourceSafe? If so doesn't sound like much of an upgrade from SVN!!
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 06:25:28
svn is in short crap, and everyone has not been doing what they should. tfs seams to work a lot better. MS didnt even use svn themselves, and from that critisism ms made a bit more of an effort with tfs. Then tfs also has stuff that managers and peeps with low it knowledge finds usefull like progress information etc.
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 06:35:08
There are loads of new nice features in the tfs. I'm in no way an expert there yet thou so peeps are better off askign someone else or seaching the info themselves. However, one very practical thing is you often work on something and when the day is at an end your in the middle of something which you in svn would not want to commit so you only have it on your computre. You can in tfs upload this seperately then and your half finished code wont be compiled with the projects/solutions if your company for instance does nightly builds etc.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 07:06:16
"svn is in short crap"

Is SVN not Sub Version in this context then? 'Coz SubVersion is what we use and I think its the Dog's Danglies ...

Kristen
Go to Top of Page

IceDread
Yak Posting Veteran

66 Posts

Posted - 2007-10-17 : 07:11:01
Yeah it''s subversion I'm talking about, it is working for me as it should this far but there are several peeps that are having problems and ms didnt like it themselves even, but if you like and it works good then kewl. I do suggest you take a little look on tfs thou.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 07:17:29
OK, will do. Helpful to know because as of just now I had all MS-created version control in the poop-scoop bucket!

Two especial hates:

Check out and LOCK (preferred: check out without lock, check in to deal with collisions)

Repository can become corrupted.
Go to Top of Page
   

- Advertisement -