[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 449

# 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!
0
jbla9028
• 5
• 5
• 2
4 Solutions

Commented:
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
0

Author Commented:
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.

0

Commented:
Hi, this sort of thing is easier in VBS for me.

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)

strSourceFile = "C:\resetstats.log"
strDestinationFile = Left(strSourceFile, Len(strSourceFile) - 4) & "_" & objNetwork.ComputerName & "_" & strDate & Right(strSourceFile, 4)

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

0

Author Commented:
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!
0

Commented:
Okay, here's the basic approach I would take in a BAT script.

@echo off

REM Define where the file comes from, and goes to
set BaseFile=resetstats.log
set DestDir=\\servername\sharename\destdir
set DestName=resetstats
set DestExt=log

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

REM Copy the file to the server adding the date/time stamp
copy "%BaseFile%" "%DestDir%\%DestName%_%Stamp%.%DestExt%"

~bp
0

Author Commented:
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
0

Commented:
Hmmm, don't see how that could be happening, the STAMP variable I created has the date and the time in it, at least it should, and does when I ran a test here.  Try he following version to see what it displays:

@echo off

REM Define where the file comes from, and goes to
set BaseFile=resetstats.log
set DestDir=\\servername\sharename\destdir
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
ECHO copy "%BaseFile%" "%DestDir%\%DestName%_%Stamp%.%DestExt%"
copy "%BaseFile%" "%DestDir%\%DestName%_%Stamp%.%DestExt%"
PAUSE

~bp
0

Author Commented:
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?
0

Commented:
Ah yes, I thought that through, and then forgot to add it to the file name, sorry about that.  Here are the changes for that:

ECHO copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"
copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"

~bp
0

Author Commented:
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%"
0

Commented:
Great, glad that helped, thanks for the feedback.

~bp
0

Commented:
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

0

## Featured Post

• 5
• 5
• 2
Tackle projects and never again get stuck behind a technical roadblock.