search pos index and move file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

search pos index and move file

#1 Post by darioit » 29 May 2012 03:32

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search pos index and move file

#2 Post by foxidrive » 29 May 2012 04:09

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#3 Post by darioit » 29 May 2012 04:17

Sorry, I can't use unix script like awk and sed

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#4 Post by darioit » 29 May 2012 08:39

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

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: search pos index and move file

#5 Post by Fawers » 29 May 2012 16:08

darioit wrote:Sorry, I can't use unix script like awk and sed

Are you writing this program for a college work?

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#6 Post by darioit » 30 May 2012 00:28

no lol for a real BIG industry that you never image lol lol

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search pos index and move file

#7 Post by foxidrive » 30 May 2012 00:34

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#8 Post by darioit » 30 May 2012 00:50

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search pos index and move file

#9 Post by foxidrive » 30 May 2012 02:06

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?

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#10 Post by darioit » 30 May 2012 02:29

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"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search pos index and move file

#11 Post by foxidrive » 30 May 2012 02:58

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]

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search pos index and move file

#12 Post by Aacini » 30 May 2012 04:05

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#13 Post by darioit » 01 Jun 2012 13:48

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: search pos index and move file

#14 Post by darioit » 04 Jun 2012 01:25

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search pos index and move file

#15 Post by foxidrive » 04 Jun 2012 02:27

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.

Post Reply