1.

Solve : copy con in batch?

Answer»

Alright, what I'm trying to do is create a batch file that'll create the text files I list (the color change is to let me know when it's done at a quick glance). I'm making a sort of calendar, so the files looks like so:

Code: [Select]color 02
copy con 7-1.txt
copy con 7-2.txt
copy con 7-3.txt
copy con 7-4.txt
copy con 7-5.txt
copy con 7-6.txt
copy con 7-7.txt
copy con 7-8.txt
copy con 7-9.txt
copy con 7-10.txt
copy con 7-11.txt
copy con 7-12.txt
copy con 7-13.txt
copy con 7-14.txt
copy con 7-15.txt
copy con 7-16.txt
copy con 7-17.txt
copy con 7-18.txt
copy con 7-19.txt
copy con 7-20.txt
copy con 7-21.txt
copy con 7-22.txt
copy con 7-23.txt
copy con 7-24.txt
copy con 7-25.txt
copy con 7-26.txt
copy con 7-27.txt
copy con 7-28.txt
copy con 7-29.txt
copy con 7-30.txt
copy con 7-31.txt
color 0A
It freezes up after the first copy con command. Any help with this situation would be greatly appreciated.Quote from: Computer_Hopeless on June 30, 2007, 05:14:03 PM

Alright, what I'm trying to do is create a batch file that'll create the text files I list (the color change is to let me know when it's done at a quick glance). I'm making a sort of calendar, so the files looks like so:

It freezes up after the first copy con command. Any help with this situation would be greatly appreciated.

When you say it freezes after the first Copy Con command Copy is waiting for input from the Console (keyboard) to write to the first file. When the input is complete CTRL+Z is necessary to terminate that Copy and continue.

No input no continue..

why do you need to use a copy con? Quote from: ghostdog74 on July 01, 2007, 03:32:34 AM
why do you need to use a copy con?

I was wondering that.
I dunno... I thought I'd try copy con because it's new to me. I just need something to create all the files, preferably without a break between commands. Know a better command keyword to use?

Thanks for clearing up how copy con works.Quote from: Computer_Hopeless on July 01, 2007, 06:40:03 AM
I just need something to create all the files, preferably without a break between commands. Know a better command keyword to use?

echo sounds like the command to use. What are the files supposed to contain? If it is OK for them to be empty you could use echo like this

echo. > 7-1.txt
echo. > 7-2.txt

etc

Yeah, that'd work. Thanks.

Just for the sake of doing more work to try to avoid doing work in the future, what should I use if I wanted to put in, for example, a list of the hours of the day in half hour increments with no break or prompt between commands?Quote from: Computer_Hopeless on July 01, 2007, 06:58:39 AM
Just for the sake of doing more work to try to avoid doing work in the future, what should I use if I wanted to put in, for example, a list of the hours of the day in half hour increments with no break or prompt between commands?

What, you want to FILL a folder up with 48 empty files which are named after the hours and half hours of the day?

I guess something like this would work. I assuming the 24 hour system. If you need a 12 hour system with am & pm then I am sure you can work out what changes are needed.

echo. > 00-00.txt
echo. > 00-30.txt
echo. > 01-00.txt

[snip]

echo. > 23-00.txt
echo. > 23-30.txt

(You cannot use a colon in a file name)

However, like the other people who have learned to code before you, you will soon be realising that it is a pain in the @ss to keep typing the same thing over and over again, ALSO it makes code which experienced programmers smile at, which is the point at which one learns about one of the fundamental programming constructs, the loop.

Loops exist in batch programming language. Here is one which will do the same job as the 48 line batch I outlined above.

Code: [Select]@echo off
for %%H in (00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23) do (
echo. > %%H-00.txt
echo. > %%H-30.txt
)

The FOR command could be shortened considerably but I left it like that on purpose so it illustrates the principle better.




That's pretty useful to know, but I was asking about having the hours within each date named text file.start a file with >, then add to it with >>

echo 00-00 > 7-1.txt
echo 00-30 >> 7-1 txt

[snip]

echo 23-00 >> 7-1.txt
echo 23-30 >> 7-1.txt

echo 00-00 > 7-2.txt
echo 00-30 >> 7-2 txt

[snip]

echo 23^:00 >> 7-2.txt
echo 23^:30 >> 7-2.txt

[etc]

To fill in the blanks is a trivial exercise which I shall leave for you to complete...


Such a batch would be 48 x 31 lines long... 1488 lines... but I expect you can think of a way around that...


you can add the time programmatically...here's a vbscript
Code: [Select]Dim myTime, objFSO, startTime,count,FilePrefix,objFile
Set objFSO= CreateObject("Scripting.FileSystemObject")
startTime="00:00:00"
count=1
FilePrefix="7_"
myTime=FormatDateTime(DateAdd("n",30,CDate(startTime)),vbShorttime)
Set objFile = objFSO.CreateTextFile(FilePrefix&CStr(count)&".txt",True)
objFile.Write(myTime)
objFile.Close
count=count+1
Do while myTime <> "00:00"
myTime=FormatDateTime(DateAdd("n",30,CDate(myTime)),vbShorttime)
Set objFile = objFSO.CreateTextFile(FilePrefix&CStr(count)&".txt",True)
objFile.Write(myTime)
objFile.Close
count=count+1
Loop
Thanks again for all the help.

When using the FOR command, how would you set a range of numbers within the set, instead of listing them all out?

What EXACTLY is and how exactly do I use a "vbscript"? I'll wiki it in the meantime.

This is my new batch file, loopcal.bat:

Code: [Select]color 02
for %%D in (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) do (
echo. > 7-%%D.txt
)
for %%H in (1 2 3 4 5 6 7 8 9 10 11 12) do (
echo %%H am >> 7-??.txt
echo %%H pm >> 7-??.txt
)
color 0a
It'll create the files, but when it's supposed to print the hours in each one, it tells me:

The filename, directory name, or volume label syntax is incorrect.

Know where I messed up?Quote from: Computer_Hopeless on July 01, 2007, 09:37:31 AM
When using the FOR command, how would you set a range of numbers within the set, instead of listing them all out?

FOR /L %%parameter IN (start,step,end) DO command

start : The first number
step : The amount by which to increment the sequence
end : The last number

command : The command to carry out, including any
command-line parameters.

%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)

So (20,-5,10) would generate the sequence (20 15 10)

(1,1,5) would generate the sequence 1 2 3 4 5

Quote
What exactly is and how exactly do I use a "vbscript"? I'll wiki it in the meantime.

Never use 'em. Scripts for Windows Scripting Host. (Wiki or Google that also)

Quote
This is my new batch file, loopcal.bat:

Know where I messed up?

1. There are 31 days in the month of July.
2. You need to nest the loops, which is going to bring us to the topic of delayed variable expansion.
3. Those question marks in "echo %%H am >> 7-??.txt" are errors and will generate the message you saw.

Can I just ask, forgive me if I am wrong, but is this your homework?


If this is a homework project, I predict your teacher will know for sure you didn't write this, so you won't dare submit it, and if it's for your own amusement, I apologise for my nasty suspicions and invite you to pore over this and attempt to puzzle it out by visiting some of the batch tutorials and guides that are plentiful on the web.

It's just that many many batch questions start off "I was just wondering [yeah, right!] how to do xyz in a batch file", and there follows a suspiciously detailed and arbitrary specification for some project which nobody would dream up for themselves in a month [28,30 or 31 days!] of Sundays (AM/PM, half hourly intervals, etc etc), which smells strongly of "a teacher wrote this". Forgive my suspicious mind if I have got it all wrong, and in that case please accept the following gift...

This is a hacked-together quick and dirty batch file. It's not a definitive solution, it's just how I would choose to make a calendar in a batch. It could be extended to do a whole year, or any month as required, to know which months have 30 or 31 days, even to decide if February has 28 or 29 days that year.

Code: [Select]@echo off
setlocal enabledelayedexpansion
REM With a number loop use the /L switch
FOR /L %%A IN (1,1,31) DO (
set filename=7-%%A

REM You insist on AM/PM which makes things trickier
REM Days start at 12 AM or end at 12 PM???
REM Assume latter

REM Pesky old fashioned AM/PM
echo 12^:00 Midnight > !filename!.txt
echo 12^:15 AM >> !filename!.txt
echo 12^:30 AM >> !filename!.txt
echo 12^:45 AM >> !filename!.txt
echo. >> !filename!.txt

FOR /L %%B IN (1,1,11) DO (
set /a hour=%%B
REM make look neater with leading zeroes
REM for hours 1 to 9
if !hour! LSS 10 set hour=0!hour!
echo !hour!^:00 AM >> !filename!.txt
echo !hour!^:15 AM >> !filename!.txt
echo !hour!^:30 AM >> !filename!.txt
echo !hour!^:45 AM >> !filename!.txt
echo. >> !filename!.txt
)

REM Pesky old fashioned AM/PM
echo 12^:00 Noon >> !filename!.txt
echo 12^:15 PM >> !filename!.txt
echo 12^:30 PM >> !filename!.txt
echo 12^:45 PM >> !filename!.txt
echo. >> !filename!.txt

FOR /L %%B IN (1,1,11) DO (
set /a hour=%%B
if !hour! LSS 10 set hour=0!hour!
echo !hour!^:00 PM >> !filename!.txt
echo !hour!^:15 PM >> !filename!.txt
echo !hour!^:30 PM >> !filename!.txt
echo !hour!^:45 PM >> !filename!.txt
echo. >> !filename!.txt
)
)



Here's 7-23.txt

Code: [Select]12:00 Midnight
12:15 AM
12:30 AM
12:45 AM

01:00 AM
01:15 AM
01:30 AM
01:45 AM

02:00 AM
02:15 AM
02:30 AM
02:45 AM

03:00 AM
03:15 AM
03:30 AM
03:45 AM

04:00 AM
04:15 AM
04:30 AM
04:45 AM

05:00 AM
05:15 AM
05:30 AM
05:45 AM

06:00 AM
06:15 AM
06:30 AM
06:45 AM

07:00 AM
07:15 AM
07:30 AM
07:45 AM

08:00 AM
08:15 AM
08:30 AM
08:45 AM

09:00 AM
09:15 AM
09:30 AM
09:45 AM

10:00 AM
10:15 AM
10:30 AM
10:45 AM

11:00 AM
11:15 AM
11:30 AM
11:45 AM

12:00 Noon
12:15 PM
12:30 PM
12:45 PM

01:00 PM
01:15 PM
01:30 PM
01:45 PM

02:00 PM
02:15 PM
02:30 PM
02:45 PM

03:00 PM
03:15 PM
03:30 PM
03:45 PM

04:00 PM
04:15 PM
04:30 PM
04:45 PM

05:00 PM
05:15 PM
05:30 PM
05:45 PM

06:00 PM
06:15 PM
06:30 PM
06:45 PM

07:00 PM
07:15 PM
07:30 PM
07:45 PM

08:00 PM
08:15 PM
08:30 PM
08:45 PM

09:00 PM
09:15 PM
09:30 PM
09:45 PM

10:00 PM
10:15 PM
10:30 PM
10:45 PM

11:00 PM
11:15 PM
11:30 PM
11:45 PM

School has been out for a while, this is just for my own curiosity and amusement. Thank you again for the help. I'll mess around with what you've given me and get back to you in a while. This loop tool (the only loops I'd ever used before were on the school calculators) will be exceptionally useful in the future. Thanks.


Discussion

No Comment Found