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 10149 times)

0 Members and 1 Guest are viewing this topic.

novice84

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    skip specific folder in FOR Command
    « on: July 01, 2013, 07:14:02 AM »
    DIR command works fine to show all folders list except with/having name "noneed"

    Code: [Select]
    dir /b /a:d | find /v "noneed"
    in FOR command it works to search all as shown below :

    Code: [Select]
    for /f "delims=" %%B in ('dir /b /a:d') do
    but if put it in FOR command it doesn't work if we change to

    Code: [Select]
    for /f "delims=" %%B in ('dir /b /a:d  | find /v "noneed" ') do
    how we can skip specific folders in FOR command

    Salmon Trout

    • Guest
    Re: skip specific folder in FOR Command
    « Reply #1 on: July 01, 2013, 07:25:06 AM »
    When you are using certain special characters in a FOR dataset you need to "escape" them. The pipe symbol | is one of these and you need to escape it with a caret ^ like this

    for /f "delims=" %%B in ('dir /b /a:d  ^| find /v "noneed" ') do

    novice84

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: skip specific folder in FOR Command
      « Reply #2 on: July 01, 2013, 07:43:31 AM »
       :D thanks Salmon
      if you can please explain this ^ character and function in batch file it will be very helpful

      Salmon Trout

      • Guest
      Re: skip specific folder in FOR Command
      « Reply #3 on: July 01, 2013, 07:50:11 AM »

      novice84

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Re: skip specific folder in FOR Command
        « Reply #4 on: July 01, 2013, 08:08:03 AM »
        thanks Salmon link gives good explanation
        I am just beginner with basic Computer knowledge, so short explanation not enough to understand.

        one more Q. this single loop of FOR I am using, any other command I put underneath goes into loop
        how we can mention in batch file to start other command after finishing one loop and then next & next...

        May be I am not able to explain properly what I want but I hope you some other link to make me understand better. ???

        Salmon Trout

        • Guest
        Re: skip specific folder in FOR Command
        « Reply #5 on: July 01, 2013, 08:41:48 AM »
        one more Q. this single loop of FOR I am using, any other command I put underneath goes into loop

        How are you writing the loop? Please give an example of code that behaves as you describe.

        novice84

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Re: skip specific folder in FOR Command
          « Reply #6 on: July 02, 2013, 01:54:05 AM »
           :-[ :-[ :-[ :-[
          I thought if any code is doing repeated job is loop.
          lilttle embarrassed...... my poor knowledge..... :( :( :( :(
          what we will call this ?

          anyway, code I am using to is to rename multiple files (from other forum post)
          Code: [Select]
          ren "%~1" "%name%"
          and under that line if I put in next line

          Code: [Select]
          move C:\folder\*.txt C:\folder2it moves all files before naming

          I want to move few files altogether to one folder after renaming all
          and display some msg thru echo screen while this FOR LOOP or say FOR command is running

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: skip specific folder in FOR Command
          « Reply #7 on: July 02, 2013, 07:05:59 AM »
          If you are putting it under the other command then you probably want to move each file as it is renamed:

          Code: [Select]
          move "%~dp1\%name%" "C:\folder2"

          novice84

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Re: skip specific folder in FOR Command
            « Reply #8 on: July 02, 2013, 07:41:51 AM »
            what if I want to move after finishing complete rename, moving few files not all, some at different place.
            and if want to start some other command not related to first task how we can start after finishing first one.
            what command/code we have to put in in-between two codes of FOR LOOP or FOR commands
            e.g. if want to display msg using echo and pause or open a folder, this interfere with rename process if we put at end of code

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: skip specific folder in FOR Command
            « Reply #9 on: July 02, 2013, 07:59:21 AM »
            Shows us your code and then ask your question.

            novice84

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              Re: skip specific folder in FOR Command
              « Reply #10 on: July 03, 2013, 07:46:27 AM »
               ???
              thankfully I got code from you Foxidrive from other forum batch rename
              may be you help people in so many fourms so you don't remember.
              anyway,  I am just putting move command under the code you made
              like this
              move C:\test\*.txt C:\test2\text-files
              move C:\test\*.doc C:\test2\DOC-files

              what I want is just to separate both process. After renaming it should do next step.

              other thing is just my curiosity. say if I am doing rename and moving and it is taking 10-15 seconds, instead blinking or showing move files messages, on screen can we display msg with echo
              Renaming & Sorting Files please wait.....


              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: skip specific folder in FOR Command
              « Reply #11 on: July 03, 2013, 09:08:26 AM »
              It's not surprising that I forget all the people I've written code for - a lot don't ever reply so it's like my posts go into a black hole.

              I don't follow your questions though as we need to see the code to follow what you are referring to.

              You can always put this before the process - it won't change the code.
              echo Renaming & Sorting Files please wait.....

              novice84

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                Re: skip specific folder in FOR Command
                « Reply #12 on: July 04, 2013, 06:06:42 AM »
                got it right... 8) I was putting echo at wrong place.
                here is the code...
                Code: [Select]
                @echo off
                pushd "C:\test\folder"
                for /f "delims=" %%a in ('dir /b /a-d * ') do call :next "%%a"
                popd
                pause
                goto :eof
                :next
                set "name=%~nx1"
                set "name=%name:old=old01%"
                ren "%~1" "%name%"
                « Last Edit: July 04, 2013, 06:42:23 AM by novice84 »

                novice84

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 7
                  Re: skip specific folder in FOR Command
                  « Reply #13 on: July 04, 2013, 07:30:54 AM »
                  I fixed it. :D
                  thanks Foxidrive, your tip was good.
                  I was also putting move command lines at wrong position.

                  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.

                  2. what we will call this FOR command ? isn't it a loop ?

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

                  thanks for your guidance in advance
                  It's not surprising that I forget all the people I've written code for - a lot don't ever reply so it's like my posts go into a black hole.


                   :o I did replied and thanked you. You are doing great help (a good karma). Saved a lot of time for me. I assure you, It's not going into black hole.
                  « Last Edit: July 04, 2013, 08:23:30 AM by novice84 »

                  Lemonilla



                    Apprentice

                  • "Too sweet"
                  • Thanked: 70
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Windows 7
                  Re: skip specific folder in FOR Command
                  « Reply #14 on: July 04, 2013, 09:20:14 AM »
                  1) Currently any other command added to the bottom will also be run during the "for loop".

                  2) It is a "for loop".

                  3) 'goto :eof' tells it to return to the place in the code it was before being called. currently it is not doing anything because it is not below a flag/label.  if you were to move it to the bottom, it would bounce back to inside the for loop after all commands were executed when you use 'call :next'.



                  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?
                  Quote from: patio
                  God Bless the DOS Helpers...
                  Quote
                  If it compiles, send the files.

                  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)