Questions

DOS Batch File Scripting Help

+
0 Votes
Locked

DOS Batch File Scripting Help

jhamilto
I have some (*.txt, *.log,) & another type with a date extension.....files to delete out of some directories on different locations on the C:\ drive of a PC.
I want to open notepad and write a text file and then save as a batch file. I need the script to open up directory paths and erase files with (*.txt, *.log). What is the correct syntax to open directory and erase the file in respective path's of c:
  • +
    0 Votes
    Who Am I Really

    to move about in directories
    use the "CD" command

    CD \pathname

    ie.

    the easiest method of getting a path into a batch file is to first go to the location with Windows Explorer

    then copy the contents of the address bar to the notepad window containing the batch file
    being created

    To get into the Temp folder under your User Name

    use the correct path for your version of the OS
    ie. on Win2K / XP it's

    C:\Documents and Settings\User name\Local Settings\Temp

    and to go there from the command window or a batch file it's

    CD "C:\Documents and Settings\User name\Local Settings\Temp"

    the opening & closing " quotes are necessary because of the spaces in the directory names

    a short 3 command batch file to kill all *.tmp files in the "Temp" directory would look like this

    (insert the correct user name into the path first)

    CD "C:\Documents and Settings\UserName\Local Settings\Temp"

    attrib -r -h -s *.tmp

    del *.tmp

    run that batch
    while windows explorer has that folder open and you'll see the files vanish

    of course it will throw 2 errors if there are no *.tmp files

    you could use the
    if not exist *.tmp

    to check for the files first and then jump if none are there

    (insert the correct user name into the path first)

    ie.

    @Echo off
    CD "C:\Documents and Settings\UserName\Local Settings\Temp"

    if not exist *.tmp goto jump
    attrib -r -h -s *.tmp
    del *.tmp
    goto end
    :jump
    echo.
    echo no .tmp files found
    echo.
    :end

    +
    0 Votes
    neilb@uk

    Create a list of the full path to the folders in a file, each on a separate line, call it what you want. I've used zap.dat in my example.

    Create a batch file with the following commmands:

    FOR /F "delims=" %%i in (path_to\zap.dat) do Del /F /Q "%%i\*.txt"
    FOR /F "delims=" %%i in (path_to\zap.dat) do Del /F /Q "%%i\*.log"

    That should do it. Get the quotes in the correct place.

    :)

  • +
    0 Votes
    Who Am I Really

    to move about in directories
    use the "CD" command

    CD \pathname

    ie.

    the easiest method of getting a path into a batch file is to first go to the location with Windows Explorer

    then copy the contents of the address bar to the notepad window containing the batch file
    being created

    To get into the Temp folder under your User Name

    use the correct path for your version of the OS
    ie. on Win2K / XP it's

    C:\Documents and Settings\User name\Local Settings\Temp

    and to go there from the command window or a batch file it's

    CD "C:\Documents and Settings\User name\Local Settings\Temp"

    the opening & closing " quotes are necessary because of the spaces in the directory names

    a short 3 command batch file to kill all *.tmp files in the "Temp" directory would look like this

    (insert the correct user name into the path first)

    CD "C:\Documents and Settings\UserName\Local Settings\Temp"

    attrib -r -h -s *.tmp

    del *.tmp

    run that batch
    while windows explorer has that folder open and you'll see the files vanish

    of course it will throw 2 errors if there are no *.tmp files

    you could use the
    if not exist *.tmp

    to check for the files first and then jump if none are there

    (insert the correct user name into the path first)

    ie.

    @Echo off
    CD "C:\Documents and Settings\UserName\Local Settings\Temp"

    if not exist *.tmp goto jump
    attrib -r -h -s *.tmp
    del *.tmp
    goto end
    :jump
    echo.
    echo no .tmp files found
    echo.
    :end

    +
    0 Votes
    neilb@uk

    Create a list of the full path to the folders in a file, each on a separate line, call it what you want. I've used zap.dat in my example.

    Create a batch file with the following commmands:

    FOR /F "delims=" %%i in (path_to\zap.dat) do Del /F /Q "%%i\*.txt"
    FOR /F "delims=" %%i in (path_to\zap.dat) do Del /F /Q "%%i\*.log"

    That should do it. Get the quotes in the correct place.

    :)