Page 1 of 2
Help with batch script - FORFILES
Posted: 28 Jul 2015 06:24
by jdicerch
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
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 07:43
by foxidrive
On a quick glance I see three errors and forfiles is not being used, as in the thread title
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 07:55
by jdicerch
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?
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 09:10
by foxidrive
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.
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 09:30
by jdicerch
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?
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 09:49
by foxidrive
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
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 09:57
by jdicerch
Not a phrase... Just words.. The words are Mary Cory Queen
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:14
by Meerkat
Hope this helps!
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
B.txt
C.txt
Code: Select all
Folder3
aaaaaaaaaaaaaaaaaaaaaaabbbbb
bbbbbbbbcc
dddd
dddddddrrrr
I am quite lazy to write a good sample text files
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
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:17
by foxidrive
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.batand it can also be found here:
viewtopic.php?f=3&t=4697Place 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
)
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:29
by jdicerch
Meerkat wrote:Hope this helps!
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....
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:29
by jdicerch
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.batand it can also be found here:
viewtopic.php?f=3&t=4697Place 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!
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:33
by Meerkat
@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.
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:35
by jdicerch
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

Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:41
by Meerkat
If no folders are created, you will see the files (w/o extensions) in C:\ drive.
Meerkat
Re: Help with batch script - FORFILES
Posted: 28 Jul 2015 10:43
by jdicerch
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\"
)
)
)
)