开发者

How to create a loop/skip in batch

I am trying to download a chunk of files from an application. The shell comman开发者_如何学God for it is 'go filename download'.

I have a text file containing all the filenames I have to download. All I want to do is to run a script/command such that when the above command is executed 1. the filenames are picked up from the textfile & executed using the above command 2. existing files/unavailable files are skipped 3. the process then continues with the next files in the list

So far I have this idea of using an operator like go $ download & then feed the operator with the text file containing the filenames list. Thanks in advance.


For Windows, you can use for /f to process the file and create a command from it. The following script supergo.cmd shows how this can be done:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "delims=" %%f in (list.txt) do (
    echo go "%%f" download
)
endlocal

The following transcripts shows it in operation:

C:\Pax> type list.txt
file1.txt
file number 2.txt
another file.jpg

C:\Pax> supergo
go "file1.txt" download
go "file number 2.txt" download
go "another file.jpg" download

If you're using a shell like bash, you can use sed to create a temporary script from the input file then run that:

#!/bin/bash
sed -e "s/^/echo go '/" -e "s/$/' download/" list.txt >/tmp/tempexec.$$
chmod u+x /tmp/tempexec.$$
. /tmp/tempexec.$$
rm -rf /tmp/tempexec.$$

This puts an echo go ' at the start of each line, a ' download at the end, then marks it executable and executes it.

In both cases (Windows and bash), remove the echo to get it to do the real work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜