Help with batch script - FORFILES

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Help with batch script - FORFILES

#16 Post by Meerkat » 28 Jul 2015 11:03

jdicerch wrote:
Meerkat wrote:@foxidrive Findrepl.bat is cool!

Note: I am serious that I created those files (A.txt etc..) and folders (C:\Folder1 etc..) in my PC.


How do i put an else in here? If it doesn't match any of these, put it in "Other" folder. the below doesnt seem to work

Code: Select all

@echo off

for /r "C:\Files\" %%a in (*) do (
for %%F in (Cory Mary Queen) do (
   for /f "tokens=1,* delims=:" %%A in (
      'type "%%~a" 2^>nul^|findstr /n "%%F"'
   ) do (
      if (
   %%A equ 1 move /Y "%%~a" "C:\%%F\"
   ) else (
      move /Y "%%~a" "C:\Other\"
   )
   )
)
)


:oops: Sorry I didn't expect that. :cry:

The else does not work because the FOR command ignores blank lines/output. In my code, since 'type "%%~a" 2^>nul^|findstr /n "%%F"' will not give anything if findstr does not find %%F in %%~a. So the whole DO part will not be executed.

This is not the "formal" approach to resolve the code snippet and this will make the code even uglier, but I will still post it: :cry:

Code: Select all

@echo off

for /r "C:\Files\" %%a in (*) do (
for %%F in (Cory Mary Queen) do (
   for /f "tokens=1,* delims=:" %%A in (
      'type "%%~a" 2^>nul^|findstr /n "%%F"^|^|echo. '
   ) do (
   if %%A equ 1 (
      move /Y "%%~a" "C:\%%F\"
   ) else (
      move /Y "%%~a" "C:\Other\" 2>nul
   )
   )
)
)

pause


Meerkat

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with batch script - FORFILES

#17 Post by foxidrive » 28 Jul 2015 11:11

jdicerch wrote:How do i put an else in here? If it doesn't match any of these, put it in "Other" folder.

foxidrive wrote:because all the regulars dislike having
to change the code if the requirements are changed in a subsequent post

if you can confirm the exact task then it would help.


Which part of my post *did* you read? :D

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with batch script - FORFILES

#18 Post by foxidrive » 28 Jul 2015 11:15

Meerkat wrote:@foxidrive Findrepl.bat is cool!


Yes, it is! There are other gems here too.

jdicerch
Posts: 11
Joined: 28 Jul 2015 06:22

Re: Help with batch script - FORFILES

#19 Post by jdicerch » 28 Jul 2015 11:33

foxidrive wrote:
jdicerch wrote:How do i put an else in here? If it doesn't match any of these, put it in "Other" folder.

foxidrive wrote:because all the regulars dislike having
to change the code if the requirements are changed in a subsequent post

if you can confirm the exact task then it would help.


Which part of my post *did* you read? :D



I like trying to do stuff on my own, and thought i could modify it if i get a little help... I just thought of the other folder now :cry:

jdicerch
Posts: 11
Joined: 28 Jul 2015 06:22

Re: Help with batch script - FORFILES

#20 Post by jdicerch » 28 Jul 2015 11:34

Code: Select all

@echo off

for /r "C:\Files\" %%a in (*) do (
for %%F in (Cory Mary Queen) do (
   for /f "tokens=1,* delims=:" %%A in (
      'type "%%~a" 2^>nul^|findstr /n "%%F"^|^|echo. '
   ) do (
   if %%A equ 1 (
      move /Y "%%~a" "C:\%%F\"
   ) else (
      move /Y "%%~a" "C:\Other\" 2>nul
   )
   )
)
)

pause


Meerkat[/quote]

Works great so far :P Now i didnt know this at first, but this does every folder inside that folder.. anyway to make it not recursive?

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Help with batch script - FORFILES

#21 Post by Meerkat » 28 Jul 2015 11:38

I am OK now. I have no regrets :)

I am not an expert, but I am happy and enthusiastic in helping...

Meerkat

jdicerch
Posts: 11
Joined: 28 Jul 2015 06:22

Re: Help with batch script - FORFILES

#22 Post by jdicerch » 28 Jul 2015 11:50

Meerkat wrote:I am OK now. I have no regrets :)

I am not an expert, but I am happy and enthusiastic in helping...

Meerkat



So no way to make it non-recursive? I tried changing the first line to

Code: Select all

for /D %%G in ("C:\Files\")
but that didnt seem to work

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Help with batch script - FORFILES

#23 Post by Meerkat » 28 Jul 2015 22:25

Hmm... I do not know FOR /D. :mrgreen:

I also do not know what does it mean to be non-recursive, but think this solves? :roll:

Code: Select all

for /f %%a in ('dir /b C:\Files\') do (

Post Reply