Another Edit file FOR /F

Microsoft Windows server 2003 r2 standar...
September 4, 2009 at 04:16:45
Specs: Windows 2003 Server, 4gb
Hi I would like some help on a script i'm making for a bank routin. I need to check every file that arrives in a specific folder for the WORD "NY" and it will be at the start of the line. Like

NY00043284089040333
NY20000000000000300
and so on. I would like it to use a IF NOT. then move the file to a new folder. so that only the files containing NY will be proccessed.Oh and the other files can contain NY in the text somwhere. So we need to check that the NY is only in the beginning of the line. I trid different things from VB and batch. since my script is in batch (CMD) I would like to make that way. Her is somthing I trid. but I can't get it to do what I want:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* skip=2 delims=" %%M in ('dir /b/a-d JOB*') do (
find /V "NY"
)
::If %ERRORLEVEL% EQU 0 echo The string does not contain NY
::echo %%M
::IF %errorlevel%==1 ECHO Yes
::if %errorlevel%==0 ECHO No
IF NOT "%%M"=="NY" ECHO Move the file...


Please help a lost soul her :P


See More: Another Edit file FOR /F

Report •


#1
September 4, 2009 at 04:57:41
If all you are trying to do is move ANY file if ANY line starts with "NY" then findstr should fix you up. This should search the current directory for files "job*"


Give this a quick test and see if it's what your after:

for /f "delims=" %%M in ('findstr /b /l /m /v "NY" job*') do (
    echo Move file %%a
)


Report •

#2
September 4, 2009 at 05:50:42
Yes I did try this but the file also contains other lines..

And the /V will then list the files with NY in as well..

But I can bypass this by removing the Last line in the script first.. as I am going to anyway. Then the file will only contain lines starting with NY.

The script you gave wont output anything is it sopouse to be %%a or is it %%M it only sits ther and I have to to a ctrl c to stop it...

And by the way the /B /L /M needs to be case..


Report •

#3
September 4, 2009 at 05:55:10
Judago, why are you using the /V switch?

EDIT: Axachi, try Judago's version, but leave off the /V switch

Axachi is it sopouse to be %%a or is it %%M
It should be whatever you specified at the start of the loop. In this case, it's %%M


Report •

Related Solutions

#4
September 4, 2009 at 06:04:57
Judago, why are you using the /V switch?

Because I dropped the ball.....

First I was listing all of the files that contain it and forgot the simple fact that then it will list just about every thing and changed it. That's what I get for answering a question after debuging my buggy c++ code for an hour.

And by the way the /B /L /M needs to be case..

It doesn't seem to on xp but perhaps 2003 has a different build of findstr.

I must learn to think a little longer, on that note I will stop being annoying until I come back with something that works.....


Report •

#5
September 4, 2009 at 06:08:12
Ok my bad it runs fine now her is the script.
@echo off
setLocal EnableDelayedExpansion
set SRC=D:\Batch scripts\
for /f "delims=" %%M in ('findstr /B /L /M "NY" "%SRC%JOB*"') do (
ECHO move file %%M
if %errorlevel%==0 echo yes
if %errorlevel%==1 echo no
)

But still this is not the right way. it now list the files that have "NY" in them and I only want to move the files that DONT. if I remove the last line in the file . and I can't use if errorlevel. it only says no for the lot of the files and not for eatch of them. if that had been the case I could have used it.


Report •

#6
September 4, 2009 at 06:16:50
No where near as efficient but I *think* it should work.

for /f "delims=" %%M in ('dir /b/a-d job*') do (
    findstr /B /L "NY" "%%M"||echo move "%%M" 
)

As for the errorlevel that's an issue of expansion, variables marked with % expand before each line and code blocks (inside parentheses) are expanded as one line....


Report •

#7
September 4, 2009 at 06:28:49
As for the errorlevel that's an issue of expansion, variables marked with % expand before each line and code blocks (inside parentheses) are expanded as one line....

I did not know thanks I will try the code now.


Report •

#8
September 4, 2009 at 06:33:36
Still this only gives me the files I am not going to move..

Well I guess I can do a xcopy exept %%M ? or what.

as I said on the top can I do a IF NOT the "FOR /F script" then move the file...

I want to check IF file contains NY then do nothing ELSE move..


Report •

#9
September 4, 2009 at 06:35:03
Didn't think long enough ;)

for /f "delims=" %%M in ('dir /b/a-d job*') do (
    findstr /B /L "NY" "%%M"&&echo move "%%M" 
)


Geeze this is getting old fast....`

Report •

#10
September 4, 2009 at 07:01:27
LOL yes becuse I get the code it works the first one. but it is the wrong file.

Look

I Have let's say 2 files called JOB123 and JOB321 now the 1'st file contains alot of this lines
NY00043284089040333
NY20000000000000300
"" END OF JES FILE""
and the secon file contains somthing else like

AH201TBRI0829000001 04BETFOR0000950421363TIA 0315
0829 VERSJON002 00000000000000000000000000

Now waht the script now dos is find that the first file contains NY at the first line and says I move that file.

But I want to move the secon file :)

So can I use IF statment... ? IF not for /f in ('dir /b job*') do (
findstr /B /L "NY" "%%M"&&set FN=""
IF "%FN%"==%%M (
move file to newloc
))


Report •

#11
September 4, 2009 at 07:02:56
I'm really sorry, I think I didn't understand the actual issue properly.

Report •

#12
September 4, 2009 at 07:05:47
That's ok :) Do you get it now?

Report •

#13
September 4, 2009 at 07:12:40
Axach: But I want to move the secon file
Perhaps I'm missing something here, but what's wrong with the script in Response 6? It appears to "move" the file if it doesn't contain "NY" at the start of a line. Should it check only the first line of the file?

Report •

#14
September 4, 2009 at 07:17:23
no :)

Is it as simple as moving every file that doesn't have NY at the start of any line?

Or is it only file that don't have NY at the start of the very first line of the file.


You could certainly use an if statement, but not in the way posted above, it will only compare strings or numbers, not the output of a command.


Report •

#15
September 4, 2009 at 07:24:20
Razor2.3 Nothing wrong with the script. it only moves the files containing NY @ the first line. I want it to move all files that don't..
Because I need to process the JOB file containing NY and then I send that file to a database server witch then imports it into a DB..

But the other files not containing NY lines needs to go to a Backup folder.
Maby I did not explain myself very good in the begining.....

Judago: Is it as simple as moving every file that doesn't have NY at the start of any line? Yes


I did say this in my original post..and so on. I would like it to use a IF NOT. then move the file to a new folder. so that only the files containing NY will be proccessed. But i may have over explaind it :P



Report •

#16
September 4, 2009 at 07:25:58
Ok now I think I know what the problem was ;), you'll laugh when you realize.

for /f "delims=" %%M in ('dir /b/a-d job*') do (
    >nul findstr /B /L "NY" "%%M"||echo move "%%M" 
)


Report •

#17
September 4, 2009 at 07:28:03
He he that easy :P Thanks a bunch Judago. now I messed up your C++ thinking .

This is perfect.
i'm such a >nul


Report •


Ask Question