TOUCH - CMD


Overview

The TOUCH command is used in Windows CMD to change the file access and modification timestamps of a file to the current time. If the file does not exist, it is created. This command is particularly useful in development environments for forcing updates, managing files, and scripts where timestamp sensitivity is pivotal.

Syntax

The basic syntax of the TOUCH command is:

TOUCH [option]... [file]...
  • [file]...: The file or files to touch. Multiple files can be specified, separated by spaces.
  • [option]...: Optional flags to modify the behavior of the command.

Options/Flags

TOUCH comes with several options that can be used to control its behavior:

  • -a: Changes only the access time of the file.
  • -m: Changes only the modification time of the file.
  • -c: Do not create any files; only modify the timestamps of existing files.
  • -r: Use the timestamp of the specified reference file.

The use of -a and -m can be combined to update both timestamps simultaneously, without specifying these flags, both timestamps are updated by default.

Examples

  1. Update Timestamp of a Single File:

    TOUCH myfile.txt
    

    This command sets the access and modification times of myfile.txt to the current time. If myfile.txt does not exist, it is created with no content.

  2. Create a New File:

    TOUCH newfile.docx
    

    If newfile.docx does not exist, this command creates it.

  3. Change Both Access and Modification Times of Multiple Files:

    TOUCH file1.txt file2.txt file3.txt
    

    Updates access and modification times for all specified files.

  4. Only Update Access Time:

    TOUCH -a file.txt
    

    This modifies only the access time of file.txt.

  5. Using a Reference File:

    TOUCH -r ref_file.txt target_file.txt
    

    Sets the access and modification times of target_file.txt to match those of ref_file.txt.

Common Issues

  • File Not Found Error:
    If the -c flag is used and the specified file does not exist, TOUCH will not create a new file. Ensure you are pointing to the correct file path or omit the -c option to create the file.

  • Permission Issues:
    Running TOUCH on files that you do not have write permissions for will result in an error. Make sure you have the necessary permissions or run the command prompt as an administrator.

Integration

Combine TOUCH with other commands to perform complex file management tasks. For example, to update the timestamps for all .txt files in a directory and then archive them:

FOR %f IN (*.txt) DO TOUCH %f
7z a archived_files.zip *.txt

This will touch all .txt files then compress them into a zip archive.

  • DATE and TIME: Commands to set or display the system date and time.
  • DIR: Lists the files and folders in the directory, use it to verify the effects of TOUCH.
  • COPY: Can be used with a no-change parameter (/b) to update file timestamps similarly.

For more detailed information about file management commands in CMD, refer to the official Microsoft documentation or resources like SS64.com.