Discussion Locked

Find a copy a file with File.Copy method

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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)

7 replies

by dklippert

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.

by pete.zerger

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

by c1980

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!

by pete.zerger

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

by pete.zerger

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

This discussion is closed to new replies.