Saved Bookmarks
| 1. |
Solve : skip specific folder in FOR Command? |
|
Answer» Quote from: Lemonilla on July 04, 2013, 09:20:14 AM Quick question of my own (more addressed to others on the board), why would you not write it all in one place. I wonder if some people prefer doing it the CALL way because they don't like DELAYED expansion for some reason. Quote from: Lemonilla on July 04, 2013, 09:20:14 AM Quick question of my own (more addressed to others on the board), why would you not write it all in one place. You lose the ! characters from the names. It matters for common things like movie names and music titles.Quote from: novice84 on July 04, 2013, 07:30:54 AM 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 It stops the subroutine being executed one final time, after all the filenames have been sent through the subroutine. Thanks for your comments.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 advanceQuote from: novice84 on July 06, 2013, 03:47:31 AM
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] Quote from: novice84 on July 06, 2013, 03:47:31 AM Code: [Select]dir /b /s /a:d | find /I "NEEDtoCheck" > list2.txt 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. 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 QUESTIONI 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 THANKS FOXIDRIVE & Salmon Trout FOR YOUR HELP IT'S NOT ONLY HELPED TO SAVE TIME BUT ALSO HELPED TO UNDERSTAND NEW THINGS. Quote from: foxidrive on July 06, 2013, 10:38:56 PM 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. Quote from: Salmon Trout on July 06, 2013, 04:22:48 AM 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.Quote from: novice84 on July 07, 2013, 05:41: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) |
|