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

very confusing file rename issue: trick of the day

Status
Not open for further replies.

chouck

MIS
Jan 30, 2005
32
US
I have a file in my home direcoty on the network in my z:\. it is located 2 subdirectories down z:\somedir\somsubdir\someuser.id (it is a notes id file and is based on the network username). I have to rename it specifically to user.id

when i run this script, i want the script to look for a file name based on "%username% & .id" and then rename it to user.id.

Here is the example of the code:

set wshshell = createobject("wscript.shell")

strcomputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")

username = wshshell.expandenvironmentstrings("%USERNAME%")
useridfile = (username & ".id")

wscript.echo username
wscript.echo useridfile

set notefileren = objwmiservice.execquery _
("Select * from CIM_DataFile where Name = " _
& "'z:\\somedir\\somesubdir\\useridfile'")

For Each fileobj in notefileren
fileobj.Rename("j:\Notes\Data\user.id")
next

Any suggestions would be greatly appreciated
 
Are "somedir" and "somesubdir" variable or do they always have the same name?

The reason I ask is that since there can't be two files in the same dir with the same name it seems like you could go right to the file without the SELECT statement and the For Each loop.

Code:
Dim oFSO 
Set oFSO = CreateObject("Scripting.FileSystemObject")
    
If oFSO.FileExists("Z:\somedir\somesubdir\" & useridfile) Then
  oFSO.MoveFile "Z:\somedir\somesubdir\" & useridfile, "j:\Notes\Data\user.id"
End If

Set oFSO = Nothing
 
z:\somedir\somesubdir\%username%.id is actually the path on the users home drive that is constant. basically i am looking at wanting to find the file %username%.id and rename it specifically to user.id, after that it will be copied to a different location and then deleted.

Thanks, so far, i think it might have given me a clue. Anything else would be awesome too
 
Moving it with a new name is the same as renaming, copying and deleting...

 
Your logic is flawless :) Quick question, i can just slap that "if" you suggested into a larger an on going "if. i'm still pretty new at this ?

ie:

if qualifier then
command
command
command

if file exist then
command
end if

command
command

end if

 
Certainly!

The only thing to remember is that the "nested" IF will always end first.

I will try to draw a picture:
Code:
If (X = TRUE) Then
    X Command
    X Command
    X IF (Y = True) Then
          X & Y Command
          X & Y Command
          X & Y etc...
    X Else
          X & Not Y Command  
          X & Not Y Command  
          X & Not Y etc...
    X End If
Else
    Not X Command
    Not X Command
    Not X etc...
End If

Well that might be more confusing that I'd hoped.

Basically what I'm trying to say is that, yes, you can put one IF inside another but you must always remember that an End If will ALWAYS apply only to the most recent IF and the same rule applies for an Else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top