Page 1 of 2

search pos index and move file

Posted: 29 May 2012 03:32
by darioit
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

Re: search pos index and move file

Posted: 29 May 2012 04:09
by foxidrive
GnuSED will provide you the two items in each line

Code: Select all

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

Re: search pos index and move file

Posted: 29 May 2012 04:17
by darioit
Sorry, I can't use unix script like awk and sed

Re: search pos index and move file

Posted: 29 May 2012 08:39
by darioit
Hello,

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


Code: Select all

@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

Re: search pos index and move file

Posted: 29 May 2012 16:08
by Fawers
darioit wrote:Sorry, I can't use unix script like awk and sed

Are you writing this program for a college work?

Re: search pos index and move file

Posted: 30 May 2012 00:28
by darioit
no lol for a real BIG industry that you never image lol lol

Re: search pos index and move file

Posted: 30 May 2012 00:34
by foxidrive
Is this any use?

Code: Select all

@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

Re: search pos index and move file

Posted: 30 May 2012 00:50
by darioit
foxidrive wrote:Is this any use?

Code: Select all

@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

Re: search pos index and move file

Posted: 30 May 2012 02:06
by foxidrive
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?

Re: search pos index and move file

Posted: 30 May 2012 02:29
by darioit
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"

Re: search pos index and move file

Posted: 30 May 2012 02:58
by foxidrive
This might work a bit simpler.

Code: Select all

@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]

Re: search pos index and move file

Posted: 30 May 2012 04:05
by Aacini
It is very difficult to write a program trying to guess what its purpose is, but I try it. These are the specifications:

Code: Select all

- 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: Select all

@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: Select all

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

Re: search pos index and move file

Posted: 01 Jun 2012 13:48
by darioit
foxidrive wrote:This might work a bit simpler.

Code: Select all

@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: Select all

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: Select all

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: Select all

in (codici.txt)
instead

Code: Select all

('type "codici.txt"')
in the second for cycle

Re: search pos index and move file

Posted: 04 Jun 2012 01:25
by darioit
also changing the first raw

Code: Select all

for /f "delims=" %%a in [b]('type "index.txt"')[/b] do (

with this

Code: Select all

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

Re: search pos index and move file

Posted: 04 Jun 2012 02:27
by foxidrive
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.