1

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
)
3
  • Your first script works for me (Win7). What has to be deleted before it works? Commented Jun 26, 2013 at 10:55
  • @Jubjub Bandersnatch When I run it, it does exactly what it is supposed to do except actually change the folder icon (Also Win7), it will only chance the icon when a file is deleted within the folder. Commented Jun 26, 2013 at 11:25
  • That sounds more like an issue with Explorer rather than your script, though. Commented Jun 26, 2013 at 12:52

3 Answers 3

2

try this:

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

%cd% wouldn't be listed.

Sign up to request clarification or add additional context in comments.

1 Comment

Works well! Changed end variable to %%~nd to just get the folder name, thanks a lot.
0

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.

2 Comments

Thanks, it looks as though it would do the trick, although the sub directories contain around 1,000-2,000 folders. I will have to up the loop and test it!
wow, u should REALLY try forfiles its very useful sometimes over the for /r command
0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.