English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am creating a batch file menu and one option is to copy all text files from the current directoy to another directory called txt files

2006-08-16 13:36:22 · 7 answers · asked by robertc_tas 2 in Computers & Internet Programming & Design

7 answers

I have XP and there are a few differences in the commands depending on the operating systems. This answer will be close for UNIX also but the command is 'cp' instead of 'copy' and for help you need to find the 'man' (short for maual) page such as 'cp man'.

In DOS;

"copy *.txt c:\...\..\txt files"
[The dots are just to represent a 'path', replace them with the true path or variables.]

It is a possibility you might need quotes around the line if any spaces exist as 'txt files'.

This copies all files from the 'working directory' (last direct the OS was operating in) to the path that ends with a directory called 'txt files'. You can also use

"copy c:\...\...\*.txt c:\...\txt files"

if the source directory is always the same. Next time put more specifics in the question, especially OS, to get a definitive answer. Since DOS was basically copied from UNIX and many OSs did the same, this should help you along.

The 'xcopy. command might be usefull, look at the help file for the switches. In BOTH is a '/v' switch to verify that the copies are identicle. I would try to use it.

If you need to know the present working directory in UNIX, the 'pwd' command will give it to you.

If you need something similar in DOS, you could 'cd' (change directory) in the script or use the script to make variables with the 'set' command for later recall. The 'set' command is very powerful. The help print out is after 'copy'.

Writing to a temporay text file for variables then deleting it and writing to a log will help also. the log will especially help with troubleshooting.

Hope this helps or write to me,

Levi Patrick II
www.TheITDoctor.com

Here is the help from XP's 'cmd prompt' environment.

----------------------------------------------------
C:\Documents and Settings\LP>copy /?
Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]

source Specifies the file or files to be copied.
/A Indicates an ASCII text file.
/B Indicates a binary file.
/D Allow the destination file to be created decrypted
destination Specifies the directory and/or filename for the new file(s).
/V Verifies that new files are written correctly.
/N Uses short filename, if available, when copying a file with a
non-8dot3 name.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.

To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).
------------------------------------------------------------------
SET

C:\Documents and Settings\LP>set /?
Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

SET command invoked with just a variable name, no equal sign or value
will display the value of all variables whose prefix matches the name
given to the SET command. For example:

SET P

would display all variables that begin with the letter 'P'

SET command will set the ERRORLEVEL to 1 if the variable name is not
found in the current environment.

SET command will not allow an equal sign to be part of the name of
a variable.

Two new switches have been added to the SET command:

SET /A expression
SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes. Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them. If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used. This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values. If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression. The
assignment operator requires an environment variable name to the left of
the assignment operator. Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

The /P switch allows you to set the value of a variable to a line of input
entered by the user. Displays the specified promptString before reading
the line of input. The promptString can be empty.

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2". "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output. "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:

set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:

set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET. These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions
version number.

%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor.
---------------------------------------------------------------

2006-08-16 14:14:02 · answer #1 · answered by Levi Patrick 2 · 0 0

Okay, I understand the question and have had to do similar things. However, I'm not aware of any program that can do this. Since I'm a programmer I would just write something, I could probably whack it out in an hour or two. Barring that, or if I didn't want to take the hour, what I would do is this: In a My Computer/Windows Explorer window, navigate to the appropriate folder. Right click an empty spot (which can be as simple as between icons, or away from the filename in Details view, which is the view I recommend for such a high number of files), click New, and click Text Document. You'll get a a new, empty text document named "New Text Document.txt" and you'll be in naming mode to type the filename. Since this is hardly better, I would just press escape. THEN, with the right mouse button I would drag it over (to the same folder, so a fraction of an inch is plenty) to get a menu, and choose Copy Here. You'll get another empty text file named "New Text Document (1).txt". Then select both - Click one and Shift-Click the other - and repeat the Right-Drag method. Now you have 4 empty text files. Click the first, Shift-Click the last and repeat the Right-Drag. Now you have 8 files. etc. etc. etc. It'll take a while, but you'll have your 900 empty text files. If you have the Status bar turned on in My Computer/Windows Explorer, highlighting them all will tell you how many you have, down in the status bar. NOW. Select the first movie file and press F2. This will get you editing the file name. Highlight the whole filename and press Ctrl-Insert (the Windows version of Copy, works everywhere in Windows, as opposed to Ctrl-C). Press Escape. Then press F2 on one of the Text files. Highlight the whole filename and press Shift-Insert (Windows version of Paste, like Ctrl-V). Press Enter. Now you have an empty text file named after that movie file. If you want to use a standard format in the text file, I would recommend adding it to the first empty text file first, before all the copying. Say for example: Client's Name: Phone Number: Address Products Used: Project Staff: That way THIS will save you a load of time with this process, since it will be in every "blank" text file before you fill it out.

2016-03-27 05:02:57 · answer #2 · answered by Anonymous · 0 0

Of course, you could just open windows explorer and go to the directory with the txt files that you want to copy, ctrl click them all and then just drag them to the txtfiles folder

2006-08-16 13:57:06 · answer #3 · answered by chuckufarley2a 6 · 0 0

Hi.

copy
copy *.txt 'txt files'

Use quotes around the filename or directory if it contains spaces.

-Leon S

p.s. Reference links below.

2006-08-16 13:46:02 · answer #4 · answered by Leon Spencer 4 · 0 0

If you are in the current directory with the .txt files then you would use:

copy *.txt

so if you have you txt folder in c:\txt

copy *.txt c:\txt

2006-08-16 13:41:56 · answer #5 · answered by Bill H 2 · 0 0

in "dos" the answer is: copy drive:\*.txt drive:\*.txt /s
The "/ s" means all subdirectories below your postion in the directory hierarchy!

2006-08-16 13:57:06 · answer #6 · answered by one_hiq 2 · 0 0

xcopy *.txt c:\foldername\

2006-08-16 13:40:41 · answer #7 · answered by zerggle 2 · 0 0

fedest.com, questions and answers