DosTips.com

A Forum all about DOS Batch
It is currently 23 Jul 2016 03:10

All times are UTC-06:00




Post new topic  Reply to topic  [ 17 posts ]  Go to page 1 2 Next
Author Message
PostPosted: 29 May 2012 03:32 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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


Top
   
PostPosted: 29 May 2012 04:09 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5789
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


Top
   
PostPosted: 29 May 2012 04:17 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
Sorry, I can't use unix script like awk and sed


Top
   
PostPosted: 29 May 2012 08:39 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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


Top
   
PostPosted: 29 May 2012 16:08 
Offline

Joined: 08 Apr 2012 17:11
Posts: 187
darioit wrote:
Sorry, I can't use unix script like awk and sed

Are you writing this program for a college work?


Top
   
PostPosted: 30 May 2012 00:28 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
no lol for a real BIG industry that you never image lol lol


Top
   
PostPosted: 30 May 2012 00:34 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5789
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


Top
   
PostPosted: 30 May 2012 00:50 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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


Top
   
PostPosted: 30 May 2012 02:06 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5789
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?


Top
   
PostPosted: 30 May 2012 02:29 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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"


Top
   
PostPosted: 30 May 2012 02:58 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5789
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]


Top
   
PostPosted: 30 May 2012 04:05 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1161
Location: México City, México
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"
      )
   )
)


Top
   
PostPosted: 01 Jun 2012 13:48 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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


Top
   
PostPosted: 04 Jun 2012 01:25 
Offline

Joined: 02 Aug 2010 05:25
Posts: 184
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


Top
   
PostPosted: 04 Jun 2012 02:27 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5789
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 17 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Yahoo [Bot] and 7 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® Forum Software © phpBB Limited