In "Real-World Scripting: Building a Code Collection," June 2001, InstantDoc ID 20795, I showed you how to build a collection of code snippets, including one-liners. A one-liner is a command sequence that can perform seemingly amazing tasks, given the small amount of code. Here are more practical one-liners that you can use to remove old files, prevent runaway scripts, and prevent other mishaps, such as running a script on the wrong computer.

Teaching Old Commands New Tricks
Have you ever thought a command or utility would be perfect for a job only to discover that it's missing an option that you need? Built-in commands, resource kit tools, and third-party utilities never seem to offer all the functionality you'd like. Fortunately, you can often use one-liners to compensate for the functionality that a command or utility lacks.

For example, suppose you have a large share, J:\public, on which users exchange and store files. Because users often forget to remove old files, you wrote a script, DelOldFiles.bat, which deletes empty folders and files more than 90 days old. For several months, this script kept the share clean. However, you recently noticed that the script hasn't been deleting many of the share's empty folders.

After watching the console during one of the script's scheduled runs, you discover that the message Access is denied appears whenever the script fails to delete an empty folder. Further investigation reveals that some users have discovered that they can keep their empty folders from being deleted by selecting the Read-only check box in the Attributes section of the folders' Properties dialog box.

With management's approval, you set out to change the read-only attribute of these empty folders so that you can delete them. Thinking that the Attrib command would be perfect for this task, you view the command's syntax, which Figure 1 shows, and discover a potential pitfall. With the Attrib command's /s and /d switches, you can change permissions for a folder and its subfolders. However, if you use these switches for J:\public, you'll also change the permissions for all the files, which you don't want to touch.

You determine that the Dir command can be of assistance. The Dir command lists files and folders in directories. By using the command

Dir /a:dr /s /b J:\public

you can locate and list all the read-only folders in J:\public. The /a:dr switch tells the Dir command to list only those items that have the attributes of directory (d) and read-only (r). The /s switch ensures that the list includes all subfolders. The /b switch tells the Dir command to provide bare output—in other words, not to include any headings or summary information.

Next, you need to redirect the Dir command's output to the Attrib command so that you can change the permissions. You can use the command

Attrib -r "%i"

In this command, the r specifies that you want to work with the read-only attribute and the hyphen (-) in front of it specifies that you want to clear that attribute. When set in the For command

For /f "tokens=*" %i in
  ('Dir /a:dr /s /b J:\public')
  Do (Attrib -r "%i")

the %i iterator variable will contain each of the read-only folders listed in the Dir command's output.

Finally, you need to log the results. You discover that you can't redirect the Attrib -r command's output to a log, but you can redirect the output of a plain Attrib command. So, you add the Attrib command and redirect its output to the file log.txt with the code

& (Attrib "%i">>C:\temp\log.txt)

The finished one-liner to change the read-only folders' permissions is

For /f "tokens=*" %i in
  ('Dir /a:dr /s /b J:\public')
  Do (Attrib -r "%i") &
  (Attrib "%i">>C:\temp\log.txt)

To use this code in your environment, you need to customize the share (i.e., J:\public) and type the code in one continuous line in the command-shell window. If you want to use this one-liner in a script, you need to add an extra percent sign (%) to the %i iterator variable (i.e., %%i). For information about how to use iterator variables on the command line and in scripts, see "Shell Scripting 101, Lesson 8," August 2001, InstantDoc ID 21984.

After you run this one-liner, you can check the results by viewing the log. If a folder appears in the log, you can tell that the folder's Read-only check box had been selected. If the letter R doesn't precede the folder, you can tell that the one-liner successfully cleared that check box.

Preventing Runaway Scripts
The power of scripting is that a script can perform numerous operations quickly. But that blinding speed can work against you if a script is launched in error. A misplaced double-click or accidentally pressing Enter while a .bat file is selected can send a script into action, copying files, deleting users, or performing some other operation at warp speed. A runaway script can do considerable damage before someone has the chance to cancel it. You can prevent the accidental launching of a script by requiring a password, adding the Pause command, or adding a .txt extension.

Requiring a password. You can prevent runaway scripts by requiring a simple password to launch a script. To launch a script, administrators need to enter the correct password as an argument. For example, if you require the password go to launch DelOldFiles.bat, which is in your machine's Scripts directory, administrators need to type

scripts\deloldfiles.bat go

on the command line. Without the password, the script doesn't run. Thus, the password protects against accidental launching.

To require a password to run a script, you can use the one-liner

@If /i Not '%1'<h1><a name="_go_Echo_Sorry_you_need_the_password_Exiting_now_amp_Goto_EOF_pre_p_Place_this_code_at_the_top_of_your_script_For_the_code_to_work_all_the_code_must_be_on_one_line_You_can_replace_i_go_i_with_a_custom_password_The_password_can_be_simple_and_easy_to_remember_because_it_s_not_meant_for_security_purposes_Anyone_with_read_permission_can_open_the_script_and_find_the_password_At_the_end_of_your_script_you_can_place_an_EOF_label_if_you_want_This_label_is_optional_because_EOF_is_a_special_reserved_expression_in_Windows_shell_scripting_For_more_information_about_EOF_see_Bob_Muzzy_Reader_to_Reader_Two_Tricks_for_Batch_Files_August_2001_InstantDoc_ID_21576_p_p_b_i_Adding_the_Pause_command_i_b_Another_way_to_prevent_runaway_scripts_is_to_use_the_Pause_command_This_command_displays_the_message_i_Press_any_key_to_continue_i_then_waits_for_a_keystroke_The_script_won_t_continue_until_someone_presses_a_key_By_using_the_Pause_command_with_a_message_that_specifies_what_the_script_does_and_how_to_cancel_or_continue_the_script_s_execution_you_can_avoid_not_only_accidental_launches_but_also_execution_of_the_wrong_scripts_For_example_the_one_liner_in_DelOldFiles_bat_might_be_p_pre_Echo_This_script_deletes_old_files_and_empty_folders_on_J_public_Press_Ctrl_C_to_exit_Press_any_other_key_to_continue_amp_Pause_pre_p_To_use_the_Pause_command_one_liner_customize_the_description_of_what_the_script_does_and_place_the_code_on_one_line_at_the_beginning_of_the_script_Don_t_use_this_one_liner_or_the_Pause_command_in_general_in_a_script_that_you_plan_to_run_with_Task_Scheduler_Task_Scheduler_comes_with_Microsoft_Internet_Explorer_#151_IE_#151_4_x_and_later_When_you_use_Task_Scheduler_no_one_is_present_to_press_a_key_so_the_script_will_never_finish_executing_p_p_b_i_Adding_a_txt_extension_i_b_Another_way_to_keep_a_script_from_running_amuck_is_to_add_a_txt_extension_to_its_filename_after_you_ve_executed_the_script_For_example_the_script_DelOldFiles_bat_becomes_DelOldFiles_bat_txt_Adding_a_txt_extension_defuses_the_danger_of_accidental_launches_but_lets_you_keep_the_script_s_original_filename_and_code_intact_for_future_reference_and_use_To_use_the_script_again_you_just_remove_the_txt_extension_p_p_Although_you_can_add_the_txt_extension_manually_a_simple_one_liner_will_prompt_a_script_to_automatically_rename_itself_The_one_liner_p_pre_Move_0_0_txt_pre_p_uses_the_Move_command_with_i_0_i_#151_an_argument_holding_environment_variable_whose_value_is_the_script_s_pathname_By_using_0_as_the_source_parameter_i_e_a_pathname_that_specifies_the_file_s_current_location_and_filename_and_i_0_txt_i_as_the_destination_parameter_i_e_a_pathname_that_specifies_the_file_s_new_location_and_filename_you_re_appending_the_txt_extension_to_the_filename_while_moving_the_script_to_the_same_location_p_p_To_use_the_Move_command_one_liner_you_must_place_it_at_the_end_of_your_script_It_must_be_the_final_line_of_code_any_code_after_it_won_t_execute_When_you_run_the_script_you_must_launch_it_with_the_extension_specified_For_example_when_you_run_the_script_DelOldFiles_bat_you_need_to_type_p_pre_scripts_deloldfiles_bat_pre_p_on_the_command_line_Typing_only_p_pre_scripts_deloldfiles_pre_p_will_cause_the_Move_command_one_liner_to_fail_p_p_Because_you_re_changing_the_name_of_the_bat_file_while_it_s_running_you_ll_likely_receive_the_message_i_The_batch_file_cannot_be_found_i_Despite_this_message_the_Move_command_one_liner_will_have_successfully_added_the_txt_extension_p_p_If_you_re_familiar_with_Windows_shell_scripting_you_might_be_wondering_why_I_didn_t_use_the_Rename_command_to_add_the_txt_extension_To_use_this_command_to_rename_a_file_you_use_the_syntax_p_pre_Rename_drive_path_filename1_filename2_pre_p_As_this_syntax_shows_the_Rename_command_requires_the_file_s_path_to_be_stated_only_once_If_you_were_to_use_the_0_argument_holding_environment_variable_with_the_Rename_command_you_d_be_specifying_the_path_twice_because_0_contains_the_complete_pathname_thereby_causing_the_command_to_fail_p_p_Another_technique_is_to_use_the_one_liner_p_pre_Del_0_pre_p_to_simply_delete_the_script_after_it_s_executed_After_all_no_one_can_accidentally_launch_a_script_that_doesn_t_exist_Administrators_often_use_this_approach_for_scripts_that_run_locally_on_users_workstations_After_performing_a_task_a_script_deletes_itself_to_clean_up_the_user_s_workstation_However_you_need_to_use_extreme_caution_with_this_approach_The_deleted_script_won_t_be_in_the_Recycle_Bin_Thus_before_you_test_or_use_a_self_deleting_script_make_sure_that_you_have_another_copy_of_the_script_Spending_many_hours_writing_a_script_only_to_have_it_permanently_disappear_during_testing_or_use_would_be_quite_upsetting_p_p_font_class_subhead_Preventing_Mishaps_font_br_Sometimes_an_administrator_might_intentionally_run_a_script_but_run_it_on_the_wrong_computer_If_you_want_to_prevent_this_mishap_you_can_use_a_one_liner_that_includes_an_If_command_with_the_Computername_environment_variable_Take_for_example_the_one_liner_p_pre_If_i_Not_Computername_">'go' Echo Sorry,
  you need the password.
  Exiting now. & Goto :EOF</a></h1>

Place this code at the top of your script. For the code to work, all the code must be on one line. You can replace go with a custom password. The password can be simple and easy to remember because it's not meant for security purposes. Anyone with read permission can open the script and find the password. At the end of your script, you can place an :EOF label if you want. This label is optional because :EOF is a special reserved expression in Windows shell scripting. (For more information about :EOF, see Bob Muzzy, Reader to Reader, "Two Tricks for Batch Files," August 2001, InstantDoc ID 21576.)

Adding the Pause command. Another way to prevent runaway scripts is to use the Pause command. This command displays the message Press any key to continue, then waits for a keystroke. The script won't continue until someone presses a key. By using the Pause command with a message that specifies what the script does and how to cancel or continue the script's execution, you can avoid not only accidental launches but also execution of the wrong scripts. For example, the one-liner in DelOldFiles .bat might be

Echo This script deletes old files
  and empty folders on J:\public.
  Press Ctrl+C to exit. Press any
  other key to continue. & Pause

To use the Pause command one-liner, customize the description of what the script does and place the code on one line at the beginning of the script. Don't use this one-liner (or the Pause command in general) in a script that you plan to run with Task Scheduler. (Task Scheduler comes with Microsoft Internet Explorer—IE—4.x and later.) When you use Task Scheduler, no one is present to press a key, so the script will never finish executing.

Adding a .txt extension. Another way to keep a script from running amuck is to add a .txt extension to its filename after you've executed the script. For example, the script DelOldFiles.bat becomes DelOldFiles.bat.txt. Adding a .txt extension defuses the danger of accidental launches but lets you keep the script's original filename and code intact for future reference and use. To use the script again, you just remove the .txt extension.

Although you can add the .txt extension manually, a simple one-liner will prompt a script to automatically rename itself. The one-liner

Move %0 %0.txt

uses the Move command with %0—an argument-holding environment variable whose value is the script's pathname. By using %0 as the source parameter (i.e., a pathname that specifies the file's current location and filename) and %0.txt as the destination parameter (i.e., a pathname that specifies the file's new location and filename), you're appending the .txt extension to the filename while "moving" the script to the same location.

To use the Move command one-liner, you must place it at the end of your script. It must be the final line of code; any code after it won't execute. When you run the script, you must launch it with the extension specified. For example, when you run the script DelOldFiles.bat, you need to type

scripts\deloldfiles.bat

on the command line. Typing only

scripts\deloldfiles

will cause the Move command one-liner to fail.

Because you're changing the name of the .bat file while it's running, you'll likely receive the message The batch file cannot be found. Despite this message, the Move command one-liner will have successfully added the .txt extension.

If you're familiar with Windows shell scripting, you might be wondering why I didn't use the Rename command to add the .txt extension. To use this command to rename a file, you use the syntax

Rename \[drive:\] \[path\]
  filename1 filename2

As this syntax shows, the Rename command requires the file's path to be stated only once. If you were to use the %0 argument-holding environment variable with the Rename command, you'd be specifying the path twice because %0 contains the complete pathname, thereby causing the command to fail.

Another technique is to use the one-liner

Del %0

to simply delete the script after it's executed. After all, no one can accidentally launch a script that doesn't exist. Administrators often use this approach for scripts that run locally on users' workstations. After performing a task, a script deletes itself to clean up the user's workstation. However, you need to use extreme caution with this approach. The deleted script won't be in the Recycle Bin. Thus, before you test or use a self-deleting script, make sure that you have another copy of the script. Spending many hours writing a script only to have it permanently disappear during testing or use would be quite upsetting.

Preventing Mishaps
Sometimes, an administrator might intentionally run a script but run it on the wrong computer. If you want to prevent this mishap, you can use a one-liner that includes an If command with the %Computername% environment variable. Take, for example, the one liner

If /i Not '%Computername%''dom1pdc'
  Echo You appear to be running
  this script on the wrong computer.
  Exiting now. & Goto :EOF

If an administrator tries to run this script on any computer other than dom1pdc, the script displays the message You appear to be running this script on the wrong computer. Exiting now. Then, the script goes to the end of the file and exits.

Similarly, you can use an If statement with the %Username% environment variable to test the administrator's logged-on username:

If /i Not '%Username%'=='fredr'
  Echo You appear to be
  running this script in the
  wrong user context. Exiting
  now. & Goto :EOF

In this case, if an administrator tries to run this script under any other username than fredr, the script displays the message You appear to be running this script in the wrong user context. Exiting now. Then, the script goes to the end of the file and exits.

To use the one-liner that tests for a computer name or username, you need to customize the computer name or username. Then, you need to place the code all on one line at the beginning of the script.

Shorter Is Better
As these sample one-liners show, you don't need lines and lines of code to automate tasks. With just one command sequence, you can perform amazing feats.