Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment filename if it already exists.

Status
Not open for further replies.
Apr 3, 2007
3
US
I'm trying to move all files not modified in the last 24 hours to a new folder but if it already exists I want to add an incremental number to the filename so that it copies.
Here is what I have so far.
Option Explicit

Const FOLDER = "\\Server\g$\Sharename"
Const BACKUP_FOLDER = "\\Server\d$\Sharename"
wscript.echo "Moving All Files Not Modified Last 24 Hours"
Dim objFSO, objFolder, objFile, filecount
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FOLDER)

On error resume next
filecount=0
For Each objFile In objFolder.Files
If objFile.DateLastModified < DateAdd("d", -1, Now) then
wscript.echo "Attempting to move file " & FOLDER & "\" & objFile.Name & " to " & BACKUP_FOLDER
objFile.Move(BACKUP_FOLDER & "\" & objFile.Name)
if Err > 0 then
wscript.echo "Error encountered moving file, The error is " & Err.Description
Err.clear
else
filecount = filecount + 1
wscript.echo objFile.name & " File Moved to backup folder"
End if
End If
Next
Any Ideas?
 
Your logic will result in duplicate copies of each file, so why don't you remove the checking entirely and simply copy all files, but make the destination location a folder named by date?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
True but for some reason the files are being re-created after I move them. I am going to talk with the Project Manager to find out why. Until then I was going to maybe rename the new files being created or possibly giving them a new extension name so that they would still copy.
 
I don't follow your thinking here. If you copy the files to a unique folder each time there is no need to rename the files or change extensions. You can have multiple versions of the same file in different directories.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I see what you are saying by creating a folder for each day but that was not the original idea going into the project. It was just to backup files and keep the drive clean. Thanks,
 
I did this with a batch file a LONG time ago. Here is the code I used to get a good date format that is easily editable:

Code:
for /F "tokens=1,2" %%d in ('date /T') do set day=%%d & set date=%%e
set yyyy=%DATE:~6,4%
set mm=%DATE:~3,2%
set dd=%DATE:~0,2%
set todaydate=%mm%-%dd%-%yyyy%
FOR /F "TOKENS=1" %%A IN ('TIME/T') DO SET SHORTTIME=%%A
Set SHORTTIME=%SHORTTIME::=.%
FOR /F "TOKENS=2" %%Q IN ('TIME/T') DO SET NOW=%TODAYDATE%.%SHORTTIME%%%Q
rename e:\source.log e:\source_%NOW%.log

You could easily an "if exists" in there and if there are multiple files in a dir, you could add a "for" loop to grab every file and check "if exists".
 
It was just to backup files and keep the drive clean.

How will appending a version number keep the drive clean? You will be creating a backup, but also you will be using more disk space due to multiple versions.

If the desire is to just copy new or changed files use RoboCopy.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top