General discussion

Locked

Find a copy a file with File.Copy method

By pete.zerger ·
I have a vbscript that I use to parse all log the files in a directory to find the log file created yesterday (using DateCreated property). The script currently zips the logs, but I want to change that.

Once the file has been found, I want to copy it locally to a different directory and rename it in the process. I know how to use File.Copy when I already know file name, but how to I tell File.Copy to copy the file when the I can't give it an explicit name??

(the log file has a different name every day)

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Find a copy a file with File.Copy method

by DKlippert In reply to Find a copy a file with F ...

Tried wildcards?

"COPY FILE creates a duplicate of the file whose name is specified in FileName1. You can use COPY FILE to copy any type of file. The file to be copied cannot be open. You must include the extensions for both the source file name FileName1 and destination file name FileName2.

FileName1 and FileName2 can contain wildcard characters such as * and ?. For example, to create backup copies of all program files with the extension .prg in the current directory, issue COPY FILE *.PRG TO *.BAK."

You can can concatenate the destination file with a variable and or use a for next loop to give multiple file unique names.

Collapse -

Find a copy a file with File.Copy method

by pete.zerger In reply to Find a copy a file with F ...

read the question closely. This is the File.Copy method in vbscript, not the DOS copy command.

Collapse -

Find a copy a file with File.Copy method

by C1980 In reply to Find a copy a file with F ...

I've do not use vbscript but maybe you can use this logic that I would use in VB6 for the same problem:

If I know the directory, I can use the Dir command to get the name of a file OR the names of the files in a specific directory.
If I call theDir command, passing to it the path with-out a file name, it will return the path and name of the first file in the directory, which then I can parse to get the file name. If I the call Dir again, it will return the NEXT file in that directory, and so on until I have looped through all of the files. I can use a place holder ( *.Doc ), in order to return files of a specific type. I can get the properties of each file with-in the loop to check, for instance, the date created. Based on this, I canget the file name, and then do the copy, for the last, file based on date,( or for all files if the criteria is for a specific date and not just the last created file), and or extension.

Hope this helps!

Collapse -

Find a copy a file with File.Copy method

by pete.zerger In reply to Find a copy a file with F ...

Thanks for your effort. The answer follows

For Each File in SubFiles

SourceFile = File.DateLastModified


'Now copy and rename that file

If DateDiff("d",SourceFile,Date())=2 Then

FileSystem.Copyfile File,"c:\temp\webstats.csv",True

End If
Next

Collapse -

Find a copy a file with File.Copy method

by 3wadman In reply to Find a copy a file with F ...

Better control than Copy!!
strFileName = "c:\InetPub\domain.com\logfile.txt"
<!-- #include virtual="ADOvbs.asp">
Set objFSO = Server.CreateObject
{"Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading,TriStateFalse)
DO WHILE objTextStream.AtEndOfStream = False
strResult = objTextStream.readline
strMessage = strMessage & strResult
Loop
S = cint(Second(Time))
Randomize
H = cint(Hour(Time))
M = cint(Minute(Time))
H = cstr(cint(rnd(H)*100))
M = cstr(cint(rnd(M)*100))
S = cstr(cint(rnd(S)*100))
strFile = H & M & S & ".txt" <<<random name
strFileName = Server.mappath("/LogFiles/" & strFile)
Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForWriting, True)
objTextStream.WriteLine strMessage
objTextStream.Close()
objFSO.DeleteFile(strFileName)<<< kills file

Collapse -

Find a copy a file with File.Copy method

by pete.zerger In reply to Find a copy a file with F ...

Thanks for your effort. The answer follows

For Each File in SubFiles

SourceFile = File.DateLastModified


'Now copy and rename that file

If DateDiff("d",SourceFile,Date())=2 Then

FileSystem.Copyfile File,"c:\temp\webstats.csv",True

End If
Next

Collapse -

Find a copy a file with File.Copy method

by pete.zerger In reply to Find a copy a file with F ...

This question was closed by the author

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums