# Batch file



## Sandeep Bhugoo (Jun 19, 2014)

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


----------



## n0tiert (Jun 19, 2014)

for what reason u need to look into it with notepad ?
do you wann do any changes in it ?

there is an spl viewer @ http://splviewer.sourceforge.net/


EDIT: or here http://www.all2pdf.com/spl-batch-converter.htm


----------



## Sandeep Bhugoo (Jun 19, 2014)

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


----------



## n0tiert (Jun 19, 2014)

if you need to clear spooler cache try this:



```
@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:


```
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)


----------



## Sandeep Bhugoo (Jun 19, 2014)

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


----------



## n0tiert (Jun 19, 2014)

does it needs to open a single file ? or multiple in spooler dir

note: it changes file name randomly i think


----------



## Sandeep Bhugoo (Jun 19, 2014)

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...


----------



## n0tiert (Jun 19, 2014)

try some like this (untested)

batch file:


```
@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


----------



## Sandeep Bhugoo (Jun 19, 2014)

NICE PAL....WORKING FINE BUT WHEN I CLOSE THE NOTEPAD IT CLOSES THE CMD SCREEN AS WELL...


----------



## n0tiert (Jun 19, 2014)

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


----------



## Sandeep Bhugoo (Jun 19, 2014)

works great...thanks a lot

cheers


----------



## n0tiert (Jun 19, 2014)

np m8


----------



## n0tiert (Jun 19, 2014)

```
@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


----------



## Shambles1980 (Jun 20, 2014)

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.


```
@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
```


----------



## Drone (Jun 21, 2014)

You can make code even shorter. You don't need to type %windir%\system32 because *system32* alone is a recognized environment variable


----------



## n0tiert (Jun 22, 2014)

this is more shorter, the other where to reach the goal


```
@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


----------



## n0tiert (Jun 22, 2014)

Drone said:


> You can make code even shorter. You don't need to type %windir%\system32 because *system32* alone is a recognized environment variable



Reffering to %SYSTEM% or %SYSTEM32% leads to file or folder not found
in my case not set as enviroment variable


----------



## Drone (Jun 22, 2014)

n0tiert said:


> Reffering to %SYSTEM% or %SYSTEM32% leads to file or folder not found
> in my case not set as enviroment variable


did you try without %s? Just system32?


----------



## n0tiert (Jun 22, 2014)

Drone said:


> did you try without %s? Just system32?



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


----------

