batch/vb to search for a char in spec column

October 14, 2010 at 09:00:20
Specs: Windows XP
I need to make sure that a slash / appears in the 23rd column of a text file. If it doesn't, I need to move the file to another folder. The file names will constantly change. I found the following code for a batch file, and have been trying to modify it, but no success so far.
---------------------------------------------------------------------------
F:
cd F:\Staging
if "%1"=="GoTo" goto %2
%comspec% /V:ON /C %0 GoTo start
goto:eof
:start

set LINE=0

for /F "delims=" %%L in ('dir /b /d') do (
set /A LINE += 1
if "!LINE!" == "1" (
set STR=%%L
echo.!STR:~22!> %TEMP%.\T1.DAT
FINDSTR /I /B /C:"/" %TEMP%.\T1.DAT > nul
del %TEMP%.\T1.DAT
if not errorlevel=1 (goto here) else (move *.* F:\fake)
goto forend
)
)
:forend
set LINE=
set STR=
echo __Bad File__ > F:\Reports\bad_file.txt
F:
cd F:\fake
dir/b >> F:\Reports\bad_file.txt
del *.*
:here
:eof
---------------------------------------------------
I have no problem scrapping this and going with other batch code, or VB.
The reason I'm looking for a slash is that there are 2 possible types of information in the files and the one consistent thing I could find is that a date appears in one of them with slashes as delimiters.
The first and last fields are too random to use Qgrep on (though I'm using it in another part to find files that have blanks at the beginning of the line).


See More: batch/vb to search for a char in spec column

Report •


#1
October 14, 2010 at 09:02:08
P.S. here's where the original script came from:
http://www.computing.net/answers/do...

Report •

#2
October 14, 2010 at 14:50:00
This will pluck the char at col 23. We can muddle through the rest.

===============================================
@echo off & setLocal EnableDELAYedeXpansion

set L=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a L+=1
set S=%%a
set C=!S:~22,1!
if !C! equ / echo.char at col 23 in line !L! is !C!
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
October 20, 2010 at 11:12:05
I've tried using the code as is in a batch file, and tried making some different adjustments, but so far, it doesn't seem to really do anything. I tried adding a pause at the end to read what it outputs, and redirecting the echo to a text file. By changing (myfile) to ('dir/b/d') I can get it to try each file in the directory (it shows each file name being placed in the string on screen).

Report •

Related Solutions

#4
October 20, 2010 at 11:31:39
ok, update - got it to work for a single file, but only if the exact name is in place of myfile; however, I need it to work for any text file I place in that folder.

Report •

#5
October 20, 2010 at 11:35:02
I can't think why it doesn't work.

Does myfile HAVE a / in col 23?

What are you hoping to do with dir/b/d?


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
October 20, 2010 at 11:37:56
"but only if the exact name is in place of myfile"

surprise!


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
October 20, 2010 at 11:39:10
the files do have a / in col 23, but it acts like they don't. If I put myfile and have a file called myfile, it works; however, if I use the dir/b/d it checks the file NAME for a / in column 23 instead of the text inside, but it will do so for all the files in the folder. The problem is, I need the text inside of say, 4 different files to be checked for the / and their names will change each time.

Report •

#8
October 20, 2010 at 11:49:44
You still haven't said what /d is for.

But what you need is a FOR loop wrapped around the one which checks inside the file.

There are plenty of examples here.


=====================================
Life is too important to be taken seriously.

M2


Report •

#9
October 20, 2010 at 12:16:55
Ok, this is what I'm up to so far:

setLocal EnableDELAYedeXpansion

set L=1
for /F "tokens=* delims= " %%g in ('dir/b') do (
for /F "tokens=* delims= " %%a in (%%g) do (
set S=%%a
set C=!S:~22,1!
if !C! equ / move %%g c:\test
))

I'm only checking the first line, as if it is ok, the rest should be. I set up 3 fake files with only one have a / in the 23rd column, and that is the only file that is moved (which is what I'm looking for). If i tried echoing the line to a text file I got more lines than I had files?
Even though it is working, I'm trying to learn at the same time. Does this seem like what you were aiming me towards? or Should I do something different. Is the !C! a name for the columns? Or does this batch file set it to the columns?


Report •

#10
October 20, 2010 at 13:34:40
@echo off & setLocal EnableDELAYedeXpansion

for /F "tokens=* delims= " %%g in ('dir/b/a-d') do (
set N=
for /F "tokens=* delims= " %%a in (%%g) do (
set /a N+=1
  if !N! equ 1 (
    set S=%%a
    set C=!S:~22,1!
    if !C! equ / move %%g c:\test
  )
)
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#11
October 21, 2010 at 13:04:43
Thanks for the help. The script is "working" now. (wanted to make sure I said that). I named the batch file Mechanix.bat :D

I was wondering though; while it is only comparing on the first line, it still seems to be reading the rest of the text file (which really increases the run time). Is it possible to get it to only read the first line of the file, or is this as good as it gets?


Report •

#12
October 22, 2010 at 02:09:48
Good point. I'll hammer it out when I've had some coffee.


=====================================
Life is too important to be taken seriously.

M2


Report •

#13
November 2, 2010 at 08:42:16
Would checking the last line of the file be just as good as checking the first line?

This uses tail.exe, which is in the Windows Resource Kit (freely downloadable from MS)


@echo off

setlocal ENABLEDELAYEDEXPANSION

for %%A in (*.*) do (
for /f "delims=" %%B in ('tail -1 %%A') do (
set CHAR=%%B
set CHAR=!CHAR:~22,1!
if "!CHAR!" == "/" move %%A c:\scripts\movefiles\movedfiles
)
)


Report •

#14
November 3, 2010 at 13:15:50
Not in this case, although I may find a use for it later :D One of the issues the slash will (hopefully) check for is the presence of a Header Line (otherwise, checking the last line would work for the other issues). I do have the command (I previously got the Resource kit in order to use Qgrep).

Report •

#15
November 10, 2010 at 16:50:05
I got sidetracked.

============================================
@echo off & setLocal EnableDELAYedeXpansion

for /F "tokens=* delims= " %%g in ('dir/b/a-d') do (
call :sub1 %%g
)
goto :eof

:sub1
for /F "tokens=* delims= " %%a in (%1) do (
set S=%%a
set C=!S:~22,1!
if !C! equ / ( echo move %%g c:\test )
goto :done
)
:done
goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#16
December 6, 2010 at 05:53:01
Great, got it working in the rest of the script (definite improvement in the time it takes to run through files).
Thanks.
Now I'll see if I can understand it too :D

Report •


Ask Question