Search drive for a given folder. If found Rename.
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.
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.
Search drive for a given folder. If found Rename.
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.
Search drive for a given folder. If found Rename.
[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
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
Search drive for a given folder. If found Rename.
Hi falle, if I understand your needs, here is a JScript script that should work:
HTH,
Bill
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);
Bill
Search drive for a given folder. If found Rename.
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 ?
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 ?
Search drive for a given folder. If found Rename.
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.
When you are confident it will rename the correct directories, uncomment the following line of code:
Simply remove the two forward slashes from the front of the line to uncomment it.
HTH,
Bill
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
Code: Select all
//folder.Name = "mydocsold"; //uncomment this line after testing
HTH,
Bill