HELP!!!!

Discussion in 'Scripting' started by safm, Apr 16, 2021.

  1. safm

    safm MDL Junior Member

    Dec 29, 2007
    50
    4
    0
    #1 safm, Apr 16, 2021
    Last edited by a moderator: Jun 29, 2021
    Hello everyone,

    I wrote this batch file to install x printers in x computers.

    Code:
    CLS
    set here=%~dp0
    for /f "tokens=1" %%a in (%here%\computers.txt) do (
        CLS
        ECHO OFF
        ping -n 1 %%a | find "TTL=" >nul
        if errorlevel 1 (
        echo %%a is Offline >> %here%\log.txt
        ) else (echo %%a >> %here%\compok.txt)
        )
    CLS
    for /f "tokens=1" %%b in (%here%\compok.txt) do (
        for /f "tokens=1,2" %%g in (%here%\printers.txt) do (
            CLS
            ECHO OFF
            echo cscript c:\windows\system32\printing_admin_scripts\en-us\prndrvr.vbs -a -m "Canon Generic Plus PCL6" -v 3 -e "Windows x64" -i "c:\temp\printer\c64\CNP60MA64.INF" -h "c:\temp\printer\c64" >  %here%\pc_cscript.cmd
            echo cscript c:\windows\system32\printing_admin_scripts\en-us\prnport.vbs -a -r "IP_%%h" -h %%h -o raw -n 9100 -me -s %%b > %here%\port.cmd
            echo cscript c:\windows\system32\printing_admin_scripts\en-us\prnmngr.vbs -a -s %%b -p "CANON_%%g" -m "Canon Generic Plus PCL6" -r "IP_%%h" > %here%\print.cmd
            robocopy /s %here%\Canon\ \\%%b\c$\temp\printer
            %here%\psexec \\%%b -c -f -h  %here%\pc_cscript.cmd
            %here%\port.cmd
            %here%\print.cmd
            echo %%g IP %%h sucessfully installed in %%b >> %here%\log.txt
            del /F /Q %here%\pc_cscript.cmd
            del /F /Q %here%\port.cmd
            del /F /Q %here%\print.cmd
            pause
        )
        )
    REM LOG
    CLS
    type %here%\log.txt
    del /F /Q %here%\log.txt
    del /F /Q %here%\compok.txt
    Pause
    
    Install everything as i wanted, but at the then after processo the last printer in last computer, it exits without ruing the finals commands below the "REM LOG"

    What i'm doing wrong?

    Thanks
     
  2. Tux 528

    Tux 528 MDL Novice

    Apr 20, 2020
    9
    11
    0
    #2 Tux 528, May 17, 2021
    Last edited: May 17, 2021
    Hi!

    First of all, you don't need to add the "CLS" and "ECHO OFF" commands to your script all the time; simply write them at the beginning. Secondly, it is better to enclose variables and file paths in quotation marks in case they contain spaces or special characters.

    I noticed that the variable "%%h" is not defined in your script because no FOR loop specifies this variable name. This is certainly the cause of the bug you encountered. Unfortunately, I can't determine what these variables correspond to, that's why I indicated them in parentheses. Don't forget to fix this problem before running this script. :animatedwink:

    Finally, it's not necessary to create temporary log files or scripts during the installation process; you can simply run the VBScripts or display information when you wish.

    Here is your code that I have cleaned up and optimized.
    Code:
    @ECHO OFF
    set "here=%~dp0"
    for /f "tokens=1" %%a in ("%here%\computers.txt") do (
        ping -n 1 %%a | find "TTL=" > nul || echo %%a is Offline
        for /f "tokens=1,2" %%g in ("%here%\printers.txt") do (
            robocopy /s "%here%\Canon" \\%%a\c$\temp\printer
            "%here%\psexec" \\%%a -i -h cscript "c:\windows\system32\printing_admin_scripts\en-us\prndrvr.vbs" -a -m "Canon Generic Plus PCL6" -v 3 -e "Windows x64" -i "c:\temp\printer\c64\CNP60MA64.INF" -h "c:\temp\printer\c64"
            cscript "c:\windows\system32\printing_admin_scripts\en-us\prnport.vbs" -a -r "IP_(%%h)" -h (%%h) -o raw -n 9100 -me -s %%a
            cscript "c:\windows\system32\printing_admin_scripts\en-us\prnmngr.vbs" -a -s %%a -p "CANON_%%g" -m "Canon Generic Plus PCL6" -r "IP_(%%h)"
            echo %%g IP (%%h) sucessfully installed in %%a
            pause
        )
    )
    Feel free to let me know if you encounter any other problems with this script. :)
     
  3. andyken

    andyken MDL Novice

    May 6, 2017
    3
    2
    0
    wow amazing Tux, the code so neat
     
  4. Tux 528

    Tux 528 MDL Novice

    Apr 20, 2020
    9
    11
    0