Computing.Net > Forums > Programming > Another Edit file FOR /F

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Another Edit file FOR /F

Reply to Message Icon

Name: Axachi
Date: September 4, 2009 at 04:16:45 Pacific
OS: Windows 2003 Server
CPU/Ram: 4gb
Product: Microsoft Windows server 2003 r2 standard edition w/sp2
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: September 4, 2009 at 04:57:41 Pacific
Reply:

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
)


0

Response Number 2
Name: Axachi
Date: September 4, 2009 at 05:50:42 Pacific
Reply:

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


0

Response Number 3
Name: Razor2.3
Date: September 4, 2009 at 05:55:10 Pacific
Reply:

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


1

Response Number 4
Name: Judago
Date: September 4, 2009 at 06:04:57 Pacific
Reply:

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


0

Response Number 5
Name: Axachi
Date: September 4, 2009 at 06:08:12 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: September 4, 2009 at 06:16:50 Pacific
Reply:

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


0

Response Number 7
Name: Axachi
Date: September 4, 2009 at 06:28:49 Pacific
Reply:

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.


0

Response Number 8
Name: Axachi
Date: September 4, 2009 at 06:33:36 Pacific
Reply:

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


0

Response Number 9
Name: Judago
Date: September 4, 2009 at 06:35:03 Pacific
Reply:

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

0

Response Number 10
Name: Axachi
Date: September 4, 2009 at 07:01:27 Pacific
Reply:

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


0

Response Number 11
Name: Judago
Date: September 4, 2009 at 07:02:56 Pacific
Reply:

I'm really sorry, I think I didn't understand the actual issue properly.


0

Response Number 12
Name: Axachi
Date: September 4, 2009 at 07:05:47 Pacific
Reply:

That's ok :) Do you get it now?


0

Response Number 13
Name: Razor2.3
Date: September 4, 2009 at 07:12:40 Pacific
Reply:

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?


0

Response Number 14
Name: Judago
Date: September 4, 2009 at 07:17:23 Pacific
Reply:

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.


0

Response Number 15
Name: Axachi
Date: September 4, 2009 at 07:24:20 Pacific
Reply:

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



0

Response Number 16
Name: Judago
Date: September 4, 2009 at 07:25:58 Pacific
Reply:

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


1

Response Number 17
Name: Axachi
Date: September 4, 2009 at 07:28:03 Pacific
Reply:

He he that easy :P Thanks a bunch Judago. now I messed up your C++ thinking .

This is perfect.
i'm such a >nul


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Another Edit file FOR /F

Batch file for text replacement www.computing.net/answers/programming/batch-file-for-text-replacement/12883.html

Batch file for renaming folders www.computing.net/answers/programming/batch-file-for-renaming-folders/15430.html

batch file: error SETting in FOR /f www.computing.net/answers/programming/batch-file-error-setting-in-for-f/16520.html