Search and sort directory and subdirectory using Batch Script - search

I have been trying to do a search of the user directory and subdirectories in a primary school for all applications, music, videos and archived files to check what the kids are hiding from me. I have been using a basic dir script similar to this:
dir C:\Sample\*.exe, C:\Sample\*.rar, C:\Sample\*.mp3 /s /b /OE > D:\Result.txt
This provides me with an output of ALL files of those types sorted by user name.
What I want to know is, is there an easy way to either change the search to sort by type or to use the sort command afterwards to sort by the last character/s of the file.
Sample of output:
U:\adwan4\My Documents\My Videos\PhotoStory1.wmv
U:\ageeg2\My Documents\install_flashplayer11x32ax_gtbp_chra_aih.exe
U:\amcka55\My Documents\My Music\Angus.wmv
I have come up with something that appears to do the trick using Find along the lines of:
Find ".exe" D:\Result.txt >>D:\Sorted.txt
This appears to work fine for the avi, exe, etc. However I would be very happy for any suggestions.

Here you go
for %%x in (exe,mp3) do (
for /r C:\Dir %%a in (*.%%x) do echo %%a >>D:\Results.txt
)

For anyone interested, my initial solution (which took a lot more typing because it has no loops) looks something like this:
ECHO Off
dir U:\*.exe, U:\*.rar, U:\*.mp3 /s /b > D:\Result.txt
Echo Applications > D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".exe" D:\Result.txt >>D:\Sorted.txt
Echo Music >> D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".mp3" D:\Result.txt >>D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".wma" D:\Result.txt >>D:\Sorted.txt
etc
A lot more work to setup and is a 2 step process. The solution doesn't look as elegant as either option presented to me either. Thanks for your help.

The DIR /O sort option works fine when run without the /S option. But when you use the /S option it sorts the results within each directory individually, whereas you want to sort the entire result set.
Bali C's edited answer solves the problem by performing multiple searches individually, one per file extension. He chose to use the FOR command to iterate the files and ECHO the results. I believe it is simpler just to use multiple DIR commands.
>d:result.txt (
dir /s /b C:\Sample\*.exe
dir /s /b C:\Sample\*.rar
dir /s /b C:\Sample\*.mp3
)
or, perhaps a bit less typing
>d:result.txt (
for %%X in (exe rar mp3) do dir /s /b C:\Sample\*.%%X
)

Related

How to loop through a directory and skip folders containing a specific string?

I am currently scanning an entire directory, copying an exe over and running it in each folder. This exe converts certain log files within each folder. However, half the directories don't contain any log files and it seems like a waste to move into each of those folders and run an exe that does nothing. An example of some folder names:
0075D-S10
0075D-S10-EVT
0132C-S10
0132C-S10-EVT
and so on...
I basically only need to copy the executable into the folders that don't contain EVT in the name. These just contain evtx files that aren't important to me at the moment.
Here is a snippet of my batch file and what it is doing:
set back=%cd%
set current_dir=%cd%
setlocal EnableDelayedExpansion
for /d %%i in ("%current_dir%\*") do (
copy LogCsv.exe "%%i" >NULL
cd "%%i"
LogCsv.exe
)
cd %back%
Really my first time just using Batch files and I've been exploring answers on here, but couldn't find a solution to this particular problem. How can I loop this directory, excluding any folders containing the "EVT" string in their name?
Thanks for taking time to assist!
Well, you can use findstr /v to exclude certain values from search results.
#echo off
set back=%cd%
set current_dir=%cd%
for /f %%i in ('dir /b /ad %current_dir%\* ^| findstr /v EVT') do (
copy LogCsv.exe "%%i" >NUL
cd "%%i"
LogCsv.exe
cd %back%
)
Also note, you need to pipe to nul not null

Search for a file and open the path in CMD

I would like to achieve the following in CMD:
Will search for a specific filename - This I know how to do, with dir /s filename
Once found it will bring me to that path.
Example: I am now on C:\, if that file was found in C:\test then it will open C:\test in command prompt.
Alternatively, I would like just to copy the found file to a path I will specify. I just don't know how to do it, since I don't know the path where the file will be stored (it's different every time).
Thanks in advance!
I'm a liitle confused with your question but I will try to walk you through a batch code I wrote to help you with this.
dir /b /s "test.txt" > "%userprofile%/Desktop/result.txt"
::find the file path to the file you want to find and insert it in a .txt file
::you made called result.txt (also %userprofile% is a variable that brings you to
::your user directory ex C:/users/admin)
for /F "tokens=*" %%A in (%userprofile%/desktop/result.txt) do (
set var1=%%A
)
::set the variable var1 equal to the first (well... only) line in the file.
::I could explain what that for loop means in detail but just remember that %%A
::is a variable set from what was found when looping through the result.txt.
xcopy /s "%var1%" "C:/wherever/you/want/it/to/go"
did this help??

Search specified directory and output location of item(s) if found

Like the title says, I would like to know how to search a specified directory for a specified file using a for loop in a windows batch script. I can't use any 3rd party software.
Heres what I have at the moment:
for /R "%loc%" %%f in ("%searchterm%") do (
echo %%f
echo %%f>> "%genloc%wefound.txt"
)
%loc% is the location in which it should search.
%searchterm% is the file (e.g "hello.txt") that the script much search for.
It must output any files found (including their full path) on the screen and into the wefound.txt.
Let me know if you need more detail.
for /f "delims=" %%f in ('dir /s /b /a-d "%loc%\%searchterm%"') do (
echo %%f
echo %%f>> "%genloc%wefound.txt"
)
To get the for /r command recursively enumerate the files under a starting point, the file set must include some wildcard character (* or ?). As you know the exact filename, let the dir command do the work of searching the file.
for /R "%loc%" %%f in ("%searchterm%") do (
echo %%~Ff
echo %%~Ff>> "%genloc%wefound.txt"
)
For further details, type: FOR /?
Why complicate things with FOR? All you need is the DIR command:
dir /s /b /a-d "%loc%\%searchterm%>"%genloc%wefound.txt"&type "%genloc%wefound.txt"
The solution is even better if you can get your hands on a Windows port of the tee command (there are numerous free versions available). You could use my hybrid JScript/batch implementation of tee
dir /s /b /a-d "%loc%\%searchterm%"|tee "%genloc%wefound.txt"

Echo a single number to all .dll files in a directory

In a folder named "drops" I have 3 .dll files. I am looking for a piece of code that can echo the number 0 to each .dll file without having to echo them individually. Basically, I don't want to have to open up each file and change the number to 0... laziness ensues!
for /f %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO echo 0
I have tried using for. Is my code wrong? Thanks in advance.
Edit: Title didn't have anything to do with my question :D
Easy
forfiles /p "C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops" /m "*.dll" /c "cmd.exe /c echo 0 > #path"
That will do it. Also, in ou code, add > %%G Thats the only mistake I found.
Mona
Drop /F, use
FOR %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO ECHO 0
/F will try to open file *.dll.

Batch script: Search if a folder contains any files

I am trying to get a batch script to check whether a folder contains any files in it. So far this is how far I have got:
IF EXIST %FILEPATH%\%%i\FromGlobus\%FILE% (
%WINZIP% %FILEPATH%\GlobusEOD\ExtraFiles\%ZIPFILE% -m %FILE%
IF errorlevel 1 goto subBADEND
)
where %FILE% is *.* but what happens is it tries to zip up files even when none exist and therefore fails!
Any tips or ideas?
Thank you
After some tinkering with my own scripts
A variation of #Belisarius' answer is what I've been using for awhile...After tinkering for a bit, #NimeCloud's version just doesn't seem to work on my XP machine...
I've come up with a blend of the two that seems to work for my needs:
%FOLDER%=C:\Temp
FOR /F %%i in ('dir /b "%FOLDER%\*.*"') DO ( goto :Process )
goto :Exit
:Process
...
...
...
:Exit
exit
You may use something like
set VAR=init
for /f %%a in ('dir /b c:\kk\*.*') do set VAR=exists
if %VAR%==exists ...
Not very efficient in case of large directories, but it works.
HTH!
dir . returns . .. maybe these virtual dirs cause the mess. You could try FIND pipe like below:
SET filter=*.*
SET notfound="File not found"
DIR %filter% | FIND %notfound%
#If ErrorLevel 1 Goto :end
If you use /A:-D for dir command, then it will switch the return code if directory has files. Works only for files (/A:D won't work for subdirectories in the same way)
dir /A:-D /B "mydir" >nul 2>nul && echo.mydir having files
You could use find with dir to ensure the return code matches what you'd expect when no files are found:
DIR /A /B | >NUL FIND /V "" && ECHO Folder contains files || ECHO Folder empty

Resources