Search drive for a given folder. If found Rename.

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
kocchB
Posts: 9
Last visit: Mon Nov 05, 2012 3:18 pm

Search drive for a given folder. If found Rename.

Post by kocchB »

Hello,I need help to write a script that will perform the following task:1/ Search a drive for a given sub folder named "Mydocs"2/ If the sub folder is found rename it "Mydocsold"3/ Document the execusion of the script on a log file that record starting date and time of the script. When the script found the sub folder to search document it in the log the path of the sub folder. Document the new name with its path. End date and time of the script.Here is the folder structure of the drive as an example: the drive letter is Zuser1 Data Mydocs Music Docsuser2 Jeux Docsuser3user4user5user6When I run the script that I will published below it list the folders asz:user1z:user1Dataz:user1DataMydocs (I want the script to find the subfolder mydocs)z:user1Musicz:user1Docs...............Here is the script: Set FSO = CreateObject("Scripting.FileSystemObject")ShowSubfolders FSO.GetFolder("Z:")Sub ShowSubFolders(Folder) For Each Subfolder in Folder.SubFolders Wscript.Echo Subfolder.Path ShowSubFolders Subfolder NextEnd SubI need help with the rest.
User avatar
kocchB
Posts: 9
Last visit: Mon Nov 05, 2012 3:18 pm

Search drive for a given folder. If found Rename.

Post by kocchB »

[quote] What is wrong with just asking for it by name?
 
ShowSubfolders FSO.GetFolder("z:user1DataMydocs")


Each folder on the root of the z drive represent the users personal folders. There are more than 300 of them.
I need to search each of them. If I find a folder that has in his path "DataMydocs[/COLOR" rename it "DataMydocsold"

With use "instr" function of VBS as you suggested see script below. I can list all the folders that have in their path "DataMydocs"

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubFolders FSO.GetFolder("z:")

Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
'Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
If InStr( Subfolder.Path,"DataMyDocs") Then
WScript.Echo Subfolder.Path
End If
Next
End Sub

The script echo as example:
z:user1DataMydocs
z:user15DataMydocsExcel...
z:User214DataMydocs
............

How do I tell the script to rename them to:
z:user1DataMydocsold
z:user15DataMydocsoldExcel...
z:User214DataMydocsold
User avatar
abqbill
Posts: 138
Last visit: Mon Sep 28, 2020 1:20 pm

Search drive for a given folder. If found Rename.

Post by abqbill »

Hi falle, if I understand your needs, here is a JScript script that should work:

Code: Select all

var fso = new ActiveXObject("Scripting.FileSystemObject");

function process(folder) {
  if (folder.Path.search(/datamydocs$/i) != -1) {
    WScript.Echo(folder.Path + " -> mydocsold");
    //folder.Name = "mydocsold";  //uncomment this line after testing
  }

  var folders = new Enumerator(folder.SubFolders);
  for (; ! folders.atEnd(); folders.moveNext())
    process(folders.item());
}

var folder = fso.GetFolder("z:");

process(folder);
HTH,

Bill
User avatar
kocchB
Posts: 9
Last visit: Mon Nov 05, 2012 3:18 pm

Search drive for a given folder. If found Rename.

Post by kocchB »

Hello AbqBill,

I do not know JScript. But When I copied and pasted your code to primalscript , it all came in one line.

I was able to get the script working:
----------------------------------------------------------------------------------------------
Set FSO = CreateObject("Scripting.FileSystemObject")ShowSubfolders FSO.GetFolder("Z:")Sub ShowSubFolders(Folder) For Each Subfolder in Folder.SubFolders
If InStr (Subfolder, "DataMydocsold") Then
Wscript.Echo "" & subfolder.Path,"exist!"
Wscript.Echo "========================"
Exit For
ElseIf InStr (Subfolder,"DataMydocs") Then
strFolderPath = subfolder
strFolderNewPath = Replace(strFolderPath, "DataMydocs", "DataMydocsold")
Set objFSO = createObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("" & strFolderPath)
objFolder.Move (strFolderNewPath)
Exit For
Else
ShowSubFolders Subfolder
' Do Nothing
End If NextEnd Sub----------------------------------------------------------------------------------------------

Any suggestions on how to write it better ?
User avatar
abqbill
Posts: 138
Last visit: Mon Sep 28, 2020 1:20 pm

Search drive for a given folder. If found Rename.

Post by abqbill »

Hi falle,

I don't know why the code would paste as all one line in PrimalScript--it didn't do that in notepad when I tried it.

Fortunately, you don't have to know JScript to use it. As posted, it will not rename any folders -- it will only output the directories it will rename. As it uses the WScript.Echo method, you should run it using cscript; e.g.

Code: Select all

cscript renameDirs.js
When you are confident it will rename the correct directories, uncomment the following line of code:

Code: Select all

//folder.Name = "mydocsold"; //uncomment this line after testing
Simply remove the two forward slashes from the front of the line to uncomment it.

HTH,
Bill
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked