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!

How make all subfolders in the current directory hidden via vbscript??

Status
Not open for further replies.

tektips12

Technical User
Nov 3, 2011
9
SY
How make all subfolders and files in the current directory hidden except two files "1.txt and 2.txt" via vbscript??
 
what's your programming knowledge (I see you joined today)?

[red]Traverse through a folders contents.[/red] [blue]Recursively for subfolders[/blue]. [green]For each file you encounter, see if the name equals "text1.txt" or "text2.txt".[/green] [purple]If not, use the files .Attributes property to set it to hidden.[/purple]

VBS Resource

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
thank u for your reply.

i am new in programing I like vbs

can u please help me writing the code correct

i want to put my vbs file in directory like c:\temp

when i run my vbs (all folders) in c:\temp (will be hidden) and (all files) except (two files)

i try this code but i think there are mistakes:

'DECLARE
SetFolder = "c:\temp"

Set fso = CreateObject("Scripting.FileSystemObject")
Set SearchFolder = fso.GetFolder(SetFolder)

'START

SEARCHFILES(SearchFolder)


'FUNCTIONS

Sub SEARCHFILES(SearchFolder)


For Each aItem In searchfolder.SubFolders
searchfolder.SubFolders.attributes = searchfolder.SubFolders.attributes AND 2
Next


end Sub
 
you're on the right track. searchfolder.SubFolders.attributes AND 2 only returns true or false on whether the folder is hidden. Instead, you need the ADD the hidden attribute. Also, the code you have only runs through folders in the searchfolder, not folders inside of folders. The searchfiles function needs to be run recursively to accomplish this. In addition, you'll ovbiously want to check the name of the file to see it equal 1.txt or 2.txt.

Here's is some code I put together that will accomplish what you want. It's color coded to highlight the logic mentioned previously

[red]1. Traverse through a folders contents.[/red]
[blue]2. Recursively for subfolders.[/blue]
[green]3. For each file you encounter, see if the name equals "text1.txt" or "text2.txt".[/green]
[purple]4. If not, use the files .Attributes property to set it to hidden.[/purple]

Code:
CONST HIDDEN = 2

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

function hideObjects(strDir)
	[red]set objFolder = objFSO.GetFolder(strDir)

	'run through the subfolders recursively and hide their contents
	[blue]for each objSubFolder in objFolder.SubFolders
		hideObjects objSubFolder.Path
	next
	[/blue]
	
	for each objFile in objFolder.Files
		[green]if ((objFile.Name <> "1.txt") AND (objFile.Name <> "2.txt")) then
		[purple]	'if the file is not already hidden, hide it.
			if NOT (objFile.Attributes AND HIDDEN) then
				objFile.Attributes = objFile.Attributes + HIDDEN
			end if[/purple]
		end if[/green]
	next[/red]
end function

hideObjects "c:\temp"

msgbox "done"

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Great code
but there are some problems:

1- the code work for files but not for folders

2- I want my vbs to work in the folder that contain vbs file,
if my vbs in d:\first so it work in it without writing the path in the code , it is working in the current path.

3- if there is file in my folder lets say "3.txt" and i do not want to change its "attribute hidden" how can do that, how can change all files . attributes except this file if all files become "not hidden" it stay without change , it stay hidden

Thanks again
 
1. True. To hide folders as well, simply both the "purple" code with the "blue" recursion. This way, the hidden attribute will be checked and set once all subfiles and subfolders have been addressed.

Code:
[blue]
    for each [b]objSubFolder[/b] in objFolder.SubFolders
        hideObjects objSubFolder.Path
        [purple]if NOT ([b]objFolder[/b].Attributes AND HIDDEN) then
            [b]objSubFolder[/b].Attributes = [b]objSubFolder[/b].Attributes + HIDDEN
        end if[/purple]
    next
[/blue]

2. you can get the path to where the vbs is located simple command "wscript.scriptfullname". then use the .GetParentFolderName method from the objFSO object.

Code:
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)
hideObject strPath

3. Create an array with files you want to exempt. Create a function that will run though this array to see if the current filename matches any values. Return true or false and apply the hidden attribute based on that.

Code:
arrExemptFiles = array("1.txt", "2.txt", "3.txt")

function isExemp(strFileName)
    isExempt = true
    for i = 0 to ubound(arrExemptFiles)
        strExeptFile = arrExemptFiles(i)
        if (strFileName = strExeptFile) then isExempt = false
    next
end function

then replace the "green" code with a call you the new function.

Code:
if (isExempt(objFile.Name) = false) then
   'set attribute
end if

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
there is another problem:
1- when i send new file or folders to my folder that contain vbs
and i run the vbs
the files and folders not hidden become hidden and
the files and folders hidden become not hidden

2- can u please help me collect codes in vbs file in correct way in tow parts:

part1: when i run vbs in "current directory"

a- all files and folders not hidden become hidden except "1.txt" stay (not hidden) always
b- and all files and folders hidden stay hidden without any change.

part2: when i run vbs in "current direcory"
a- all files and folder hidden become not hidden except(2.txt stay hidden always)

3- in past codes when i run vbs there is a pop up message tell me that there is a problem in hideobject , when i delete hideobject the codes work fine
so what cause this problem?? and what the purpose from using hideobject???

4- last qusetion:
can u please advise me how can i learn vbs in easy way and quick
from good sites or pdf books that give me examples with explanation
because i like to learn from examples

thanks
 
1- when i send new file or folders to my folder that contain vbs and i run the vbs the files and folders not hidden become hidden and the files and folders hidden become not hidden

Yes, I see that. Although I had tested the code, I didn't observe that initially the hidden attribute was being toggled. The reason why that happens is because the logic of the IF..THEN statment to determine if an object is hidden or not was wrong. It always returned true and thus would add 2 (the value of the hidden attribute) to the objects attributes, essentially toggling the hidden attribute.

I hope you have backups of these files from before you ran the script. The script likely messed up the file attributes.

2- can u please help me collect codes in vbs file in correct way in tow parts:

By this I assume you are asking to assemble the code in the correct order. The code I posted below satisfies or requirements. I STRONGLY recommend testing it on test file first :).

3- in past codes when i run vbs there is a pop up message tell me that there is a problem in hideobject , when i delete hideobject the codes work fine so what cause this problem?? and what the purpose from using hideobject???

Of course it runs fine. Functions and sub routines don't run unless they are instructed to do so (called). By deleting the call to hideObjects, the hideObjects function doesn't run. This is why the error message went away, the code that contains the error never ran.

4- last qusetion:can u please advise me how can i learn vbs in easy way and quick from good sites or pdf books that give me examples with explanation because i like to learn from examples

It's clear that you aren't too familiar with programming logic. I cannot suggest any books from which to learn VBS - everyone learns differently. However, I can recommend that you start at your local book store. They likely have a section about computer programming. Look through the books to find one that meets your learning style and experience level.

As for websites? It's as simple as googl'ing "Learn VBScript". Again, look through the links to find one that meets your style and experience.

I will, however, recommend a link that I frequently visit. I'ts that same link I posted in my first response to you.

The code. Notice how the code is "sectioned" off. I first define the variable I want to use. Then the functions. and finally, the main block of code. If the main section were empty, the script would do nothing. It's not, however. The script will therefore do three things: determine the path where the VBS file is located, hide the objects in that path, and say "done"

Code:
'*********************************************************************
' VARIABLE DEFINITION
'*********************************************************************

CONST HIDDEN = 2

arrExemptFiles = array("log.txt", "convertToBF.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'*********************************************************************
' FUNCTIONS / SUBS
'*********************************************************************

'Recursively traverse and hide a directory's content 
sub hideObjects(strDir)
	set objFolder = objFSO.GetFolder(strDir)
	for each objSubFolder in objFolder.SubFolders
		hideObjects objSubFolder.Path
		setAttribute objSubFolder, HIDDEN
	next
	
	for each objFile in objFolder.Files
		if (isExempt(objFile.Name) = false) then
			setAttribute objFile, HIDDEN
		end if
	next
end sub

'Check to see if a filename is exempt
function isExempt(strFileName)
	isExempt = false
	for i = 0 to ubound(arrExemptFiles)
		strExeptFile = arrExemptFiles(i)
		if (strFileName = strExeptFile) then isExempt = true
	next
end function

'Turn on the hidden attribute for a FSO object
sub setAttribute(objItem, intAttribute)
	msgbox objItem.Name & vbNewLine & objItem.Attributes & vbNewLine & (objItem.Attributes AND intAttribute)
	if ((objItem.Attributes AND intAttribute) <> intAttribute) then
		 objItem.Attributes = objItem.Attributes + intAttribute
	end if
end sub

'*********************************************************************
' MAIN
'*********************************************************************

'1. Get VBS File path
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

'2. Hide the objects in strPath
hideObjects strPath

'3. say "done"
msgbox "done"

-Geates


"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
you are genius programmer
Thanks alot
you helped me very much

i tested the vbs

1-it is work fine but there are some messages appear after run vbs and then the files and folders become hidden
can i make them hidden without appear these messages.

2-I need another vbs that return all these files and folders to not hidden
without any message or msgbox and except "1.txt" (stay hidden anyway)

Iam sorry if i make you tired
thanks again
 
1 - Take the "msgbox" out. They are from testing and I forgot to take it out.

2 - Hidding and unhidding an object is as simple as adding or substracting the bit. Modify the setAttributes() function to accept an "on" or an "off" argument. Look at the FSO attributtes method on It might give some context to what I am talking about.

Code:
'Turn on the hidden attribute for a FSO object
sub setAttribute(objItem, intAttribute, [red]boolOn[/red])
   [red]if (boolOn = true) then[/red]
      if ((objItem.Attributes AND intAttribute) <> intAttribute) then
         objItem.Attributes = objItem.Attributes + intAttribute
      end if
   [red]else
      if ((objItem.Attributes AND intAttribute) = intAttribute) then
         objItem.Attributes = objItem.Attributes - intAttribute
      end if
   end if[/red]
end sub

Not you can pass a true or false to turn the bit on or off.

Code:
'hide objFile
setAttribute(objFile, HIDDEN, true)

'unhide objFile
setAttribute(objFile, HIDDEN, false)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi Geates,

i do not know why it is not working
and i have some problems in collect them in one vbs in correct way

I told u about unhidden because i want my vbs in 4 parts and two timers
*********************************************************************
part1: if exist hello.txt goto 1 else goto 2 (how do that in vbs??)

part2: hidden the new files and folders
with stay hidden old files and folders in their hidden attributes
(except 1.txt stay unhidden any way) ' very very important

timer for 10 seconds i think we need use Wscript.sleep 10000

part3: after timer 10 second
the vbs will make all files and folders unhidden
(except 2.txt stay hidden any way) ' very very important

timer for 2 minutes i think by using Wscript.sleep 120000
in case i forgot to hidden files and folders so it will be hidden auto.

part4:after timer 2 minutes
all files and folders will be hidden
(except 1.txt stay unhidden anyway) ' very very important
************************************************************************
i try to collect all codes in one vbs but there are some messages tell there are wrongs
please help me to build this 4 parts in one vbs file in (correct way) assemble the code in the correct order

thanks Geates .
 
I'm not following the troubles you ar ehaving. Can you post your code?

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
the code like that:

'*********************************************************************
' VARIABLE DEFINITION
'*********************************************************************

CONST HIDDEN = 2

arrExemptFiles = array("log.txt", "convertToBF.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'*********************************************************************
' FUNCTIONS / SUBS
'*********************************************************************

'Recursively traverse and hide a directory's content
sub hideObjects(strDir)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
hideObjects objSubFolder.Path
setAttribute objSubFolder, HIDDEN
next

for each objFile in objFolder.Files
if (isExempt(objFile.Name) = false) then
setAttribute objFile, HIDDEN
end if
next
end sub

'Check to see if a filename is exempt
function isExempt(strFileName)
isExempt = false
for i = 0 to ubound(arrExemptFiles)
strExeptFile = arrExemptFiles(i)
if (strFileName = strExeptFile) then isExempt = true
next
end function

'Turn on the hidden attribute for a FSO object
sub setAttribute(objItem, intAttribute)

if ((objItem.Attributes AND intAttribute) <> intAttribute) then
objItem.Attributes = objItem.Attributes + intAttribute
end if
end sub

'*********************************************************************
' MAIN
'*********************************************************************

'1. Get VBS File path
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

'2. Hide the objects in strPath
hideObjects strPath

'3. say "done"



Wscript.sleep 10000


'*********************************************************************
' VARIABLE DEFINITION
'*********************************************************************

CONST HIDDEN = 2

arrExemptFiles = array("log.txt", "convertToBF.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'*********************************************************************
' FUNCTIONS / SUBS
'*********************************************************************

'Recursively traverse and hide a directory's content
sub hideObjects(strDir)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
hideObjects objSubFolder.Path
setAttribute objSubFolder, HIDDEN
next

for each objFile in objFolder.Files
if (isExempt(objFile.Name) = false) then
setAttribute objFile, HIDDEN
end if
next
end sub

'Check to see if a filename is exempt
function isExempt(strFileName)
isExempt = false
for i = 0 to ubound(arrExemptFiles)
strExeptFile = arrExemptFiles(i)
if (strFileName = strExeptFile) then isExempt = true
next
end function

'Turn on the hidden attribute for a FSO object
sub setAttribute(objItem, intAttribute)

if ((objItem.Attributes AND intAttribute) + intAttribute) then
objItem.Attributes = objItem.Attributes - intAttribute
end if
end sub

'*********************************************************************
' MAIN
'*********************************************************************

'1. Get VBS File path
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

'2. Hide the objects in strPath
hideObjects strPath

'3. say "done"



Wscript.sleep 120000


'*********************************************************************
' VARIABLE DEFINITION
'*********************************************************************

CONST HIDDEN = 2

arrExemptFiles = array("log.txt", "convertToBF.txt")

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'*********************************************************************
' FUNCTIONS / SUBS
'*********************************************************************

'Recursively traverse and hide a directory's content
sub hideObjects(strDir)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
hideObjects objSubFolder.Path
setAttribute objSubFolder, HIDDEN
next

for each objFile in objFolder.Files
if (isExempt(objFile.Name) = false) then
setAttribute objFile, HIDDEN
end if
next
end sub

'Check to see if a filename is exempt
function isExempt(strFileName)
isExempt = false
for i = 0 to ubound(arrExemptFiles)
strExeptFile = arrExemptFiles(i)
if (strFileName = strExeptFile) then isExempt = true
next
end function

'Turn on the hidden attribute for a FSO object
sub setAttribute(objItem, intAttribute)

if ((objItem.Attributes AND intAttribute) <> intAttribute) then
objItem.Attributes = objItem.Attributes + intAttribute
end if
end sub

'*********************************************************************
' MAIN
'*********************************************************************

'1. Get VBS File path
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

'2. Hide the objects in strPath
hideObjects strPath

'3. say "done"
msgbox "done"



i want vbs to do part1 then sleep 10s then part2 then sleep 2m then part3

part1:to hidden any new files or folders
part2:unhidden all files and folders so i can explorer them
part3:rehidden all files and folders again


if i want to put if statement in the first of code how do that
like if exist hi.txt goto 1 else goto 2
how make goto in vbscript???

thanks in advance.
 
i do not know why it is not working

uh...Not meaning to be rude, but what is wrong is day and night. The reason we write subs and functions is that we only have to write the code once. Copying them multiple times does not help in the slightest. Instead, causes confusion and inefficiency.

I asked you early on what is your programming knowledge. It's clear it is zero. Your question is lightyears ahead of your experience. I would concentrate on programming "Hello World" before attempting something like this.

I really do not feel comfortable giving you the complete code. It is clear you have no idea what it does. That being said, I've given you ALL the pieces. You'll need to assemble the puzzle. Sorry.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi Geates,

I do not meaning to be rude at all that is why i said "sorry if i make you tired" but i want to learn from examples first
and i need this script very important for personal using

you helped me very much and i appreciate what u do

but the last step "assemble the code" i have some problems with that
if u help me in that i will be grateful
and if u do not feel comfortable giving me the complete code i respect that and thanks u for all u doing.

thanks Geates.
 
It's not a matter of being tired. If you look at other posts in which I have participated, they offen have double digit responses (I tend to make mistakes from which I learn). I don't mind helping you learn but this script is not one to begin with.

Again, the purpose of this script is leaps and bounds beyond novice. You said it yourself "i want to learn from examples first". This script is by no means an example from which a novice in programming logic should learn.

[!]There are several websites out there that provide tutorials starting from the basics.[/!]

This is how the code would be assembled in the MAIN section. Modify the hideObjects() function to accept a [red]new argument[/red]. Make sure to pass this new argument along to the new setAttribute function I posted before.

Code:
strPath = objFSO.GetParentFolderName(wscript.scriptfullname)

hideObjects strPath[red], true[/red]
wscript.sleep 10000 '10 seconds

hideObjects strPath[red], false[/red]
wscript.sleep 120000 '120 seconds

hideObjects strPath[red], true[/red]

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank u Geates.

the script working excellent :)

thanks for helping and for your advices.
i will learn from basics, i do not know how much time i need to be good in vbs.

I wonder how can communicate with u in future if u do not mind.

sorry if I make u feel boring from my questions :)

Thanks again
nice to meet u Geats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top