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.
"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.
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.
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
If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.
Find a copy a file with File.Copy method
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)