Copying and concatenation
In we will:

Creating sample source files
We now have the logic flow through the menu and sub-menu of the Build option working. Our script creates a temporary file, SF, in the folder. We need to use this sample file to create a set of test source files (which we'll use in developing our backup script).
Open BKSETUP.BAT script in Notepad
Open the BKSETUP.BAT script in Notepad with a command :
Commenting out a line in a Batch script
Our script creates the sample file, SF, and later deletes it (in the :CLEANUP section). We need to practise using this file in immediate mode, so it would be more useful (for now) if the sample weren't deleted as the script finishes.
The easy way to disable (temporarily) one or more lines of a script is to them out: that is, to prefix each with so they become non-functioning comments.
out the line that deletes our sample file :
Run the script to leave the sample file in place
You've clicked File, Save, so run the script, and choose B . This will create our sample file, SF, rebuild a new set of sample folders, and then leave the sample file in place (in the folder) because we commented out the line :
Check with
Check the sample file is now left in place. Use a command :
Copying the sample file
We'll make a number of files from our sample, simply by copying it with . We've seen how to copy the original file to one with a different name in one operation. The original file is left unaffected. Make a copy called FILENAME.EXT in C:\BKTEST. Remember to use the
Permission to overwrite
Repeat the same command. You're now asked for permission to overwrite the existing file, FILENAME.EXT, that we've just created in . Reply Y for yes, and press Return :
Inconsistent prompting
As we warned in Lesson 8, you'll find that replying to Windows' prompts for such things as permission to overwrite a file is inconsistent between different commands:
Suppress prompt for overwrite permission
When BKSETUP.BAT is used to create sample files, they won't exist already. However, if they did, this prompt would stall the script while waiting for a user response. When it doesn't make sense to get permission (as in this case) you can use the /y switch of to reply Y automatically, avoiding the prompt. Try again, with /y :
Note 1 In scripts, we'll normally use
Note 2 FILENAME.EXT is a canonical sample file name. The EXT simply stands for extension, and isn't a normal valid file type (but in this case, these are only sample files for us to practise backing up, so the names and extension types can be arbitrary).
Suppress files copied report with >NUL
Since the user of a script won't see the command, it doesn't make sense to leave the files copied report visible either. We can suppress it with to . Use to create another file, and this time suppress the report :
Check with
Check the copied files have been placed in C:\BKTEST :
Use /a-d to avoid listing folders
Remember that DATA and
Tuning listings precisely
listings (which can be captured to files with , just like messages), are useful for many purposes. Learning to tune them to the precise items you want (there are many switches to help with this) is a useful skill.
In the script we'll expand the SRC variable
Now we've practised in immediate mode, let's transfer these commands to our script. As usual, in the script, since we set SRC to our test folder, C:\BKTEST, we'll expand the variable when we create the files:
| In our script, this: | expands to this: |
| %SRC% | C:\BKTEST |
| And so these: | are expanded to these: |
| %SRC%\FILENAME.EXT | C:\BKTEST\FILENAME.EXT |
| %SRC%\ANOTHER.TXT | C:\BKTEST\ANOTHER.TXT |
Add commands to the script
These lines will go in the :BUILDYES section (after we've created the folders). Add the lines with a (as shown) to explain what we're doing :
Delete the test folder then check the script
With those new lines saved in the script, we can test them. We've already created these two files manually, as practice. And so, to test the script properly, we'll first delete them again (along with the entire test folder) with :
Check with
Check that the C:\BKTEST folder and all the files really have gone :
A \*.* flags BKTEST as a folder, not a file
We've met the idea that to ensure knows we mean the folder C:\BKTEST (and not a file with that name) we add the \*.* to the foldername. When we used this before it was merely to get to report a more sensible version of the "not found" message.
In this case, since we used the /s (include subfolder contents) switch, we needed the \*.* to mark C:\BKTEST as a folder. Without it, if there were no folder with that name, the /s switch would cause to search the entire C: drive for any files named BKTEST
Searching for all files with a particular name
For example, if you used README.TXT in that command (without the
Check script rebuilds folder and the two files
And now, back to the matter in hand. We need to test that those new lines do create our two sample files in C:\BKTEST. Run the script, press B , then press Y at the sub-menu :
Check with
Use to check the test folders and the two files were properly rebuilt :
Using to concatenate files
Although is not as versatile as the command (which we'll make use of in later Lessons), it has a useful feature: file concatenation. Concatenating files means joining several files together into one single file. We'll use this feature to make some of our sample files longer.
Create four small files
To see how it works, make four small files with , first J1.TXT :
Now create J2.TXT
Next, create J2.TXT with the next line :
Then create J3.TXT
Then create J3.TXT with the third line :
And lastly J4.TXT
And finally, create J4.TXT with the fourth line of Jabberwocky :
Concatenate the files
To concatenate these files, you use , listing the files to be joined with + in between. It's a good idea to use the /b switch when concatenating files :
The /b avoids a legacy Ctrl Z terminator
The 1 file(s) copied shows only one new file was created. The /b switch ensures the files are copied normally (as binary images). Without it, adds a final Ctrl Z character to the combined file when concatenating (this is a legacy feature required by some older software, and hardly ever used now).
Four become one
Now the new file's contents :
Using a for concatenation
You can use concatenation with . We can specify J?.TXT, where the ? wildcard represents any single character in that position. finds all the files of that form and combines them in the destination file.
Try a wildcard concatenation; make a second file CARROLL2.TXT :
The doesn't allow control of the order
With the method, locates the files in the order they just happen to occur in the folder index (a low-level disk structure). In this case, since we created the files in order (and without deleting anything in between to leave spare index slots), they just happen to be in the right order, this time.
The + method gives you proper control of the order in which files are combined.
Check with
the new CARROLL2.TXT file to confirm its contents :
An easy way to compare files, (file compare)
Both files look the same. However, when you need to be sure two files are the same, you can use the (file compare) command. Try it now :
Click for details of the file compare utility
Remember you can click on for our Course Reference to see extra details and syntax examples that aren't in the normal
Doubling up our sample file with concatenation
We can use concatenation with with our sample file, so that not all the test files we create from it are the same length.
We'll simply concatenate our temporary small file SF with itself, and call the result LF (larger file, and again it doesn't need an extension because we'll give the copies of it an extension when we create them). We'll create LF in the folder.
The new command goes in the :BUILDYES section. Add it (with a ) immediately after the point where we make the new folders :
Run the script to check the LF file's contents
You clicked File, Save? Run the script to check that the LF file is created correctly. Choose B , then Y at the sub-menu :
Check LF with
We haven't put a delete command in :CLEANUP for the new LF file, yet. So it will remain, and we can check it consists of the quatrain twice over :
Making more test files
Now we'll add some more lines, making copies of our longer sample file, LF, to create some new files in C:\BKTEST\DATA. We'll give those copies of the file the extension DAT – add the commands after the other lines :
Add some files to the
Now add the command to create a last pair of sample files in the
Add the new command lines after the other lines :
Test the script once more
You've clicked File, Save to save the four new lines, so let's test them now. Run the script again, choose B then Y :
Check with
Check the test folder, its two subfolders, and all six files were created :
Test often to pick up errors more easily
We've now completed the code for the Build option in our script tool. You may have made a few mistakes in adding all the new lines. Our testing frequently, after every minor addition, should have made it easy for you to see which lines were going wrong.
When you write your own scripts, follow our practice of testing the script after every few lines added. You'll pick up errors more easily. The most common problems are usually:
Use debug techniques
For more troublesome errors, remember the debug techniques we've covered:
Breakpoints are useful
A further technique is useful, too. The command pauses execution of a script until Return is pressed. You can insert an command followed by a at any of several points in your script: this creates breakpoints.
prompt. And you can click on any command name in blue to call up our Course reference.Our temporary workfiles still remain in
When we started this Lesson, we commented out the clean up command for the workfile SF. We have also created a further workfile, LF, in . This file must also be deleted when the script finishes. We could do this with a further command, but instead, we'll look at another feature of .
We now have 16 files
We have accumulated some clutter in C:\CSW. List all the files :
It's best to tidy up sooner rather than later
We shan't need the following files any longer:
| J1.TXT | |
| J2.TXT | |
| J3.TXT | |
| J4.TXT | (those four were combined to make CARROLL.TXT) |
| CARROLL2.TXT | (it's a duplicate of CARROLL.TXT) |
| 314159.TXT | |
| 265358.TXT | (those two were combined to make RhymeForPi.txt) |
Using a list of files for deletion with
You can list several specific files, and even mix in some specifications, all in the same command line, and will delete all of them. So we can use a combined line such as:
to clear away the files we no longer need. Try this now :
Check with
Use to check the surplus files have now been deleted :
Cleaning out workfiles with
We'll replace the command in our script with a list. In the script, we'll add the /y switch to avoid 's confirmation prompt. And we shan't need to see the files deleted report either, so we'll redirect it to .
By the way, remember always to spell with only one L , or you'll accidentally create a file called NULL in your C:\CSW folder!
Remove the command and add the line as shown :
Note 1 Remember, doesn't show an error message if you try to delete a file that doesn't exist. So we can (try to) delete both SF and LF in the :CLEANUP section (used by all options), even though LF is created only by the B option. This feature is useful. It saves our having to test whether LF exists before trying to delete it.
Note 2 We don't use a such as %TEMP%.\?F in our script – although it's shorter and would do the job. When we used the J?.TXT wildcard in , we knew there were no other files of that form that might also get deleted. In our script we make certain we delete only those workfiles we create. Anything else isn't our business.
Check the script does remove the files
The change is saved, so run the script. Choose the Q option. As with all the options, its flow goes through the
Check with
Check the SF and LF files are no longer in the folder. Use :
Always make sure workfiles are erased
When you are developing your own scripts, always check that they clean up their own workfiles before they finish. Otherwise disk clutter gradually builds up. It wastes space and may even slow your system down. We've tidied up, so we'll finish now.
What we have learnt
In this Lesson we have learnt how to: