Solved I need help to write a batch file that will sort folder

August 11, 2013 at 20:32:48
Specs: Windows XP
Write a batch file that will sort a text file of a directory listing and rewite it into different files depending on folder depth.

e.g. input = input.txt which contains something like

g:\folder1
g:\folder1\folder2
g:\folder3
g:\folder4\folder5
g:\folder6\folder7\folder8
I want to sort them by counting the delims ('\') and write them to different files so I will have 3 different files.. 1 folder deep listings, 2 folder deep listings, 3 folder deep listings.

I have this

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=\" %%a in (f.txt) do (
set /a N+=1
Set VAR1=word1 word2 word3 word4
Set count=0
For %%j in (%VAR1%) Do Set /A count+=1
Echo.%%a >> %count%.txt
)
but its not very close >.<
Thanks in advance

message edited by TomBombo


See More: I need help to write a batch file that will sort folder

Report •


#1
August 12, 2013 at 19:46:57
Homework problems are somewhat discouraged here, for obvious reasons. So I will just give a general description of what I would do. First, create a sub that measures the length of a string. Call the sub with the line-input from the textfile. Replace the "\" with null, then call the sub again. Then find the difference betweent the two lengths. You can dodge enabledelayedexpansion if you're dextrous using subs. There are other means of doing this, but this one might allow greater depth of folders to be analyzed.

Report •

#2
August 12, 2013 at 19:59:10
Thanks for your reply. What do you mean homework problems? No sorry I am working on a project to clean our sharted folders for our large government organisation. Once I have the folder levels sorted I plan to put them in a batch file and run icacls against them and process the results.

I dont know really if its needed to replace the '\' and count folder lengths. Shesh I just wanted to count the levels but what is suggested seems to make it just more complicated.


Report •

#3
August 12, 2013 at 21:15:53
✔ Best Answer
Ok, fair enough. My aplogies, but the intro was really suggestive of HW: "Write a batch file that will ..." I can hear the strident voice of the instructor...
Ok, I tend to assume "worst-case" and try to make my code work under "extreme demand conditions" (in this case, more than 3 or 4 levels deep). Maybe overkill...
If there are no spaces, or commas, in the fullpath of the filenames", then this might
work:
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (f.txt) do (
set z=%%a
set z=!z:\=,!
set /a c=0
set q=
call :a !z!
)

goto :eof

:a
:: not sure what format, cumulative vs specific, choose your weapon accord...
rem set q=%1
set q=%q% %1
set /a c+=1
shift
if "%1" neq "" goto :a
echo level %c% %q%
::=========== end batchscript
ps: if you do have issues with spaces and/or commas in filenames, you might regenerate the list using the "dir /x" option, but that f_s up the /b option (THANKS, MIcrosoft!).

message edited by nbrane


Report •

Related Solutions

#4
August 13, 2013 at 21:04:56
Ok, I wanted to experiment, so here's another routine for splitting strings:

@echo off & setlocal
::splits a string indefinitely on any non-critical delimiter.
set dlm=\
dir /s /b > workk
for /f "tokens=*" %%a in (workk) do (
echo %%a
call :b "%%a"
set ~
echo count: !c!
pause
for /L %%e in (1 1 !c!) do set ~%%e=
set c=0
)
goto :eof

:b
set /a c+=1
echo level: %c%
pause
echo current input: %~1
for /f "tokens=1* delims=%dlm%" %%c in ("%~1") do (
echo this cell, c=%%c
set ~%c%=%%c
echo next feed, d=%%d
call :b %%d
)::======== end demo-batchscript

message edited by nbrane


Report •

#5
August 14, 2013 at 13:43:41
If I were doing something like this for a commercial operation or a Government department I wouldn't mess about with batch files. Do it properly and write a program in C or Perl, both of which provide much better string handling capabilities and will allow you to incorporate proper error handling.

Report •

#6
August 14, 2013 at 22:31:49
Thanks guys.. I am really in over my head. I wanted to manually fix all the security groups but my boss wants me to automate it. Its just a huge waste of time.
We are going to tackle it now folder by folder.
Thanks again for help/suggestions

Report •

#7
August 15, 2013 at 18:05:34
Ha ha! I know the feeling, but usually for me it was the other way around, trying to convince someone that I can ace this job with some programming. Ijack has it right though, batchscript is rarely the best solution for anything serious. Many times I've seen on here where management prohibits anything other than batchscript due to misguided paranoia, shooting themselves in the foot for not using something that works much more effectively. Good luck with the project :-)

Report •

#8
August 16, 2013 at 05:28:20
You should still have access to VBScript, though.

How To Ask Questions The Smart Way


Report •

Ask Question