Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: skip specific folder in FOR Command  (Read 10156 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: skip specific folder in FOR Command
« Reply #15 on: July 04, 2013, 01:05:08 PM »
Quick question of my own (more addressed to others on the board), why would you not write it all in one place.

[...]

Is there any differences, or is it just a stylistic thing?

I wonder if some people prefer doing it the CALL way because they don't like delayed expansion for some reason.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: skip specific folder in FOR Command
« Reply #16 on: July 04, 2013, 07:07:58 PM »
Quick question of my own (more addressed to others on the board), why would you not write it all in one place.
Code: [Select]
setlocal EnableDelayedExpansion
for /f "delims=" %%A in ('dir /b /a-d * ') do (
set "name=%%~nxA"
set "name=!name:old=old01!"
ren "%%~A" "!name!"
)
Is there any differences, or is it just a stylistic thing?

You lose the ! characters from the names.  It matters for common things like movie names and music titles.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: skip specific folder in FOR Command
« Reply #17 on: July 04, 2013, 07:25:05 PM »
1. but still question remains what if I put any other FOR command or any other command under this code for some different purpose, will it work ? & how it will work ? how separate next code from this.

This is a for loop and it calls a subroutine. 
You can add commands to it but placement is important.

try it with the commands below and it might be clearer


@echo off
:: start here
pushd "C:\test\folder"
:: this for loop sends each filename to the subroutine
for /f "delims=" %%a in ('dir /b /a-d * ') do call :next "%%a"
popd
:: this is the end of the batch file on the goto :eof
echo Done
pause
goto :eof


:next
:: for each filename this subroutine is executed
echo The file being processed is "%~nx1"
set "name=%~nx1"
set "name=%name:old=old01%"
echo The file is being renamed from "%~1" to "%name%"
ren "%~1" "%name%"
:: you can add commands to change what it does or to do extra things
pause



Quote from:
3. another question what is
Code: [Select]
goto :eofin the code. Even if I remove it, doesn't make any difference!!!!!???

It stops the subroutine being executed one final time, after all the filenames have been sent through the subroutine.

Thanks for your comments.

novice84

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    Re: skip specific folder in FOR Command
    « Reply #18 on: July 06, 2013, 03:47:31 AM »
    questions related to original topic :
    I am trying to make a list of folders having words in folder name "NEEDtoCheck"
    It could be complete name of folder or part of folder name.

    Code: [Select]
    dir /b /s /a:d | find /I "NEEDtoCheck" > list2.txt
    above code works fine. But it also includes sub folders. e.g. list look like this :
    C:\TEST FOLDER\old\this NEEDtoCheck
    C:\TEST FOLDER\old\this NEEDtoCheck\too

    second line is unwanted. how not to get this ?

    In another batch for some reason I could not get this code working.

    Code: [Select]
    pushd "C:\TEST FOLDER"
    for /D "delims=" %%B in ('dir /b /s /a:d ^| find /I "NEEDtoCheck" ') do echo Check %%~fB>> list2.txt
    popd
    pause

    Thanks all in advance
    « Last Edit: July 06, 2013, 04:11:04 AM by novice84 »

    Salmon Trout

    • Guest
    Re: skip specific folder in FOR Command
    « Reply #19 on: July 06, 2013, 04:22:48 AM »

    I could not get this code working.

    Code: [Select]
    for /D "delims=" %%B in

    Study FOR documentation - type FOR /? at the prompt or else Google for Windows command line & batch syntax guides. You need to start doing this.

    You are mixing FOR /D and FOR /F syntax. No "options" block allowed with /D switch. It will repeat the options block in an error message, for example like this: "delims=" was unexpected at this time. (Didn't you see this?)

    FOR /D %variable IN (set) DO command [command-parameters]

    FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
    FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
    FOR /F ["options"] %variable IN ('command') DO command [command-parameters]





    Salmon Trout

    • Guest
    Re: skip specific folder in FOR Command
    « Reply #20 on: July 06, 2013, 04:27:22 AM »
    Code: [Select]
    dir /b /s /a:d | find /I "NEEDtoCheck" > list2.txt
    above code works fine. But it also includes sub folders.

    If you study the DIR documentation, (type DIR /? at the prompt) and then look at the switches you are using, (/b /s /a:d) you will understand why sub directories are being listed, and what you need to change or remove. Don't be afraid of experimenting.


    novice84

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: skip specific folder in FOR Command
      « Reply #21 on: July 06, 2013, 05:14:58 PM »
      with all due respect  Salmon Trout
      For command I have not understood yet, as I have just started learning.
      But I have done experiment with DIR command. Please see complete code (as it is  in batch file) below again :

      Code: [Select]
      pushd "C:\TEST FOLDER"
      dir /b /s /a:d | find /I "NEEDtoCheck" > list2.txt
      popd

      and results example as below :
      C:\TEST FOLDER\old\this NEEDtoCheck
      C:\TEST FOLDER\old\this NEEDtoCheck\too
      C:\TEST FOLDER\old\this NEEDtoCheckalso
      C:\TEST FOLDER\old\this also NEEDtoCheck
      C:\TEST FOLDER\old\this also NEEDtoCheck\too

      there are no words "NEEDtoCheck" in "too" folder.

      I put /S to search in subdirectories, if don't put it just search in C:\TEST FOLDER and no result as folder list I need exist in deep down in directories.

      /a:d I have added to list only folders

      I hope you will understand my question

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: skip specific folder in FOR Command
      « Reply #22 on: July 06, 2013, 10:38:56 PM »
      I think there was some misunderstanding with your aim - your descriptions were a little unclear.

      This seems to be what you are after and it should exclude all the folders that do not have NEEDtoCheck in them.

      Code: [Select]
      @echo off
      pushd "C:\TEST FOLDER"
      for /f "delims=" %%a in ('dir /b /s /a:d') do (
      echo "%%~nxa" | find /I "NEEDtoCheck" >> list2.txt
      )
      popd

      novice84

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Re: skip specific folder in FOR Command
        « Reply #23 on: July 07, 2013, 05:41:28 AM »
        THANKS FOXIDRIVE & Salmon Trout FOR YOUR HELP
        IT'S NOT ONLY HELPED TO SAVE TIME BUT ALSO HELPED TO UNDERSTAND NEW THINGS.

        I think there was some misunderstanding with your aim - your descriptions were a little unclear.

        You are right. Sometimes for a NOVICE it's not easy to explain and mix up & mess things.
        sorry for confusion.

        It will repeat the options block in an error message, for example like this: "delims=" was unexpected at this time. (Didn't you see this?)

        I tried to look what's happening but even without @echo off and putting pause at the end of code, screen just blink and I could not see what is error.
        « Last Edit: July 07, 2013, 05:55:14 AM by novice84 »

        Salmon Trout

        • Guest
        Re: skip specific folder in FOR Command
        « Reply #24 on: July 07, 2013, 06:19:28 AM »
        I tried to look what's happening but even without @echo off and putting pause at the end of code, screen just blink and I could not see what is error.

        If you start batch scripts from Windows Explorer by double clicking, if the script bombs out you may see nothing. To see error messages etc you can open a command prompt in the folder and start the batch script manually (by typing its name and then hitting ENTER)