Link to home
Start Free TrialLog in
Avatar of jbla9028
jbla9028Flag for United States of America

asked on

script to move files

Can someone help provide a script to copy a file to a fileshare and rename a file to

filename_%Serverhostname%_date.log

This is a windows system.

Thanks!
Avatar of Bill Prew
Bill Prew

What file is being copied?

Is %sytemhostname% defined on the computer?

What format do you want the date in?

If you type the following at a command prompt what does it display, exactly?

ECHO %DATE%

~bp
Avatar of jbla9028

ASKER

the file name is called resetstats.log no preference on date format. if it could include a time in case I need to copy the file multiple times a day it would be good.

when I do the echo %date% the output is Tue 09/11/2012.

if I type hostname at the command prompt. the server name is displayed which is what I would like in the file name as I'll want to run this on multiple machines and copy it to the same directory.

Thanks for your help.
SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rob,
  Thank you this works perfectly. The last piece is in the script, where do I specify the UNC path to copy the file to? thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
billprew, this code works but could we add in the time as well? I just get the date. the file copied name was this

resetstats_CRN-JBLA_20120912.log
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
one last thing in that script that's missing in the file output is the server hostname. How can I get that in the filename?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! Here's the final script that worked with the edit provided! Saved the text as a .bat file and viola. Thanks everyone for their help.

@echo off

REM Define where the file comes from, and goes to
set BaseFile=C:\Program Files (x86)\Suite\resetstats.log
set DestDir=\\int\logs\
set DestName=resetstats
set DestExt=log

REM Get the current date/time in YYYYMMDD_hhmmss format
ECHO Date=[%DATE%]
ECHO Time=[%TIME%]
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
ECHO Stamp=[%STAMP%]

REM Copy the file to the server adding the date/time stamp and computer name
ECHO copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"
copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"
Great, glad that helped, thanks for the feedback.

~bp
Hi, sorry for not getting back to you ....I was asleep ;-)

Just for the sake of completeness, here's the VBS version of the same thing.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")

strDate = Year(Now) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2) & Right("0" & Second(Time), 2)

strSourceFile = "C:\Program Files (x86)\Suite\resetstats.log"
strDestinationFolder = "\\int\logs\"

If Right(strDestinationFolder, 1) <> "\" Then strDestinationFolder = strDestinationFolder & "\"
strFileName = Mid(strSourceFile, InStrRev(strSourceFile, "\") + 1)
strDestinationFile = strDestinationDir & Left(strFileName, Len(strFileName) - 4) & "_" & objNetwork.ComputerName & "_" & strDate & Right(strFileName, 4)

If objFSO.FileExists(strDestinationFile) = True Then objFSO.DeleteFile strDestinationFile
objFSO.MoveFile strSourceFile, strDestinationFile

Open in new window