Windows Vista piping dir output into attrib command

0

I had a virus or something on my computer that set the attributes for all the folders in the root of my external drive to system and hidden, and created shortcuts to them. I am now trying to remove these attributes all at once with the following command, but it doesn't do anything:

dir /ash /b | attrib -h -s

According to my understanding of the documentation of these commands, this should work. Is there something wrong here?

Thanks

user195487

Posted 2013-02-04T19:11:46.780

Reputation:

Answers

1

Yes. The pipe | redirects program 1’s output to program 2’s input. However, your program 2 (attrib), does not read any input. It wasn’t written to do so. Instead, it expects file names in its command-line parameters.


There are some tools available in Unix-style systems to handle piping of arguments—though of little relevance here—they are:

  • xargs to handle such cases of converting text input into command-line arguments
  • find to handle this specific case of applying a command recursively
  • chmod command that has a “recursive mode” option

On Windows, without xargs, you will have to do something like:

for /f "tokens=*" %f in ('dir/b/ash') do @attrib -r -h -s "%~f"

Or maybe:

for /r . %f in (*) do @attrib -r -h -s "%~f"

user1686

Posted 2013-02-04T19:11:46.780

Reputation: 283 655

What a roundabout way of doing things! attrib supports recursion natively, see keltari's answer. – Karan – 2013-02-05T02:01:02.863

Thanks for the explanation as to why it doesn't work. I will try your suggestions. On the other hand, I could also just use my linux machine to do this, although I am not exactly sure how the windows attributes will translate into a linux environment, For instance, this particular thing is not a problem when I just connect the drive to my linux machine (because of the .filename used for hidden folders in unix) – None – 2013-02-05T05:39:55.533

1

Actually you can do it in a much easier fashion:

attrib e:\*.* -s -h /s

This will remove all the system and hidden attributes starting at the root of the E: drive and all its sub-directories. The /s tells attrib to process sub-directories.

Keltari

Posted 2013-02-04T19:11:46.780

Reputation: 57 019

Can also add /d if the directories have been affected as well. – Karan – 2013-02-05T02:01:48.243

I also considered this option, but only the directories were affected, and only in the root of the drive. I suppose it could not do any harm do do it this way, other than changing the attributes for files that needs to stay hidden and system. I could also use the attrib command on the folders individually, but of course that will take a long time. I was just frustrated that it didn't work although I figured it should. – None – 2013-02-05T05:35:45.043