The Agony of Looping and Doing Foreach In XP DOS

07/07/12

It has been a primary goal of mine for several years to write a Windows program, in either DOS or Perl, to automate changing the folder icon in a batch of subdirectories.  It is quite tedious to do this process manually, and when I am trying to customize folder icons this process can eat up hours at a time!

Thus this post is simply to get my frustration down about all the time and effort I put into trying to figure this out and the dearth of knowledge on DOS commands and looping, and the alternative difficulty of doing this in other Windows-friendly languages…. Nature calls … Please let me know if you’ve ever had similar problems?!?

07/08/12

Here are some attempts I made last night to list all subdirectories (folders) in a given folder using the fully-qualified path name. Notice that in batchfoldericon.bat and batchfoldericon2.bat there is a lot of extra code … This is because after 3 hours of research and troubleshooting, I was finally able to get the code to do the variable expansion. But, for some reason it would never write the fully-qualified path to the text file … That or it would be reading the subdirs from one level up … huh? And to top it off, sometimes I’m getting the correct results with the for loop code I have, and other times it is giving me incorrect results as stated earlier!

NOTE: WordPress.com hosted code syntax highlighting doesn’t support “dos”, “batch”, “bat”, “command line”, “cmd”, or anything of that nature. Therefore, the closest logical relative is “powershell”, so that is the sourcecode language I am using to display the code below … It leaves much to be desired, but as stated, it’s not designed for DOS code. However, this problem is easily remedied when using a self-hosted WordPress site … There are nice plugins that a Google search will show that can do DOS code syntax highlighting just fine, but they are unavailable when you let Automatic host your site.

batchfoldericon.bat

[sourcecode language=”powershell”]
@echo off
REM — File: batchfoldericon.bat
REM — Created: 07/07/12
REM — Author: Eric Hepperle

REM — Notes: %1 refers to the first argument passed to the file.

rem @echo off
REM — THIS WORKS FOR LISTING LEV1 SUBDIRS! 🙂 –>
REM for /d %%X in (%~dp1*) do echo %%X >> c:listjpg.txt

REM for /r %1 %%X in (*.jpg) do (echo %%X >> c:listjpg.txt)
rem SETLOCAL EnableDelayedExpansion

if “%~1” == “.” goto :defaultdircond
if “%~1” == “” goto :defaultdircond

goto :dirpassed
:dirpassed
set %startdir% = %~f1
goto: processdirs

:defaultdircond
echo CONDITION cond was gone-to.
set %startdir% = %cmd%

:processdirs
REM for /d %%X in (%startdir%*) do (
REM echo %%X >> c:listjpg.txt
REM attrib -r “%startdir%*.*” /S /D
REM )
for /d %%X in (%~f1*) do echo %%X >> c:listjpg.txt

REM — THIS WORKS FOR GRABBING ALL LEV1 SUBDIRS AND LISTING
REM — THEIR FULLY QUALIFIED PATHS:
REM — for /d %%X in (%~f1*) do echo %%X >> c:listjpg.txt

[/sourcecode]

batchfoldericon2.bat

[sourcecode language=”powershell”]
@echo off
REM — File: batchfoldericon2.bat
REM — Created: 07/07/12
REM — Author: Eric Hepperle

REM — Notes: %1 refers to the first argument passed to the file.

if “%~1” == “.” goto :defaultdircond
if “%~1” == “” goto :defaultdircond

goto :dirpassed
:dirpassed
set %startdir% = %~f1
goto: processdirs

:defaultdircond
echo CONDITION cond was gone-to.
set %startdir% = %cmd%

:processdirs
for /d %%X in (%startdir%*) do (
echo %%X > c:listjpg.txt
rem @attrib -r “%%X”
)

REM — THIS WORKS FOR GRABBING ALL LEV1 SUBDIRS AND LISTING
REM — THEIR FULLY QUALIFIED PATHS:
REM — for /d %%X in (%~f1*) do echo %%X >> c:listjpg.txt
REM
REM — Get all lev1 subdirs can also be done by:
REM — dir /ad /b
[/sourcecode]

batchfoldericon3.bat

NOTE: This one works!!! — It writes fully-qualified pathnames to the output file correctly! 🙂 Now I just have to get it to process each folder!
[sourcecode language=”powershell”]
@echo offfor /d %%X in (%~f1*) do echo %%X >> c:listjpg.txt
[/sourcecode]
Date Published: 2012-07-07
Date Updated: 2022-09-09

Eric Hepperle

Eric loves to write code, play guitar, and help businesses solve challenges with code and design.
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x