Sign in to follow this  
Followers 0
reload05

Need help renaming a directory in a particular way

6 posts in this topic

Ok, let me see if I can explain what I need my script to do. This should be easy for the experts, but I'm not sure how to go about doing this.

I need to rename a directory with the word "temp" in it to a constant name. For instance, I want to rename D:\temp to D:\backup. But the trick is, D:\temp could be anything, but it will have the word temp in it. For example, D:\temp could be D:\454545454temp5656565 and I need it to rename it to D:\backup. As long as that directory has the name "temp" somewhere in the name, I want it to rename it to D:\backup.

I tried using wildcards, but I guess that is just illegal. Here is what I had:

DirMove ( "D:\*temp*", "D:\backup",1 )

Any suggestions on how to go about doing something like this?

Share this post


Link to post
Share on other sites



I suggest looking in the help at the FileFindFirstFile function and StringInStr function


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

$search = FileFindFirstFile("D:\*temp*")  

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If $file = "temp" Then ContinueLoop
    If StringInStr($file, "temp") and FileGetAttrib("D:\" & $file) = "D" Then DirCopy("D:\" & $file, "D:\Backup", 1)
WEnd

FileClose($search)


We have enough youth. How about a fountain of SMART?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Are you sure that is the code sykes? I ran it, and it didn't work. :) Does the FileFindFirstFile command work with directories, or just files?

Edited by reload05

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I just wanted to thank both of you guys! I'm VERY new to coding and I'm really excited to say that it works! I had to change a few things, but it works PERFECTLY!

Here's the code I used:

; Shows the filenames of all files in the current directory, note that "." and ".." are returned.
$search = FileFindFirstFile("c:\*Temp*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If StringInStr($file, "Temp") and FileGetAttrib("C:\" & $file) = "D" Then DirMove ("C:\" & $file, "C:\backup", 1)

WEnd

; Close the search handle
FileClose($search)

I removed the line If $file = "temp" Then ContinueLoop and replaced DirCopy with DirMove and everything worked out perfect.

Thanks again guys for helping a newb!

Edited by reload05

Share this post


Link to post
Share on other sites

I just wanted to thank both of you guys! I'm VERY new to coding and I'm really excited to say that it works! I had to change a few things, but it works PERFECTLY!

Here's the code I used:

; Shows the filenames of all files in the current directory, note that "." and ".." are returned.
$search = FileFindFirstFile("c:\*Temp*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If StringInStr($file, "Temp") and FileGetAttrib("C:\" & $file) = "D" Then DirMove ("C:\" & $file, "C:\backup", 1)

WEnd

; Close the search handle
FileClose($search)

I removed  the line If $file = "temp" Then ContinueLoop and replaced DirCopy with DirMove and everything worked out perfect.

Thanks again guys for help a newb!

<{POST_SNAPBACK}>

I used the D:\ directory because thats what you had in your first post ... When testing it i was using C:\

The

If $file = "temp" Then ContinueLoop

Was there in case there was a directory on the drive simply called "Temp"

Without this code it will move the "temp" directory to "backup" and also any folder with "temp" in the name to "backup" as well.


We have enough youth. How about a fountain of SMART?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0