Help with batch script - FORFILES

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Help with batch script - FORFILES

#1 Post by jdicerch » 28 Jul 2015 06:24

So I am trying to loop through every file in a folder, and read the first line in that file. Can someone let me know what i am doing wrong, or a better way to do it?

Also, the error I get says " filenamel:"' > was unexpected at this time.

Code: Select all

for /r "C:\Files\" %%a in (*) do call:head "%%~a"

:head
set filename=%~1
for /l %i in (1,1,1) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" ('echo %filename%') ^| findstr /r "^%l:"') do @echo %b
EXIT /B 0

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

Re: Help with batch script - FORFILES

#2 Post by foxidrive » 28 Jul 2015 07:43

On a quick glance I see three errors and forfiles is not being used, as in the thread title

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

Re: Help with batch script - FORFILES

#3 Post by jdicerch » 28 Jul 2015 07:55

foxidrive wrote:On a quick glance I see three errors and forfiles is not being used, as in the thread title



Can you point out the three errors?

And I meant i wanted to try and use Forfiles, but could not get it to work. Which way would you suggest?

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

Re: Help with batch script - FORFILES

#4 Post by foxidrive » 28 Jul 2015 09:10

jdicerch wrote:Could you please point out those errors? Also, i tried using forfiles but could not get it to work.


The first two posts of a new poster need approval, you can post freely now.

I'm happy to give code but I need to be clear about the task, because all the regulars dislike having
to change the code if the requirements are changed in a subsequent post (unless they cock it up themselves).

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

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

Re: Help with batch script - FORFILES

#5 Post by jdicerch » 28 Jul 2015 09:30

foxidrive wrote:
jdicerch wrote:Could you please point out those errors? Also, i tried using forfiles but could not get it to work.


The first two posts of a new poster need approval, you can post freely now.

I'm happy to give code but I need to be clear about the task, because all the regulars dislike having
to change the code if the requirements are changed in a subsequent post (unless they cock it up themselves).

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



Of course! Thanks for the help.. I have files in a folder.. lets say that is C:\Folder\....I need to read the first line of each file, and if that first line contains a word (lets call it "word1" or "word2" or "word3") then i need to move that specific file to another folder C:\Word1, C:\Word2, C:\Word3 respectively.

Does that make sense?

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

Re: Help with batch script - FORFILES

#6 Post by foxidrive » 28 Jul 2015 09:49

Yes, it makes sense.

The make up of the phrase being searched for can change the code, if it contains certain characters.
Exact details are always good.

See here: viewtopic.php?f=3&t=6108

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

Re: Help with batch script - FORFILES

#7 Post by jdicerch » 28 Jul 2015 09:57

Not a phrase... Just words.. The words are Mary Cory Queen

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

Re: Help with batch script - FORFILES

#8 Post by Meerkat » 28 Jul 2015 10:14

Hope this helps! :D

I created C:\Files, C:\Folder1, C:\Folder2, C:\Folder3.
I made three text files: A.txt, B.txt, C.txt (First lines are "Folder1","...2","...3" respectively).

A.txt

Code: Select all

Folder1
123
1


B.txt

Code: Select all

Folder2
222


0000


C.txt

Code: Select all

Folder3
aaaaaaaaaaaaaaaaaaaaaaabbbbb
bbbbbbbbcc
dddd

dddddddrrrr


I am quite lazy to write a good sample text files :mrgreen:

A workaround code (sorry for its ugliness):

Code: Select all

@echo off
for /r "C:\Files\" %%a in (*) do call :head "%%~a"
pause
exit /b 0

:head
set "filename=%~1"
for %%F in (Folder1 Folder2 Folder3) do (
   for /f "tokens=1,* delims=:" %%A in (
      'type "%filename%"^|findstr /n "%%F"'
   ) do (
      if %%A equ 1 move /Y "%filename%" "C:\%%F"
   )
)
exit /b 0


Does the job, but because of the move of files, but the %%a is not updated, there are errors:
Output:

Code: Select all

        1 file(s) moved.
The system cannot find the file specified.
The system cannot find the file specified.
        1 file(s) moved.
The system cannot find the file specified.
        1 file(s) moved.
Press any key to continue . . .


The error came from the 'type ......' part.

I am not good on "formal" error handling. However, since that is not really a "huge" error, we can just prevent the error message to be printed:

(Slightly edited...)

Code: Select all

@echo off

for /r "C:\Files\" %%a in (*) do (
for %%F in (Folder1 Folder2 Folder3) 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"
   )
)
)
pause


Output:

Code: Select all

        1 file(s) moved.
        1 file(s) moved.
        1 file(s) moved.
Press any key to continue . . .


I think there are simplier techniques ('coz I am still learning...), but this is what I can provide.

Meerkat
Last edited by Meerkat on 28 Jul 2015 10:21, edited 2 times in total.

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

Re: Help with batch script - FORFILES

#9 Post by foxidrive » 28 Jul 2015 10:17

Here's another way:

This uses a native Windows batch script called findrepl.bat (by aacini)
- download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
and it can also be found here: viewtopic.php?f=3&t=4697

Place it in the same folder as the batch file, or in a folder that is on the system path.

Code: Select all

@echo off
cd /d "C:\Folder\"
for %%a in (*) do (
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Mary">nul && move "%%a" "c:\Mary" >nul
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Cory">nul && move "%%a" "c:\Cory" >nul
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Queen">nul && move "%%a" "c:\Queen" >nul
)

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

Re: Help with batch script - FORFILES

#10 Post by jdicerch » 28 Jul 2015 10:29

Meerkat wrote:Hope this helps! :D


0000[/code]



A workaround code (sorry for its ugliness):

Code: Select all

@echo off
for /r "C:\Files\" %%a in (*) do call :head "%%~a"
pause
exit /b 0

:head
set "filename=%~1"
for %%F in (Folder1 Folder2 Folder3) do (
   for /f "tokens=1,* delims=:" %%A in (
      'type "%filename%"^|findstr /n "%%F"'
   ) do (
      if %%A equ 1 move /Y "%filename%" "C:\%%F"
   )
)
exit /b 0


Meerkat



I think this worked (said files were moved), but where did it move them to....

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

Re: Help with batch script - FORFILES

#11 Post by jdicerch » 28 Jul 2015 10:29

foxidrive wrote:Here's another way:

This uses a native Windows batch script called findrepl.bat (by aacini)
- download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
and it can also be found here: viewtopic.php?f=3&t=4697

Place it in the same folder as the batch file, or in a folder that is on the system path.

Code: Select all

@echo off
cd /d "C:\Folder\"
for %%a in (*) do (
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Mary">nul && move "%%a" "c:\Mary" >nul
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Cory">nul && move "%%a" "c:\Cory" >nul
if exist "%%a" type "%%a"|findrepl /o:1:1 |find "Queen">nul && move "%%a" "c:\Queen" >nul
)



I will try it with using that other bat if i cant get it working without having two batch files.. Thanks for the suggestion!

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

Re: Help with batch script - FORFILES

#12 Post by Meerkat » 28 Jul 2015 10:33

@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.

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

Re: Help with batch script - FORFILES

#13 Post by jdicerch » 28 Jul 2015 10:35

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.


I didn't create the appropriate folders, and i think it just deletes the file is the directory doesn't exist :P

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

Re: Help with batch script - FORFILES

#14 Post by Meerkat » 28 Jul 2015 10:41

If no folders are created, you will see the files (w/o extensions) in C:\ drive.

Meerkat

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

Re: Help with batch script - FORFILES

#15 Post by jdicerch » 28 Jul 2015 10:43

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\"
   )
   )
)
)

Post Reply