View unanswered posts | View active topics It is currently 23 Apr 2014 02:54



Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
search pos index and move file 
Author Message

Joined: 02 Aug 2010 05:25
Posts: 151
Post search pos index and move file
hello,

I have this issue

filename:
C:\index.txt

contents:
abcdefgCODXXhijklmnopqrstuvwxyz2012abcde.PDF
abcdefgCODYYhijklmnopqrstuvwxyz2012abcde.PDF
....................

filename:
C:\2012abcde.PDF
C:\2012efhhi.PDF
.................

I like to search in index file position 8,5 code (ex. CODXX) and move file find in position 32,13 to another directory.

Best is using a table like this
table.txt:
CODXX
CODYY
........

call search_move.bat table.txt

Thanks in advance.

Regards
Dario


29 May 2012 03:32
Profile

Joined: 10 Feb 2012 02:20
Posts: 3672
Post Re: search pos index and move file
GnuSED will provide you the two items in each line

Code:
@echo off
for /f "tokens=1,*" %%a in ('
sed "s/^.......\(.....\)...................\(.............\).*/\1 \2/" "index.txt"
') do echo "%%a" "%%b"
pause


29 May 2012 04:09
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
Sorry, I can't use unix script like awk and sed


29 May 2012 04:17
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
Hello,

I wrote this code, works, but is not too elegant, smart, could you help me wrote it better?


Code:
@echo off
setlocal enabledelayedexpansion

FOR /F "delims=" %%a in (%1) do set "name=%%a" & FOR /F "delims=" %%b in (%~dp0codici.txt) do set "codice=%%b" & call :procName
GOTO:EOF

:procName
IF "%name:~40,5%"=="%codice%" (
SET filename2=%name:~117,30%
GOTO fine_ricerca
)
GOTO:EOF

:fine_ricerca
echo %count%--%codice%--!filename2!--

GOTO:EOF


29 May 2012 08:39
Profile

Joined: 08 Apr 2012 17:11
Posts: 187
Post Re: search pos index and move file
darioit wrote:
Sorry, I can't use unix script like awk and sed

Are you writing this program for a college work?


29 May 2012 16:08
Profile WWW

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
no lol for a real BIG industry that you never image lol lol


30 May 2012 00:28
Profile

Joined: 10 Feb 2012 02:20
Posts: 3672
Post Re: search pos index and move file
Is this any use?

Code:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "index.txt"') do (
set "a=%%a"
set "b=!a:~7,5!"
set "c=!a:~31,13!"
echo "!b!" "!c!"
)
pause


30 May 2012 00:34
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
foxidrive wrote:
Is this any use?

Code:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "index.txt"') do (
set "a=%%a"
set "b=!a:~7,5!"
set "c=!a:~31,13!"
echo "!b!" "!c!"
)
pause


really nice, its possible to match code from a list file code as codelist.txt
00001
00002

only for match this code, regards


30 May 2012 00:50
Profile

Joined: 10 Feb 2012 02:20
Posts: 3672
Post Re: search pos index and move file
I don't understand your reference to codelist.txt
Can you explain what you need to do with the two terms?

Evidently you have some PDF files and want to copy them somewhere else. Where do they need too be copied?


30 May 2012 02:06
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
I have thousand code in a index.txt and I like to extract only a few code as CODXX and CODZZ

abcdefgCODWWhijklmnopqrstuvwxyz2012abcde.PDF
abcdefgCODXXhijklmnopqrstuvwxyz2013abcde.PDF
abcdefgCODYYhijklmnopqrstuvwxyz2014abcde.PDF
abcdefgCODZZhijklmnopqrstuvwxyz2015abcde.PDF

I wrote this
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "index.txt"') do (
set "a=%%a"
set "b=!a:~40,5!"
set "c=!a:~117,30!"
rem echo "!b!" "!c!"
FOR /F "usebackq tokens=1,2,3 delims=;" %%e in (%~dp0codici.txt) do set "codice=%%e" & if "!codice!" EQU "!b!" echo "copy /y %%f\!c! %%g"
)

inside codici.txt
CODXX;E:\;"\\server\destination"
CODZZ;E:\;"\\server\destination"


30 May 2012 02:29
Profile

Joined: 10 Feb 2012 02:20
Posts: 3672
Post Re: search pos index and move file
This might work a bit simpler.

Code:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "index.txt"') do (
set "a=%%a"
set "b=!a:~40,5!"
set "c=!a:~117,30!"
rem echo "!b!" "!c!"
for /f "tokens=1,2,3 delims=;" in %%x ('type "codici.txt"') do if /i "!b!"=="%%x"  copy /b /y "%%y\!c!" "%%~z"
)


inside codici.txt
CODXX;E:\;"\\server\destination"
CODZZ;E:\;"\\server\destination"[/quote]


30 May 2012 02:58
Profile
Expert

Joined: 06 Dec 2011 22:15
Posts: 621
Location: México City, México
Post Re: search pos index and move file
It is very difficult to write a program trying to guess what its purpose is, but I try it. These are the specifications:
Code:
- There is an index.txt file with sourceCodice field at positions 7,5 and sourceFilename at positions 31,13:

0123456CODXX23456789 123456789 FILENAME.PDF

- There is a codici.txt file (in the same drive and folder of the Batch file) with fields targetCodice, targetDrive and targetDestination separated by semicolons:

CODXX;E:\;"\\server\destination"

- The program must read index.txt lines and find the corresponding codici.txt line that match sourceCodice and targetCodice. If found: copy targetDrive\sourceFilename targetDestination

The program below assume that both files are sorted by Codice fields; this is called file merge and it is very fast:
Code:
@echo off
setlocal EnableDelayedExpansion
set targetCodice=0
< "%~dp0codici.txt" (
for /F "delims=" %%a in (index.txt) do (
   set "sourceLine=%%a"
   set "sourceCodice=!sourceLine:~7,5!"
   set "sourceFilename=!sourceLine:~31,13!"
   call :getMatchingTargetRecord
   if !sourceCodice! equ !targetCodice! (
      copy /Y "!targetDrive!\!sourceFilename!" "!targetDestination!"
   )
))
goto :EOF

:getMatchingTargetRecord
if !targetCodice! geq !sourceCodice! exit /B
set /P targetLine=
for /F "tokens=1-3 delims=;" %%e in ("!targetLine!") do (
   set targetCodice=%%e
   set targetDrive=%%f
   set targetDestination=%%g
)
goto getMatchingTargetRecord

The Batch program below works with non-sorted files, but is much slower:
Code:
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (index.txt) do (
   set "sourceLine=%%a"
   set "sourceCodice=!sourceLine:~7,5!"
   set "sourceFilename=!sourceLine:~31,13!"
   for /F "tokens=1-3 delims=;" %%e in ('findstr /C:"!sourceCodice!" %~dp0codici.txt') do (
      if "!sourceCodice!" equ "%%e" (
         copy /Y "%%f\!sourceFilename!" "%%g"
      )
   )
)


30 May 2012 04:05
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
foxidrive wrote:
This might work a bit simpler.

Code:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('type "index.txt"') do (
set "a=%%a"
set "b=!a:~40,5!"
set "c=!a:~117,30!"
rem echo "!b!" "!c!"
for /f "tokens=1,2,3 delims=;" in %%x ('type "codici.txt"') do if /i "!b!"=="%%x"  copy /b /y "%%y\!c!" "%%~z"
)


inside codici.txt
CODXX;E:\;"\\server\destination"
CODZZ;E:\;"\\server\destination"
[/quote]

I correct this raw
Code:
for /f "tokens=1,2,3 delims=;" %%x in ('type "codici.txt"') do if /i "!b!"=="%%x" echo copy /b /y "%%y\!c!" "%%~z"


but......this script is extremely faster in only 1 second the batch is finished
Code:
FOR /F "tokens=1,2,3 delims=;" %%e in (codici.txt) do set "codice=%%e" & if "!codice!" EQU "!b!" echo "copy /y %%f\!c! %%g"


I think the reason is because I use
Code:
in (codici.txt)
instead
Code:
('type "codici.txt"')
in the second for cycle


01 Jun 2012 13:48
Profile

Joined: 02 Aug 2010 05:25
Posts: 151
Post Re: search pos index and move file
also changing the first raw
Code:
for /f "delims=" %%a in [b]('type "index.txt"')[/b] do (

with this
Code:
for /f "delims=" %%a in [b](index.txt)[/b] do (


is really impressive the performance with 100.000 record, only few second instead over 10 minutes


04 Jun 2012 01:25
Profile

Joined: 10 Feb 2012 02:20
Posts: 3672
Post Re: search pos index and move file
Yes, that is very useful information.

I knew of the huge delay that large input files will cause, but that is a way around them in many cases. Thanks.


04 Jun 2012 02:27
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.