Built on Sin
Built on Sin

Reputation: 351

for loop batch file

How can I copy the batch file to subdirectories, run it, then delete the batch file?

I am working on a batch file that will automatically change the folder icon of the folder it is in. Shown below:

@ECHO OFF

attrib +s "%CD%"
set ICODIR=%CD%\Icon\

for %%F in ("%ICODIR%"*.ico) do set ICO=%%~nxF

set ICOINI=Desktop.ini

IF EXIST Desktop.ini (
    attrib -s -h %ICOINI%
)

echo [.ShellClassInfo] > %ICOINI%
echo IconResource=%ICODIR:~2%%ICO%>>%ICOINI%
echo InfoTip=%ICO:~0,-4%>>%ICOINI%

attrib -a +s +h %ICOINI%

Pause

This works, after many problems found out that it will not create the folder icon until a file is deleted within that directory.

I have been trying to work on a for loop that will list all sub directories and store their names. Though it lists the root directory first. How can I get it to skip the root directory? Code shown Below:

@ECHO OFF

for /R /D "delims=\" %%d IN (%CD%) do echo %%~nd

Pause

EDIT: Why is the file only made in the last folder?

for /D /R "%cd%" %%d IN (*) do (

    set something=%%~nd 
    echo TEST>%something%\Desktop.txt
)

Upvotes: 1

Views: 475

Answers (3)

Endoro
Endoro

Reputation: 37589

try this:

for /D /R "%cd%" %%d IN (*) do echo %%~d

%cd% wouldn't be listed.

Upvotes: 2

mojo
mojo

Reputation: 4142

For the second part, I found that adding an IF statement allows you to filter out the root of the search. Your example didn't work at all for me, so I had to modify it a little. /R prepends the current dir in the search (not %CD%) to the loop variable, so I had to strip some sentinel value out. I deemed it unlikely enough to find files with "ZZZZZ" in the name, but your filesystem may need a different sentinel.

FOR /R "%CD%" %%d IN (ZZZZZ) do @(SET "D=%%~d" & SET "D=!D:\ZZZZZ=!" & IF NOT "!D!"=="%CD%" @ECHO !D!)

Upvotes: 0

Monacraft
Monacraft

Reputation: 6620

Hmm.. Interesting scenario.

Try looking into the forfiles command at http://ss64.com/nt/forfiles.html . However using the for command this is the best solution I could come up with.

:loop
set /a count+=1

for /f "tokens=%count% delims=\" %%a in ("%CD%") do (echo %%a > 
output.txt)

if %count% == 8 exit
goto :loop

Note this is not the best answer but it works. This instance will check a maximum of 7 directories in from the drive letter but by incrementing the if check.

This Worked for me, hope it helps.

Yours Mona.

Upvotes: 0

Related Questions