Batch file to print receipts - batch-file

I'm trying to get a program that was left to me to work on another PC, it won't print.
The printer works fine but it won't print using a batch file. There's 2 batch files that I think I need to run, 1 is called "auto_net_use" which contains
Net Use LPT1: \\opr-02\EPSONTM- /PERSISTENT:YES
What does this do? is it the share path? is opr-02 the name of the PC?
Here's the batch file for printing a text file used by the program
#echo off
cd \
set pth=%~dp0
Print "%pth%balance_inquiry.txt"
pause
exit
What is "set pth=%dp0" ?

Net Use LPT1: \\opr-02\EPSONTM- /PERSISTENT:YES is mapping a printer named EPSONTM- that's located on the opr-02 system
set pth=%~dp0 is the current directory where the script was called from. See More Link

Related

Printing ZPL file using Batch File Issue

We run NetSuite for our shipping software and use Zebra ZP450 printer to print the thermal label.
The software downloads a .zpl file which I have assigned to run with a batch file, script here:
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
I have installed this on probably 20 or more computers without an issue. But the last two will initiate the batch file but not print.
I was able to hit pause on the batch file and here is what I got:
C:\users\noah\downloads\net use LPT2: \(ComputerName)\ZebraFedex
The command completed successfully
C:\Users\Noah\Downloads\Copy LPT2
The system cannot find the file specified. 0 files copied
C:\Users\Noah\Downloads>Net use LPT2: /Delete
I've checked my file association, which seems correct since I see the command prompt flash on the screen. I have no idea here...
Working successfully on another computer
Problem on puzzling computer
Here's my batch to transfer file to my venerable TLP2844:
#ECHO OFF
SETLOCAL
FOR %%a IN (%*) DO (
IF EXIST "%%a" (COPY /b "%%~a" \\%computername%\TLP2844
) ELSE (
IF EXIST "%%a.txt" (
COPY /b "%%~a.txt" \\%computername%\TLP2844
) else (
ECHO Neither "%%a" nor "%%a.txt" appears to EXIST
)
)
)
where I have a printer called TLP2844 installed as a ZDesigner TLP 2844 device on USB003.
The problem, as shown by the graphics I've included in your question (saves running off chasing links) is that the label file C:\users\melan\downloads\UPSSHIPPINGLABEL1700448.zpl is being provided to the batch as %1 (first parameter). On the problem installation, no filename (presumably C:\users\Noah\downloads\UPSSHIPPINGLABEL???????.zpl is being provided to the batch as %1.
Therefore it's not a batch problem, but a problem with Netsuite.
From your .pdf, on page 26 :
To install a thermal printer driver on a Windows PC:
...
5. In the Share Name field, enter the name of the printer. For example, LP 2844.
The printer name cannot contain spaces.
So you're told the printer name cannot contain spaces, but the example printer name contains a space....Excellent! (shows my level of confidence in the product, but not actually directly relevant to the problem) :(
Personally, I'd change your batch file to
Copy %1 \\%ComputerName%\ZebraFedex
Perhaps you'd try that after fixing the problem where Netsuite fails to deliver the filename to the batch. Perhaps it's set up to produce .pdfs on the computer in question (That's a wild guess)
In response to reply:
Please change your batch file (temporarily for test purposes) to this:
Echo File to print : "%1"
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
PAUSE
which will report the filename supplied by Netsuite to the batch file on the console window, then keep the window open at the end of the batch until the open window receives a keystroke.
Please report results for both a "working" and a "buggy" installation.
Change .bat file like below, replacing the printer share name with your own. Happened on one of our critical computers that is also the host for FedEx/UPS.
C:\WINDOWS\system32\net.exe use LPT2: \\%ComputerName%\Zebra_ZP500_Shipping_FedEx1
Copy %1 LPT2
C:\WINDOWS\system32\net.exe use LPT2: /Delete
Thank you Noah Schultz for the Pause suggestion for batch file, and to QWERTY for article on Net command issue
Reference

How to setup %UserProfile% in address correctly

If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file

Batch file to run cmd command

I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"

Batch script does not run in temporary folder after decompressing SFX WinRAR file

I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:
#echo off
if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64
:x86
"%cd%"\setup.exe
exit
:x64
"%cd%"\setup-x64.exe
exit
Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!
%cd% reffers to the directory of the call of the batch-file.
For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:
echo %cd%
pause
and you start it for example from the command-line like this:
C:\>%USERPROFILE%\Desktop\Folder\bat.bat
it should echo C:\ as that is where you called it from.
Two ways from the comments to solve the problem:
Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%
or
do not use "%cd%" but "%~dp0"
Both ways use the fact that the zeroth argument of a batch-file is its path.
You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.

How do I locate a batch file

So I am writing a little program in batch, and I am hoping it will be able to run it on other's computers. My cd is to C:\Users\%username%\Desktop\,\thing, but that is not where other people may store it. How can I locate where it is and do a cd to it? Thanks!
%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)
%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.
#echo off
echo %appdata%
CD %appdata%
pause
echo %CD%
pause
Dir
pause
echo "%~dp0%"
pause
CD "%~dp0%"
Dir
cls
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0
pause
Programs and documents can be added to the registry so typing their name without their path in the Start - Run dialog box or shortcut enables Windows to find them.
This is a generic reg file. Copy the lines below to a new Text Document and save it as anyname.reg. Edit it with your programs or documents.
In paths use \\ to seperate folder names in key paths as regedit uses a single \ to seperate it's key names. All reg files start with REGEDIT4. A semicolon turns a line into a comment. The # symbol means to assign the value to the key rather than a named value.
The file doesn't have to exist. This can be used to set Word.exe to open Winword.exe.
This sample add IE.Txt (from IE5) to the registry so typing IE.Txt will open it. I think the file is called IE4.txt in IE4.
REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension>
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\IE.txt]
;The # means the path to the file is assigned to the default value for the key.
;The whole path in enclosed in a quotation mark ".
#="\"C:\\Program Files\\Internet Explorer\\IE.txt\""
;Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry
;Informs the shell that the program accepts URLs.
;"useURL"="1"
;Sets the path that a program will use as its' default directory. This is commented out.
;"Path"="C:\\Program Files\\Microsoft Office\\Office\\"
reg command can read or write to the registry in batch. Register it as if it's an exe file not a batch file.

Resources