No announcement yet.

Batch File creation - Help Needed.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch File creation - Help Needed.


    Ive created a cd that autoruns a graphical menu with clickable links, and one of the links points to a HOSTS.bat file that copies over a custom HOSTS file from the cd to the ..\drivers\etc folder on the system root.

    The problem I'm having is that I'm unsure of the correct syntax to use to automatically overwrite the existing HOSTS file.

    I need to write it to work on both Win2K and XP machines, so have included two lines; one copies the HOSTS file to WINDOWS\system32\drivers\etc, and one copies it to Winnt..\etc. Thsi seems to work ok, for if it's an XP machine it ignores the Winnt instruction and vica versa.

    However, this has all been tested with my original HOSTS file temporarily renamed to HOSTS2, so there is no HOSTS file to be overwritten. I cannot seem to make it work if there is a HOST file already present.

    Is there a switch to make it overwrite? Or changing tack entirely, is there a way I can instruct the IP address I need to be written into the original HOSTS file?

    Here is the contents of the batch file as it stands:


    copy "hosts" "C:\WINDOWS\system32\drivers\etc\hosts"

    copy "hosts" "C:\WINNT\system32\drivers\etc\hosts"

    If anyone can help I would be most appreciative!


  • #2

    Use 'XCOPY' not 'COPY'

    XCOPY is much more powerful

    Open a command prompt window (START > RUN > type CMD press Enter)

    type: XCOPY /?

    This shows the switches you can use with the XCOPY command.

    XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
    [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
    [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z]

    source Specifies the file(s) to copy.
    destination Specifies the location and/or name of new files.
    /A Copies only files with the archive attribute set,
    doesn't change the attribute.
    /M Copies only files with the archive attribute set,
    turns off the archive attribute.
    /D:m-d-y Copies files changed on or after the specified date.
    If no date is given, copies only those files whose
    source time is newer than the destination time.
    Specifies a list of files containing strings. Each string
    should be in a separate line in the files. When any of the
    strings match any part of the absolute path of the file to be
    copied, that file will be excluded from being copied. For
    example, specifying a string like \obj\ or .obj will exclude
    all files underneath the directory obj or all files with the
    .obj extension respectively.
    /P Prompts you before creating each destination file.
    /S Copies directories and subdirectories except empty ones.
    /E Copies directories and subdirectories, including empty ones.
    Same as /S /E. May be used to modify /T.
    /V Verifies each new file.
    /W Prompts you to press a key before copying.
    /C Continues copying even if errors occur.
    /I If destination does not exist and copying more than one file,
    assumes that destination must be a directory.
    /Q Does not display file names while copying.
    /F Displays full source and destination file names while copying.
    /L Displays files that would be copied.
    /G Allows the copying of encrypted files to destination that does
    not support encryption.
    /H Copies hidden and system files also.
    /R Overwrites read-only files.
    /T Creates directory structure, but does not copy files. Does not
    include empty directories or subdirectories. /T /E includes
    empty directories and subdirectories.
    /U Copies only files that already exist in destination.
    /K Copies attributes. Normal Xcopy will reset read-only attributes.
    /N Copies using the generated short names.
    /O Copies file ownership and ACL information.
    /X Copies file audit settings (implies /O).
    /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.
    Just use the switches after the lines in your BAT file... you'll probably want to use the /Y switch though to overwrite if the file exists.

    Hope that helps...

    1 + 1 = 11 ... honest!


    • #3
      just add the following line instead of those 2 lines:

      copy /y <CDDRIVE>\dir\hosts "%windir%\system32\drivers\etc\hosts
      if this won't work, you can indeed use xcopy instead of copy
      Technical Consultant

      MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
      "No matter how secure, there is always the human factor."

      "Enjoy life today, tomorrow may never come."
      "If you're going through hell, keep going. ~Winston Churchill"


      • #4
        Originally posted by Dumber
        just add the following line instead of those 2 lines:

        copy /y <CDDRIVE>\dir\hosts "%windir%\system32\drivers\etc\hosts
        if this won't work, you can indeed use xcopy instead of copy
        That should work fine, but remember you'll have to be an Admin on the machine for the overwrite to work.

        * Shamelessly mentioning "Don't forget to add reputation!"


        • #5

          Thanks for the reply, it worked a treat!