[Alg] .txt regel voor regel door batchfile halen *

Pagina: 1
Acties:

  • CmdrKeen
  • Registratie: Augustus 2000
  • Laatst online: 27-05 21:11

CmdrKeen

Krentenboltosti

Topicstarter
Kan ik vanaf de command line (wxp) een .txt-bestand regel voor regel door een batchfile laten behandelen?

Ik heb een lijst van bestanden die ik wil converteren. De lijst (tiflist.txt) ziet er zo uit:
code:
1
2
c:\mijn documenten\blaat.tif
d:\blaat\dir\eerste map\bla.tif

De batchfile (tif2jpg.bat) gaat met IrfanView de TIF-bestanden omzetten naar JPG:
code:
1
i_view32 "%~1" /convert="%~1.jpg"

Ik heb dit geprobeerd:
code:
1
tif2jpg<tiflist.txt

De batchfile leest echter niets, of in ieder geval niet regel voor regel uit de tekstfile.

Hoe kan ik de batchfile regel voor regel laten lezen en al die regels apart laten behandelen?

(Ja: gezocht op Google, TechNet en GoT..)

[ Voor 3% gewijzigd door CmdrKeen op 20-06-2003 15:51 ]

Bloed, zweet & koffie


  • Schmoove
  • Registratie: Juli 2001
  • Laatst online: 18:24
Misschien stomme vraag, maar kun je niet gewoon batch conversie gebruiken in IrfanView dan?

  • RAMeijer
  • Registratie: Februari 2000
  • Laatst online: 10-02 13:39
for /?:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    or, if usebackq option present:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

    filenameset is one or more file names.  Each file is opened, read
    and processed before going on to the next file in filenameset.
    Processing consists of reading in the file, breaking it up into
    individual lines of text and then parsing each line into zero or
    more tokens.  The body of the for loop is then called with the
    variable value(s) set to the found token string(s).  By default, /F
    passes the first blank separated token from each line of each file.
    Blank lines are skipped.  You can override the default parsing
    behavior by specifying the optional "options" parameter.  This
    is a quoted string which contains one or more keywords to specify
    different parsing options.  The keywords are:

        eol=c           - specifies an end of line comment character
                          (just one)
        skip=n          - specifies the number of lines to skip at the
                          beginning of the file.
        delims=xxx      - specifies a delimiter set.  This replaces the
                          default delimiter set of space and tab.
        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
        usebackq        - specifies that the new semantics are in force,
                          where a back quoted string is executed as a
                          command and a single quoted string is a
                          literal string command and allows the use of
                          double quotes to quote file names in
                          filenameset.

    Some examples might help:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

    would parse each line in myfile.txt, ignoring lines that begin with
    a semicolon, passing the 2nd and 3rd token from each line to the for
    body, with tokens delimited by commas and/or spaces.  Notice the for
    body statements reference %i to get the 2nd token, %j to get the
    3rd token, and %k to get all remaining tokens after the 3rd.  For
    file names that contain spaces, you need to quote the filenames with
    double quotes.  In order to use double quotes in this manner, you also
    need to use the usebackq option, otherwise the double quotes will be
    interpreted as defining a literal string to parse.

    %i is explicitly declared in the for statement and the %j and %k
    are implicitly declared via the tokens= option.  You can specify up
    to 26 tokens via the tokens= line, provided it does not cause an
    attempt to declare a variable higher than the letter 'z' or 'Z'.
    Remember, FOR variables are single-letter, case sensitive, global,
    and you can't have more than 52 total active at any one time.

    You can also use the FOR /F parsing logic on an immediate string, by
    making the filenameset between the parenthesis a quoted string,
    using single quote characters.  It will be treated as a single line
    of input from a file and parsed.


etc... >:)

  • CmdrKeen
  • Registratie: Augustus 2000
  • Laatst online: 27-05 21:11

CmdrKeen

Krentenboltosti

Topicstarter
Tnx for the for :)

Het gaat erom dat ik niet weet waar de bestanden staan en dat de doelbestanden op dezelfde plek als de bronbestanden komen. Dat kan niet met de grafische interface (ook niet met de command line interface).

Bloed, zweet & koffie


  • SilentStorm
  • Registratie: November 2000
  • Laatst online: 18-03 14:45

SilentStorm

z.o.z

Als je in je batchfile dingen wilt opvangen, moet je dat doen met een procentteken (%) met een nummer (%1,%2,%3) voor elke variabele (bestandsnaam). Omdat dit systeem maximaal 9 nummers kent, zul je in de batchfile gebruik moeten maken van het 'shift' commando. Deze verplaatst de variabele in %2 naar %1, 3 naar 2, etc. Omdat de variabelen in een regel met de opdracht moeten worden meegegeven is het best inefficient om iets te bouwen als wat jij beschrijft. Het moet ook extern gebeuren, een batchfile kan geen bestanden openen. Als je gebruik maakt van wat ik hierboven beschreeft kun je het vrij simpel oplossen met zoiets:
code:
1
2
3
4
:blaat
i_view32 %1 /convert=%1.jpg
shift
goto blaat

Als je nu vanuit je explorer de bestanden aanklikt die je wilt converten en deze op je batchfile dropt, moeten ze worden geconverteerd. Windows geeft deze namelijk gewoon als "bestand1.tif" "bestand2.tif" "bestand3.tif" aan je batchfile mee. Er staat me nog bij dat er ergens een maximum was aan het aantal karakters dat je hiervoor kan gebruiken in 16bits modus (voor batch). Ik denk echter dat je er hiermee zelf wel uitkomt. Succes!

edit:
ik ben helemaal nog voorbij gegaan aan de mogelijkheden van for. Hier kun je idd een hoop mee uitvoeren wat bij de klassieke (prewindows / win9x) batchfiles niet mogelijk was.

[ Voor 10% gewijzigd door SilentStorm op 20-06-2003 16:07 ]

Localhost is where the heart is


  • F_J_K
  • Registratie: Juni 2001
  • Niet online

F_J_K

Moderator CSA/PB/AI

Front verplichte underscores

^^^ FOR ja. Met for /? moet het idd goed lukken. De rest van de info die je zo krijgt verteld bijv. hoe je enkel de dir kan achterhalen. Maar met SHIFT kom je er idd ook leuk :)

Deze en meer tips staan trouwens ook achter de links in de SA FAQ: DOS commando's. De Windows Operated Systems - Policy was trouwens ook handig om even te lezen: ik pas je topictitel even aan ;)

Edit:
Vilenin schreef op 20 June 2003 @ 16:30:
Tenks voor de topictitlechange, maar was niet nodig: ik test het op XP, maar converteer het naar Win2000 en uiteindelijk moet het gaan werken op een NT4-machine. De policies ken ik wel hoor ;)
O-)

Er zijn trouwens IIRC kleine verschillen tussen de mogelijkheden tussen XP en NT4 batchfiles.

[ Voor 35% gewijzigd door F_J_K op 20-06-2003 16:47 ]

'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind' (Terry Pratchett, Eric)


  • CmdrKeen
  • Registratie: Augustus 2000
  • Laatst online: 27-05 21:11

CmdrKeen

Krentenboltosti

Topicstarter
Tenks voor de topictitlechange, maar was niet nodig: ik test het op XP, maar converteer het naar Win2000 en uiteindelijk moet het gaan werken op een NT4-machine. De policies ken ik wel hoor ;)

Even de situatie, want volgens mij is er wat onduidelijkheid:
Op een directory op een fileserver staan veel gescande foto's e.d. (.TIF) van gebruikers. Elke dag komen er nieuwe files bij, en de gebruikers vergeten aan de lopende band om hun originelen te verwijderen. De tif-bestanden worden nl. omgezet naar jpg en gebruikt op sites, prints, enz.. Heeft men de originelen nodig (bv. voor drukwerk), dan worden deze elders opgeslagen.

Ik wil de .tif-bestanden elke nacht automagisch converteren naar .jpg en de originelen deleten/ergens anders bewaren. De jpg's moeten in plaats van de tif's komen, dus op dezelfde locatie met +/- dezelfde naam.

[ Voor 4% gewijzigd door CmdrKeen op 20-06-2003 16:30 ]

Bloed, zweet & koffie


  • CmdrKeen
  • Registratie: Augustus 2000
  • Laatst online: 27-05 21:11

CmdrKeen

Krentenboltosti

Topicstarter
Voor de mensen die met de search op dit topic komen is hier het eindresultaat:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@echo off

Rem Converts .TIF to .JPG using Irfan View.
Rem Usage: tif2jpg "c:\Source Dir\My Pictures\Tiff dir" "c:\Backup of My Tiffs" > "C:\My Logfiles\Tif2Jpg.log"
Rem Ifran View: http://www.irfanview.com/
Rem spam ~ at ~ huijgen.com

Rem Search for .TIFF files
dir "%~1\*.tif" /b /s /a:-h > tiflist.txt

Rem Convert the .TIFF files to .JPG using Irfan View
for /F "tokens=*" %%I in (tiflist.txt) do i_view32 "%%I" /convert="%%~dI%%~pI%%~nI.jpg"

Rem Move original .TIFF files to backup directory
if exist "%~2" rmdir /s /q "%~2"
md "%~2"
xcopy "%~1\*.tif" "%~2" /i /e
del "%~1\*.tif" /s /q

Rem Copy result to screen using old format for log filing
tree "%~1" /f /a
tree "%~2" /f /a


Of, als je minimalistisch bent aangelegd:
code:
1
2
3
4
5
6
7
8
9
@echo off
dir "%~1\*.tif" /b /s /a:-h > tiflist.txt
for /F "tokens=*" %%I in (tiflist.txt) do i_view32 "%%I" /convert="%%~dI%%~pI%%~nI.jpg"
if exist "%~2" rmdir /s /q "%~2"
md "%~2"
xcopy "%~1\*.tif" "%~2" /i /e
del "%~1\*.tif" /s /q
tree "%~1" /f /a
tree "%~2" /f /a


Disclaimers: gebruik op eigen risico. Getest op Windows XP Pro.

Dit batchfiletje gebruikt Irfan View om .TIFF-bestanden te converteren naar .JPG-bestanden. Het eerste argument is de sourcedir. Het script zoekt naar .TIFF-bestanden in de sourcedir en alle subdirs daarin. Het converteert alle .TIFF-bestanden die het tegenkomt en hernoemt ze ook meteen naar .JPG. De originelen worden naar het pad dat het tweede argument is verplaatst voor backupdoeleinden.

Ik hoop dat ik er iemand tijd mee bespaar. Bedankt voor jullie hulp :)

[ Voor 12% gewijzigd door CmdrKeen op 23-06-2003 12:29 ]

Bloed, zweet & koffie

Pagina: 1