Solved

How to modify VB Script to check file existence?

Posted on 2011-03-18
6
477 Views
Last Modified: 2012-05-11
Hi,

can you help me to adjust my VB script? I need to search the script that exists in the %TEMP% file test.log, and if so, read the contents (this is always one digit), and added that the current file name in parentheses.
Finally, the script must delete the text file test.log.
In the event that no file test.log in the temp directory is not, the script work as now.

Thanks in advance for your great help.

TM
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strDesktop = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop"
strUserName = objShell.ExpandEnvironmentStrings("%USERNAME%")
strDestination = "\\S1\tests\2011 A\"
arrExtensions = Array(".pp", "doc", "xls")

If Right(strDesktop, 1) <> "\" Then strDesktop = strDesktop & "\"
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
For Each objFile In objFSO.GetFolder(strDesktop).Files
	boolCopy = False
	For Each strExt In arrExtensions
		If LCase(Left(Mid(objFile.Name, InStrRev(objFile.Name, ".")), Len(strExt))) = LCase(strExt) Then boolCopy = True
	Next
	If boolCopy = True Then
      strNewName = strUserName & "_" & objFile.Name
      If objFSO.FileExists(strDestination & strNewName) = True Then objFSO.DeleteFile strDestination & strNewName, True
      objFSO.CopyFile objFile.Path, strDesktop & strNewName, True
      objFSO.MoveFile objFile.Path, strDestination & strNewName
   End If
Next
MsgBox "Your files have been copied to " & strDestination

Open in new window

0
Comment
Question by:Thomas_Meyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35165622
Still not clear with the requirements,

What is location of temp file?
What do you mean by add that the current file name in parentheses?

It would be good if you can give an example
0
 

Author Comment

by:Thomas_Meyer
ID: 35165934
Okay, I'll try to explain it further:

In the System folder "%Temp%" I have a text file test.log.
At this moment, my script works by renaming an existing file on your desktop user name (system variable "%USERNAME%") and I need to make the newly renamed file added more digits of the test.log file (if file exists).
The result might look like this:
The script renames the file on your desktop: Thomas.Meyer(2). pptx
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35175075
Try the following...

regards
Prashanth
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strDesktop = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\test"
strUserName = objShell.ExpandEnvironmentStrings("%USERNAME%")
strtempfile=objShell.ExpandEnvironmentStrings("%temp%")&"\test.log"
strDestination = "\\S1\tests\2011 A\"
arrExtensions = Array(".pp", "doc", "xls")

tempnum=""
If objfso.FileExists(strtempfile) Then
    Set tempfile=objfso.OpenTextFile(strtempfile,1)
    tempnum=Trim(tempfile.ReadLine)
    tempfile.close
    strNewName = strUserName & "_" & Replace(objFile.Name,".","("&tempnum&").")
End If

If Right(strDesktop, 1) <> "\" Then strDesktop = strDesktop & "\"
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
For Each objFile In objFSO.GetFolder(strDesktop).Files
    boolCopy = False
    For Each strExt In arrExtensions
        If LCase(Left(Mid(objFile.Name, InStrRev(objFile.Name, ".")), Len(strExt))) = LCase(strExt) Then 
            boolCopy = True
        End If
    Next
    If boolCopy = True Then
        
        If Len(tempnum)>0 Then
            Set tempfile=objfso.OpenTextFile(strtempfile,1)
            tempnum=Trim(tempfile.ReadLine)
            strNewName = strUserName & "_" & Replace(objFile.Name,".","("&tempnum&").")
        Else
            strNewName = strUserName & "_" & objFile.Name
        End If
        If objFSO.FileExists(strDestination & strNewName) = True Then 
            objFSO.DeleteFile strDestination & strNewName, True
            objFSO.CopyFile objFile.Path, strDesktop & strNewName, True
            objFSO.MoveFile objFile.Path, strDestination & strNewName
        End If
    End If
Next
MsgBox "Your files have been copied to " & strDestination

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:Thomas_Meyer
ID: 35175260
Hi,
I wrote a script error in that line (see below) and error: Object required: 'objFile'

strNewName = strUserName & "_" & Replace(objFile.Name,".","("&TempNum&").")

Regards,
TM
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35175311
Delete line no 14 and try

regards
Prashanth
0
 

Author Closing Comment

by:Thomas_Meyer
ID: 35175380
It helped that it works as it should!
I give thanks and evaluation.
Regards
TM
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question