Solved Batch script to scan for folders that don't contain a file

April 18, 2015 at 12:44:04
Specs: Windows XP
Hey guys,

I'm a systems admin looking to write a .bat that'll scan folders, see if they contain a certain file type (.id) and then output the ones that DONT to a text file.

As an example, we have a parent folder called userdata that contains each users data (unsurprisingly). I want to scan each users folder to see if it contains a .id file, then tell me which folders don't have it.

Thanks for your time!


See More: Batch script to scan for folders that dont contain a file

Report •

#1
April 18, 2015 at 19:40:44
Maybe:

For /d %%a in (DRIVE:\PATH\USERDATA\*) do if not exist %%a\*.id >> results.txt echo %%a

message edited by nbrane


Report •

#2
April 18, 2015 at 20:09:11
Hey man, thanks for the response, I tried that but it still came up with a list of all folders, not just the ones that don't contain the file.

Report •

#3
April 18, 2015 at 20:51:41
✔ Best Answer
I don't know. It worked fine on my tests, on winXP. I wrote a "one-liner". The results.txt must be sure to be cleared before-hand:

@echo off>results.txt
For /d %%a in (DRIVE:\PATH\USERDATA\*) do if not exist %%a\*.id >> results.txt echo %%a

If you've substituted DRIVE,PATH, USERDATA with your objective, and the location of "results.txt", it should work as advertised. (About 99 percent of difficulties and misinterpretations in "programming" forum are with paths! either that or running the batch from a desktop icon instead of commandline. My solution is always to be run from the command prompt.)

message edited by nbrane


Report •

Related Solutions

#4
April 18, 2015 at 21:42:15
::====== script starts here ===============
:: list folders NOT containing
:: rocket.bat 2015-04-19 11:30:00.04
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s/ad d:\files') do (
if not exist "%%a\*.txt" echo.%%a
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#5
April 19, 2015 at 14:41:56
So, I'm still getting the error with:

@echo off>results.txt
For /d %%a in (D:\USERDATA\*) do if not exist %%a\*.id >> results.txt echo %%a

Making sure to run it from cmd and to delete results.txt each time. What am I doing wrong?


Report •

#6
April 19, 2015 at 19:52:49
Here's what I ran, cut/pasted from your last post:

@echo off>results.txt
For /d %%a in (D:\USERDATA\*) do if not exist %%a\*.id >> results.txt echo %%a

::=== end
I created D:\USERDATA, and put three subdirs (including spaces in their names just in case). In two, I put a .id file, and the other one not. The test worked, resulting in one when "NOT", and the other two without the "NOT, so I'm stumped. Have you tried M2's version?

message edited by nbrane


Report •

#7
April 19, 2015 at 23:18:41
LOL

nbrane's works; so does M2's

=====================

M2 Get custom script or take private lessons


Report •

#8
April 20, 2015 at 21:52:22
@M2: Yeh, I figure there's something else hiding behind the curtain. @OP: good luck. There's got to be something amiss...

Report •

#9
April 21, 2015 at 21:11:46
So, the folder structure is set up as D:\\Userdata\Firstname.Lastname\Notes8 and the notes8 folder is where the .id file is located. Do I perhaps have to specify? Or should the * command drill down through the folder structure?

Thanks again for the help guys, I'm pretty stumped.


Report •

#10
April 22, 2015 at 00:48:21
Ah. I see what the problem is. I'll get back tomorrow with a solution, assuming you still need it. I didn't account for subdirs off the base....
Ah ha! Ha ha! we both got about the same solution:

@echo off>results.txt
for /d %%a in (d:\userdata\*) do if not exist %%a\notes8\*.id >>results.txt echo %%a

message edited by nbrane


Report •

#11
April 22, 2015 at 01:42:36
No need, I fixed it up, pretty much answered my own question.

Just needed an extra bit to specify the subfolder:

For /d %%a in (D:\USERDATA\*) do if not exist %%a\notes8\*.id >> results.txt echo %%a


Report •

Ask Question