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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename and move file based on user input

Status
Not open for further replies.

HEMICharger

IS-IT--Management
Jun 11, 2010
4
US
I'll first apologize for my lack of knowledge on this subject. I put together a script using examples I found else where, so I'm pretty sure this is not the most efficient way to do what I want.

My program (running on Win Server 2003) creates a video file with the same name every time. We will use "video.wmv" as the example. I need to allow the user to enter a new name for this file, and have the script move it to another location.

The script I have works by renaming and moving the file. But if the original file doesn't exist, it just bombs out with an error. How can I make it so if the original video file doesn't exist, it will end the script with a dialog box saying "file doesn't exist" instead of an error?

Here is what I have:
Code:
Do While X = 0
    strAnswer = InputBox _
        ("Please enter a name for your video file:","Create File")
    If strAnswer = "" Then
        Wscript.Echo "You must enter a file name."
    Else
        Wscript.Echo strAnswer
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.MoveFile "C:\video\video.wmv" , "C:\archive\" & strAnswer & ".wmv"
        Exit Do
	End If
Loop
 
If objFSO.FileExists("C:\video\video.wmv") Then
objFSO.MoveFile "C:\video\video.wmv" , "C:\archive\" & strAnswer & ".wmv"
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks very much for that info.

I have since modified the script to do one other thing, which is check to see if the filename the user wants to create already exists. But I'm not sure how to make it so that if it is a duplicate file name, it sends them back to the beginning input dialog box to try again.

Any ideas?

Here is my latest:
Code:
Do While X = 0
  strAnswer = InputBox _
    ("Please enter a name for your video file:","Create File")
  If strAnswer = "" Then
    Wscript.Echo "You must enter a file name."
  Else
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("C:\archive\" & strAnswer & ".wmv") Then
       Wscript.Echo "That file name already exisits"
    Else
      If objFSO.FileExists("C:\video\video.wmv") Then  
         objFSO.MoveFile "C:\video\video.wmv" , "C:\archive\" & strAnswer & ".wmv"
         Wscript.Echo "Your file has been Archived as " & strAnswer & ".wmv"
      Else
        Wscript.Echo "The original video file does not exist"
      End If
    End If

Exit Do        
  End If

Loop
 
Sorry, not sure what happened to that code

Code:
Do While X = 0
  strAnswer = InputBox _
    ("Please enter a name for your video file:","Create File")
  If strAnswer = "" Then
    Wscript.Echo "You must enter a file name."
  Else
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("C:\archive\" & strAnswer & ".wmv") Then
       Wscript.Echo "That file name already exisits"
    Else
      If objFSO.FileExists("C:\video\video.wmv") Then  
         objFSO.MoveFile "C:\video\video.wmv" , "C:\archive\" & strAnswer & ".wmv"
         Wscript.Echo "Your file has been Archived as " & strAnswer & ".wmv"
      Else
        Wscript.Echo "The original video file does not exist"
      End If
    End If

Exit Do        
  End If

Loop
 
Something like this ?
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists("C:\video\video.wmv") Then
  Wscript.Echo "The original video file does not exist"
Else
  Do
    strAnswer = InputBox _
                ("Please enter a name for your video file:", "Create File")
    If strAnswer = "" Then
      Wscript.Echo "You must enter a file name."
    ElseIf objFSO.FileExists("C:\archive\" & strAnswer & ".wmv") Then
      Wscript.Echo "That file name already exisits"
    Else
      objFSO.MoveFile "C:\video\video.wmv", "C:\archive\" & strAnswer & ".wmv"
      Wscript.Echo "Your file has been Archived as " & strAnswer & ".wmv"
      Exit Do
    End If
  Loop
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If I knew you, I'd buy you a beer.

Thanks, I appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top