1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Batch file

Discussion in 'Programming & Webmastering' started by Sandeep Bhugoo, Jun 19, 2014.

  1. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    hello,

    i am not so good in creating batch files i need some help in creating one.

    in the following path

    C:\Windows\System32\spool\PRINTERS

    there is a .SHD and .SPL file created when prints launched

    i need a batch file to open the .SPL in notepad first then on closing the notepad opened, all the files .SHD and .SPL are permanently deleted

    any help please

    code to open the notepad is as below

    start notepad "C:\windows\system32\spool\printers\FP00002.SPL"

    the file is opened but there are unwanted characters that appear as well as below

    upload_2014-6-19_10-43-2.png
     
  2. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
  3. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    in fact when i reset a user password it is supposed to print the generated password to a printer but i don't want to print it. i want the batch file to open the .spl file generated in notepad, i note the password then on closing the notepad, the files .shd and .spl are deleted permanently from the folder
     
  4. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    if you need to clear spooler cache try this:


    Code:
    @Echo OFF
    CLS
    net stop spooler
    del c:\system32\spool\printers\*.shd
    del c:\system32\spool\printers\*.spl
    net start spooler
    much nicer using %windir% variable:

    Code:
    net stop spooler
    del %windir%\system32\spool\printers\*.shd
    del %windir%\system32\spool\printers\*.spl
    net start spooler
    i donĀ“t think you can read it as plain text
    also here is an emf/spl viewer
    http://www.fengyuan.com/download.html (emf.zip)
     
    Last edited: Jun 19, 2014
  5. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    thank you for your reply. how to merge the codes so that the batch file does this

    the batch file displays the options below

    1. open the *.spl file in notepad
    2. delete the *.shd and *.spl files
    3.exit

    commands execute as options are selected
     
  6. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    does it needs to open a single file ? or multiple in spooler dir

    note: it changes file name randomly i think
     
  7. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    it has to open any .spl file present. each time the folder will be cleared when choosing the second option to clear. when another generation is done, another .spl file is created. i run the batch file to open it again when choosing option 1. ya the file name changes each time...
     
  8. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    try some like this (untested)

    batch file:

    Code:
    @EchoOFF
    CLS
    :MENU
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    :SPL
    for %%X in ("%windir%\system32\spool\printers\"*.spl) do notepad %%X
    GOTO MENU
    :CLEAR
    net stop spooler
    del %windir%\system32\spool\printers\*.shd
    del %windir%\system32\spool\printers\*.spl
    net start spooler
    GOTO MENU


    save as *.bat


    fixed :MENU tag
     
    Last edited: Jun 19, 2014
    Steevo says thanks.
  9. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    NICE PAL....WORKING FINE BUT WHEN I CLOSE THE NOTEPAD IT CLOSES THE CMD SCREEN AS WELL...
     
  10. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    recopy script added the :menu tag and it should jump back to menu

    might also be run as admin to have permission of deleting files in spooler dir
     
  11. Sandeep Bhugoo New Member

    Joined:
    Jun 19, 2014
    Messages:
    6 (0.01/day)
    Thanks Received:
    0
    works great...thanks a lot

    cheers
     
  12. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    np m8
     
  13. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    Code:
    @ECHO OFF
    CLS
    :MENU
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    :SPL
    if exist {"%windir%\system32\spool\printers\"*.spl} (
            ECHO .
            ECHO Files found, opening...
            ECHO.
            for %%X in ("%windir%\system32\spool\printers\"*.spl) do notepad %%X
            GOTO MENU
        ) else (
            ECHO.
            ECHO No Files found...
            ECHO.
            GOTO MENU
        )
    :CLEAR
    if exist {"%windir%\system32\spool\printers\"*.spl} (
            ECHO .
            ECHO Files found, deleting...
            ECHO.
            net stop spooler
            del %windir%\system32\spool\printers\*.shd
            del %windir%\system32\spool\printers\*.spl
            net start spooler
            GOTO MENU
        ) else (
            ECHO.
            ECHO No Files found...
            ECHO.
            GOTO MENU
        )
    :EOF
    exit

    a bit nicer, added some file checks and notes
     
  14. Shambles1980

    Joined:
    May 3, 2014
    Messages:
    559 (0.69/day)
    Thanks Received:
    106
    dont need to cls the @Echo off and its advisable to @Echo on at the end of a batch file just before it exits.. "but next to no one does it"

    i assume your doing cls so a new menu will be clean though, which keeps the bat nicer looking. rather than having a cls at the end of every option.
    I think your way looks a lot cleaner than i would have done.. and i d have probably ended up with something looking a lot more like this.
    Im prety sure your way is probably the better way the more i think about it. but id have still done it the way im adding just to ensure that it always ended at the menu.
    I dont see any reason why your way wouldnt jump back to the menu though. so your way has to be better.

    Code:
    @ECHO OFF
    :MENU
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    :SPL
    if exist {"%windir%\system32\spool\printers\"*.spl} (
    ECHO .
    ECHO Files found, opening...
    ECHO.
    for %%X in ("%windir%\system32\spool\printers\"*.spl) do notepad %%X
    pause
    cls
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    ) else (
    ECHO.
    ECHO No Files found...
    pause
    cls
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    )
    :CLEAR
    if exist {"%windir%\system32\spool\printers\"*.spl} (
    ECHO .
    ECHO Files found, deleting...
    ECHO.
    net stop spooler
    del %windir%\system32\spool\printers\*.shd
    del %windir%\system32\spool\printers\*.spl
    net start spooler
    ECHO deleted
    pause
    cls
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    
    ) else (
    ECHO.
    ECHO No Files found...
    pause
    cls
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open *.spl Files
    ECHO 2 - Clear Spooler cache
    ECHO 3 - EXIT
    ECHO.
    ECHO ...............................................
    ECHO.
    
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO SPL
    IF %M%==2 GOTO CLEAR
    IF %M%==3 GOTO EOF
    
    )
    :EOF
    cls
    pause
    @Echo on
    
     
    Last edited: Jun 20, 2014
  15. Drone

    Drone

    Joined:
    Sep 1, 2010
    Messages:
    4,719 (2.19/day)
    Thanks Received:
    4,455
  16. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    this is more shorter, the other where to reach the goal

    Code:
    @ECHO OFF
    REM Set File Path variable
    SET SPLFILES=%WINDIR%\SYSTEM32\spool\printers\
    REM Build Menu
    :MENU
        CLS
        ECHO ...............................................
        ECHO.
        ECHO 1 - Open *.spl Files
        ECHO 2 - Clear Spooler cache
        ECHO 3 - EXIT
        ECHO.
        ECHO ...............................................
        ECHO.
        SET /P M=Type 1, 2, or 3 then press ENTER:
        IF %M%==1 GOTO SPL
        IF %M%==2 GOTO CLEAR
        IF %M%==3 GOTO EOF
    REM check if there are any and open files with notepad
    :SPL
        REM "FORFILES" Native command in Vista/Windows7/2008, via Resource Kit for XP
        for %%G in (.spl) do FORFILES -p %SPLFILES% -m  *%%G -c "cmd /c ECHO Opening File: @path & cmd /c notepad @path"
        ECHO.
        pause
        goto MENU
    REM check if there are any stop spooler , delete files, start spooler
    :CLEAR
        net stop spooler
        ECHO.
        REM "FORFILES" Native command in Vista/Windows7/2008, via Resource Kit for XP
        for %%G in (.shd, .spl) do FORFILES -p %SPLFILES% -m  *%%G -c "cmd /c ECHO Deleting File: @path & cmd /c del @path /Q"
        ECHO.
        net start spooler
        pause
        goto MENU
    REM Exit Script
    :EOF
        exit
    
    have fun
     
    Last edited: Jun 22, 2014
  17. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    Reffering to %SYSTEM% or %SYSTEM32% leads to file or folder not found
    in my case not set as enviroment variable
     
  18. Drone

    Drone

    Joined:
    Sep 1, 2010
    Messages:
    4,719 (2.19/day)
    Thanks Received:
    4,455
    did you try without %s? Just system32?
     
  19. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    1,037 (0.34/day)
    Thanks Received:
    562
    Location:
    Frankfurt/Main - Germany
    nope not working,
    you could do some like this to set Env Variable

    setx -m System32"%windir%\System32\"

    for current User

    but its similar to my:

    SET SPLFILES=%WINDIR%\SYSTEM32\spool\printers\


    type: set in commad-line to see your system env variables
     
    Last edited: Jun 22, 2014
    Drone says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)