Computer Hope
Microsoft => Microsoft DOS => Topic started by: sp_key on August 21, 2009, 08:35:01 AM
-
Hi all,
I have a text file that contains a number of files and their location. (C:\MyFolder\myfile1.txt)
I need to create a batch file that reads the filenames and their location of the text file and copies the actual files to a destination folder.
So far I've found how to copy the entire folder but the problem is my list contains around 600 files while the entire folder contains thousands of files.
Any thoughts?
Many thanks in advance
-
Please state the format of the text file.
-
Please state the format of the text file.
Does a plain text (.txt ) file have more than the plain ascii format that opens with notepad?
-
i dont think you can do that in batch
without an external program
-
Does a plain text (.txt ) file have more than the plain ascii format that opens with notepad?
They mean the same thing.
By "format", I meant, does the file contain just path and filenames, like this:
C:\path to\folder\file1
C:\path to\folder\file2
C:\path to\folder\file3
C:\path to\folder\file4
etc ?
-
They mean the same thing.
By "format", I meant, does the file contain just path and filenames, like this:
C:\path to\folder\file1
C:\path to\folder\file2
C:\path to\folder\file3
C:\path to\folder\file4
etc ?
i think thats what the orig poster means
but i dont think you can read files in batch much less parse them
-
but i dont think you can read files in batch much less parse them
Where on earth did you get that idea?
-
batch is very unpowerful as a programming language
its good for starting to program
but not for complex programs and games
-
batch is very unpowerful as a programming language
its good for starting to program
but not for complex programs and games
smeezekitty, I respectfully suggest that you should think before you post... also you have not answered the specific question: Why did you write this:
but i dont think you can read files in batch much less parse them
-
The point I was trying to make is that in a help forum like this, a "good" post is one where you have
1. Read and understood the original question.
2. Know the answer.
A "bad" post is one where one or neither of these is true, for example where you just post of the top of your head stuff like "I don't think you can do xyz in batch", especially when you actually can do it very easily.
Batch language is very good at simple tasks like reading text files and parsing the contents. That is what it was designed for. It is not so good at detecting sprite collisions or doing 3-D graphics. We know this.
Here is how to read a text file and do something with each line it contains:
1. Let us assume that this is the text file contents:
C:\path to\folder\file1
C:\path to\folder\file2
C:\path to\folder\file3
C:\path to\folder\file4
You can see it is a list of paths and filenames, one to a line
2. Let us further assume that it is called My test file.txt, and it is in the folder C:\Myfolder.
3. Assume that each line in the file refers to an actual file and that we want to copy each one to another folder, called C:\destination folder.
4. Here is a batch file that will do that by reading each line of the file:
@echo off
set textfile=C:\MyFolder\My test file.txt
set destfolder=C:\destination folder
for /f "delims=" %%F in ( ' type "%textfile%" ' ) do (
echo Copying %%F
copy "%%F" "%destfolder%"
)
-
???
you can use a command in a control string of a for loop????
----edit----
it sure cant parse C and thats a text file
-
you can use a command in a control string of a for loop?
(deleted annoying multiple question marks.)
You can use the FOR /F command to parse the output of a command.
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
e.g.
FOR /F "delims=" %%A in ( ' dir /b ' ) do echo %%A
it sure cant parse C and thats a text file
You are getting two different uses of the word 'parse' confused.
cmd.exe is not a C interpreter. Nobody said it was.
Smeexekitty, you display an astonishing attitude:knowledge ratio which makes me wonder if you are aged about 11? Have you actually ever tried researching some of these things?
-
...if you are aged about 11?
Judging by a reaction to this, an estimation can be made on the age.
Back on topic:
Until we see a sample of the file we are using no one can really help you. Only a SAMPLE, as we don't need 600 lines, 6 should be fine :)
-
Hey guys, chill out it's good to debate :)
Salmon Trout, your code works fine! You can't even imagine how extremely useful this is to me. Greatly appreciated!
The reason I did not give you guys a sample of my list is because I decide how to do it. It doesn't really matter however this was exactly what I had in mind. A long list of filenames with their location.
One more question though I'm going to test this thoroughly. The files I'm copying are way larger than 2GB each, can you foresee any issues with that?
Also, could you please explain me the for /f "delims=" %%F line? It's one thing to do something and another to understand it :)
Many thanks again!
Sp.
-
Over 2gb? Depending on your computer's speed, could take a while.
The command FOR is used in many ways to parse things.
I'm tired and am not going to take hours explaining how FOR works, but another member may.
-
this should do it
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.
Finally, you can use the FOR /F command to parse the output of a
command. You do this by making the filenameset between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file. So the following
example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
would enumerate the environment variable names in the current
environment.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
-
Hey, many thanks for all this detail!
Sp.
-
its just going to the command prompt and typing "for /?" ;)
-
its just going to the command prompt and typing "for /?" ;)
It seems like magic to some people.
-
It seems like magic to some people.
But it is magic!
BTW, I know I'm pushing my luck here however, is it possible somehow to find out the total duration of the copying procedure? i.e. the Start and End time???
-
set time1=%time%
your process here
set time2=%time%
echo started %time1%
echo finished %time2%
-
It basically starts copying and once all files are copied cmd closes so can't really see whether it reports the time taken for the process.
Any thoughts?
Many thanks in advance!
Edit: Done it! Added a pause statement at the end of the script and I can now confirm it works fine! Many thanks again.