Running dir against multiple directories - windows

If I run, for example:
dir /s /b /o:gn "c:\Program Files\TrueCrypt" | findstr .sys
I comfortably get
c:\Program Files\TrueCrypt\truecrypt.sys
c:\Program Files\TrueCrypt\truecrypt-x64.sys
..in return. But if I add a folder that doesn't exist (I won't go into the reasons why this would be the case, but it will be), I just get an error:
dir /s /b /o:gn "c:\Program Files\TrueCrypt" "c:\non folder\non subfolder" | findstr .sys
The system cannot find the file specified.
Unlike in the UNIX world where I can use "find" and it returns files found in any directories without only barfing on the directories that don't exist.

You can use a loop and call dir for each of the folders:
for %F in ("c:\Program Files\TrueCrypt" "c:\non folder\non subfolder") do (
dir /s /b /o:gn "%F"
) | findstr .sys
However, depending on what you need exactly you can probably do better by ditching dir and findstr here and opt for a loop over the files. But that depends a bit on what your overall goal is.

Related

Windows cmd: piping the dir list output into find (or findstr) is not working

I need to do some very big Windows searches for some specific searchterms in th contents of all the files in a folder and all sub-folders. The GUI search facility is not finding all my tests, so I would like to try to use find via the cmd.
I can list all filenames in raw data format using:-
dir /S /B
I can successfully search for the searchterm in thecontents of all files in a single folder using :-
find "Searchterm" *.*
But there are thousands of recursive sub-folders, so when I pipe the output from the dir listing to the find (and exclude the filename parameter):
dir /S /B | find "Searchterm"
I am getting no results.
Furthermore, I have also successfully sent all the dir /B /S filenames to a text file:-
dir /S /B >> filenames.txt
and using type to pipe the contents of each file from the list to the find :-
type filenames.txt | find "Searchstring"
This does not work either. What am I missing? Microsoft's documentation suggests exactly the same format in https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/find as I am trying.
The solution to your question(s) should be clear by reading the output from %SystemRoot%\System32\findstr.exe /? ENTERed in a Command Prompt window.
I'd advise that you use the /L, literal, option for your initial code.
Direct results example:
%SystemRoot%\System32\findstr.exe /I /L /P /S "Searchstring" *
If you first send the filenames to a text file, e.g. Dir /B /S /A:-D 2>NUL 1>"filenames.txt", you could use the following idea:
%SystemRoot%\System32\findstr.exe /F:"filenames.txt" /I /L /P "Searchstring"
Just be aware, in this case, that unless you include a path outside of the target tree when initially creating filenames.txt, it will include itself in its own content. That means your FindStr command will also pick up any matches in that file too.

How to list all folders on specified drives with folder name first then gap then full path to folder?

dir i:\ j:\ k:\ /b /s /a:-D >c:\Users\Jason\Desktop\allFolderFilesOn_I_J_K.txt
This works great, but I am after a custom output, foldername full_path
Is this possible and if so how do I achieve this?
If I understand correctly, you don't want any files, but for every folder instead of C:\windows\system32 you want an output like system32 C:\Windows\
This can be done with the following command:
(for /d /r "D:\" %a in (*) do #echo %~nxa %~dpa)>c:\Users\Jason\Desktop\allFolderFilesOn_I_J_K.txt
where /d means "Directories only"
/r "Recursive" ("down the folder tree")
"D:\" the starting point
%~nxa the last element (the folder name)
%~dpa the drive/path to the folder
Note: this is command line syntax. For use in a batch file, replace each % with %%

CMD dir command

I'm using dir to retrive all directories from specified path but I'm getting directories paths from two locations.
dir "C:\ssis" /b /s /ad *.* | sort >c:\ssis\task4\content.txt
And result is
I want to get only marked (yellow) lines. Not directories from place where I open CMD
Thanks :)
PM
remove *.*:
dir "C:\ssis" /b /s /ad | sort >c:\ssis\task4\content.txt
Because you specify the directory where you want to list the files earlier "C:\ssis" so *.* will also list files in actual directory.

List files in subdirectories but don't list directories

When I do a dir /s /b command from command prompt I get the ususal files in the directory,subdirectories,and files in the subdirectories.For example:
C:\Files\more_files>dir /b /s
C:\Files\more_files\18.0.1025.39 Dev_chrome_installer.exe
C:\Files\more_files\7z920.exe
C:\Files\more_files\Firefox Setup 2.0.exe
C:\Files\more_files\ie6setup.exe
C:\Files\more_files\qbasic
C:\Files\more_files\sp6i386.exe
C:\Files\more_files\Thunderbird Setup 10.0.1.exe
C:\Files\more_files\qbasic\QBASIC.EXE
C:\Files\more_files\qbasic\QBASIC.HLP
C:\Files\more_files\qbasic\QBASIC.INI
C:\Files\more_files>
But I dont want subdirectories listed,but I want the files in the subdirectories listed.Basically, I want dir to not show subdirectories, but show the files in them.Here is an example with subdirectories removed:
C:\Files\more_files>dir /b /s
C:\Files\more_files\18.0.1025.39 Dev_chrome_installer.exe
C:\Files\more_files\7z920.exe
C:\Files\more_files\Firefox Setup 2.0.exe
C:\Files\more_files\ie6setup.exe
C:\Files\more_files\sp6i386.exe
C:\Files\more_files\Thunderbird Setup 10.0.1.exe
C:\Files\more_files\qbasic\QBASIC.EXE
C:\Files\more_files\qbasic\QBASIC.HLP
C:\Files\more_files\qbasic\QBASIC.INI
C:\Files\more_files>
I removed the qbasic subdirectory from the listing, but it still shows the files in the subdirectory(s).I want to use this for a cahce manifest for a webapp but with a different folder.Also, is there a way to list files in subdirectories but not the subdirectories themselves in Linux using ls?
Try:
dir /b /s /a-d
/a-d means "A" attribute, "-" no , "d" directories

While searching for .pst files FOR keeps looping when it reaches a hidden shortcut folder

The following command creates an infinite loop which is not what I want since I am iterating through files and it needs to end sometime...
Here is what I have:
cd C:\
FOR /R %i IN (*.pst) do #echo %i
See what happens is that when it reaches AppData and finds a .pst (in AppData\Local\Microsoft\Outlook) there is a shortcut folder inside AppData\Local called "Application Data" which loops back to AppData\Local but keeps adding it's name to the address like so:
%AppData%\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\%filename%.pst
What could I add to my code to keep it from looping or much better to completely ignore shortcuts so that the loop ends when it finds all the files that I need?
-----------Edit-------------
This seems to do something similar:
dir /s /b *.pst
You can filter out reparse points with DIR /A-L.
However, using DIR /A-L /S won't work also, because reparse point contents are not reparse points, so, try this:
Instead of FOR use:
FindFiles.bat *.pst c:\
Create a FindFiles.bat file with:
#ECHO OFF
:GetDirFiles %1=Wildcard %2=Path
FOR %%f IN ("%~f2\%~1") DO ECHO %%~ff
FOR /F "DELIMS=" %%d IN ('DIR /B /AD-L "%~f2"') DO CALL :GetDirFiles %1 "%~2\%%d"
This will recursivelly get all directories which are not reparse points and echo items matching pattern for each directory.
Ok, I recommend you use forfiles which should be on your computer if your using windows 7. Type forfiles /? for more info. Try this:
forfiles /p "C:\" /s /m "*.pst" /c "cmd /c (Echo #path)"
That should work perfectly. Im looking in ways of doing this with a for /r loop. It probably involves a dir check in a for /r /d. Tell me if this works fine for you.
Mona

Resources