views:

127

answers:

1

So I have alot of .txt files. I need for it to move txt files to a certain folder if they have more than 1 line. That is,

Text text text
Blah blah blah

But leave it alone if it's just

Text text text

Any suggestions on how to do this? This is on Windows XP if that helps. Thanks!

EDIT: Thanks John for your efforts. Figured out a way to do it in PowerShell

# Specify folder name that will contain the filtered .txt files
$goodFolder = "Filtered"

get-childitem *.txt | foreach ($_) {
$a = get-Content $_.fullname | Measure-Object

# If more than one line is found in text file, move it. Otherwise, leave it alone
If ($a.Count -gt 1) {   # If there is more than one line found in txt file
    Move-Item $_.fullname $goodFolder       # Then move it to a folder with text files with more than 1 line
}
}
A: 

Note: the original question was how do this in a .bat file.

This answer is a solution to the original question. It is not a powershell solution.

save this as clines.bat

@echo off
setlocal enabledelayedexpansion
set clines = 0
for /F %%J in (%1) do set /A clines = !clines! + 1
if %clines% LEQ 1 goto finis

rem @echo %1 has %clines% lines
move %1 %2

:finis
endlocal

and then call it in another batch file that iterates through the files

@echo off
for %%I in (*.txt) do call clines.bat %%I newfolder
John Knoeller
Awesome, works perfectly! Thanks a ton, however you might want to edit your code and change "mov" to "move" :) Thanks again!
Axsuul
Well at least for me changing "mov" to "move" worked for me. Dunno if this is because I'm on WinXP?
Axsuul
Hmm actually, I'm getting some strange results. Like sometimes there are files that still remain with more than 1 line of text. I know that's kind of vague... Any ideas?
Axsuul
no, move is correct, I had that line commented out when I tested the script.
John Knoeller
I think the for /F loop may only count non-blank lines, could that be it?
John Knoeller
It could also be that the files need CR LF and not just LF as line terminators. I don't really know.
John Knoeller
I was running it on about 500 text files, it did move alot of text files with more than 1 line, but there were still text files remaining that also had more than 1 line of text (non-blank). Is that what you were asking?
Axsuul
How would I add the CR LF line terminator?
Axsuul
I'm saying a file with 1 line of text and 1 blank line might not move. a file that had LF's as line separators might not be seen by the for loop as having on a single (long) line. CR LF is the standard for windows. If you open the file in notepad, does it look like 1 long line?
John Knoeller
I don't think it's moving a file with 1 line of text and 1 blank line. But it's sometimes not moving a file with more than 1 line of text (and it shows in notepad, like 5 lines of text)
Axsuul
post the text and Ill try it on my machine.
John Knoeller
John, thanks for the help and your time. No need anymore though, I figured out how to write it in PowerShell. I posted the original code in my question if anyone needs it.
Axsuul
using powershell is cheating ;). Seriously, if plan to allow other tools you really should say so in your question.
John Knoeller