Command Line Works - .Bat Script Doesn't - windows

When attempting to run the following commands via the cmd prompt on a windows 2008 server everything works successfully. However once those same commands are saved to a .bat or .cmd file the script no longer works.
#echo off
set FILE="\\servername\c$\users\users\desktop\test.txt"
FOR %I in (%FILE%) DO set SIZE=%~ZI
IF %SIZE% == 75 (
copy "\\servername\c$\users\users\desktop\test.txt" "\\servername\c$\users\users\desktop\test-1.txt"
del "\\servername\c$\users\users\desktop\test.txt"
) ELSE (
echo "Failure"
)
TIMEOUT /T -1
I'm not sure what the difference would be and would appreciate any help that can be provided. The text file I'm using has the following text in it.
"
hello goodmorning
I need this to have a file size.
this file size is 75
"

Inside batch scripts, variables for for loops need two % symbols.
FOR %%I in (%FILE%) DO set SIZE=%%~ZI

Related

Run Autocad exe and load script file error

This is My first post in here
I need a help for your side I have made a batch file for run autocad exe and load a script file but give error when I run the batch file
#echo off
set KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools
set VALUE_NAME=installpath
set FN=loadload
set FE=scr
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
set approot=%pInstallDir:~0,-1%
echo %approot%\%FN%.%FE%
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %approot%\%FN%.%FE%
pause
Error: while running batch file autocad opens and in commandline the script file not loading "Xpresslisp.scr": Can't find file."
and bellow one is working
script file loading without getting error
#echo off
set path=%USERPROFILE%
set fol=Documents
set NAME=1
set SUFFIX=scr
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %path%\%fol%\%NAME%.%SUFFIX%
pause
Regarding your second question in the comments...
Bellow command will create the text file and write the first line to it e.g. "some text" like in the command below.
Echo some text > full_path_to_txt_file
Command below will append new text to same file.
Echo some text >> full_path_to_txt_file
'>' char creates file and writes firs line
'>>' char append text
check that %path%\%fol%\%NAME%.%SUFFIX% returns the Full Path to the "Xpresslisp.scr" file !
if it does, inspect the Full Path and see if it contains any white spaces.
if it does, enclose the %path%\%fol%\%NAME%.%SUFFIX% with apostrophes
"%path%\%fol%\%NAME%.%SUFFIX%"
It may be something as simple as blindly removing the last character of the installpath without knowing for sure what it is, (doublequote or backslash?).
As there is unlikely to be multiple copies of any filename in the Xpresslisp Tools tree, I would suggest something like this:
#Echo Off
Set "KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools"
Set "VALUE_NAME=installpath"
Set "FN=loadload"
Set "FE=scr"
(Echo=FILEDIA 0
Echo=(LOAD "C:\\loadmyfile.lsp"^)
Echo=FILEDIA 1)>%FN%.%FE%
For /F "Tokens=2*" %%A In ('Reg Query "%KEY_NAME%" /v "%VALUE_NAME%"') Do (
For /F "Delims=" %%C In ('Dir/B/S/A-D "%%~B"\"%FN%.%FE%" 2^>Nul') Do (
Start "" "%ProgramFiles(x86)%\AutoCAD 2002\acad.exe" /b "%%~C"))
This doesn't care if there is a trailing backslash or not and will only run the AutoCAD command if the file is there.

Cannot access locked file in Windows Batch

I'm having difficulty with a simple Windows script. This examines the current directory, stores the names of the (assumed to be 2) text files in an array, and then runs fc to compare them, piping the output to a text file.
#echo off
setlocal enableDelayedExpansion
IF EXIST comparison.txt del /F comparison.txt
set filename=
set /a N=0
for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set /a N+=1
set v[!N!]=%%a
)
fc %v[1]% %v[2]% > comparison.txt
When I run this I get the error message "the process cannot access the file because it is being used by another process".
I've tried variants where I write the completed fc command line to another BAT file and then call that but with the same result (note that the actual fc code appears to be correct in this—it has the correct file names and output redirection destination). After trying to run the BAT once, I then even get the same error if I pull up an interactive command line and type the fc command manually. This doesn't happen if I type the command without first running the BAT.
This is on Win 7 x64, Version 6.1.7601.

Call file with custom extension in batch

I'm using a second batch file in my main one to save/load variables, like this:
if not exist "KeemyDataPersistence.bat" (
echo #ECHO OFF > KeemyDataPersistence.bat
echo SET GENDER=F >> KeemyDataPersistence.bat
echo SET BGCOLOR=0 >> KeemyDataPersistence.bat
echo SET FGCOLOR=2 >> KeemyDataPersistence.bat
)
call KeemyDataPersistence.bat
It's working fine, but I'd like to save that second file (KeemyDataPersistence.bat) with another extension (.keemy), so KeemyDataPersistence.keemy. It saves fine just by replacing .bat by .keemy in the whole code, but when using call KeemyDataPersistence.keemy, it runs the windows default window to choose the program the user wants to open it with.
How would I be able to make it call the file as a batch file?
Try first to run this script once with admin permissions (and .bat extension):
#echo off
rem :: A files with .keemy extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .keemy=batps
ftype batps=c:\caller "%%1" %*

How to start batch using start command when CMD is disabled?

I am trying to make a batch file which will work on computers with CMD disabled. Below is my code. (The bit saying command is other code which I have not included.)
If I try running this code, a CMD window will open but it will not run because CMD is disabled. It is opening as a CMD file not a batch file. How can I change this? Or could I make the batch file create a .reg file to enable CMD before the start command is used?
#setlocal enableextensions enabledelayedexpansion
for %%f in (%0) do set basename=%%~nf
set UsPr=%UserProfile%
copy %0 "%UsPr%"
if "%0" == "%UsPr%\%basename%.bat" (
goto :1
) else (
goto :2
)
:1
command
pause
:2
command
start "%UsPr%\%basename%.bat"
pause
I'm not sure what you mean by "CMD is disabled". But your START command is failing because your target path is enclosed by quotes (probably by necessity), but START will treat the first argument as a title if it is enclosed by quotes. The solution is to add an additional quoted argument for the title - an empty string will do:
start "" "%UsPr%\%basename%.bat"

Batch Print - Batch Script - findstr condition in for loop

My problem at hand is to download pdf files and send all of them to the printer.
I download via ftp correctly and all the files go into my local directory:
File Name = FtpScript.ftp
open ftp.domain.com
username
password
!:--- FTP commands below here ---
lcd local/Directory
cd /remote/Directory
binary
mget "*.pdf"
prompt
disconnect
quit
This batch file then calls the ftp script.
File Name = retrieve_print.bat
#ftp -i -s:"C:\Scripts\FtpScript.ftp"
set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~-4%
IF NOT EXIST {C:\Users\print_test2\print_%mm%_%yy%}( mkdir C:\Users\print_test2\print_%mm%_%yy% )
IF NOT EXIST C:\Users\print_test2\print_%mm%_%yy%\uploaded_%mm%_%dd%_%yy%.txt (
#echo Uploaded Text -- Date: %date% Time : %time% >> C:\Users\print_test2\print_%mm%_%yy%\uploaded_%mm%_%dd%_%yy%.txt
)
IF NOT EXIST C:\Users\print_test2\print_%mm%_%yy%\printed_%mm%_%dd%_%yy%.txt (
#echo Printed Text -- Date: %date% Time : %time% >> C:\Users\print_test2\print_%mm%_%yy%\printed_%mm%_%dd%_%yy%.txt
)
REM LOOP THROUGH PDFs THAT WERE UPLOADED AND INSERT THE NAMES INTO THE UPLOADED_*_*.txt TEXT FILE
FOR %%x in ( C:\Users\print_test2\print_%mm%_%yy%\*.pdf ) DO (
findstr "%%~nxx" C:\Users\print_test2\print_%mm%_%yy%\uploaded_%mm%_%dd%_%yy%.txt
#ECHO Error level = %errorlevel%
#ECHO %%~nxx
#pause
IF NOT %errorlevel% == 0 (
#echo %%~nxx >> C:\Users\print_test2\print_%mm%_%yy%\uploaded_%mm%_%dd%_%yy%.txt
)
)
REM LOOP THROUGH PDFs THAT WERE UPLOADED AND PRINT THEM, THEN INSERT THEM INTO THE PRINTED_*_*.txt TEXT FILE TO SUPPRESS DUPLICATE PRINTS
FOR %%x in ( C:\Users\print_test2\print_%mm%_%yy%\*.pdf ) DO (
findstr "%%~nxx" C:\Users\print_test2\print_%mm%_%yy%\printed_%mm%_%dd%_%yy%.txt
#ECHO Error level = %errorlevel%
#ECHO %%~nxx
IF NOT %errorlevel% == 0 (
rem PRINT FUNCTION
#echo %%~nxx >> C:\Users\print_test2\print_%mm%_%yy%\printed_%mm%_%dd%_%yy%.txt
)
)
The text files generate incorrectly. My thought is that I could loop through the files available in the print_test2/print_%mm%_%yy% directory for all the files that I received via ftp and log it into a text file.
The problem becomes apparent when I try to run the script a second time when the text files have file names in them. I expect the findstr function to give back an %errorlevel% of 0 , but it does not detect the string that is in the text file and appends all the file names again in both my uploaded and printed text files.
Is there a better way of logging the files received and printing the pdfs only once?
Thanks
Your problem is that the %errorlevel% value is taken inside a for, so it is replaced by the value errorlevel had before enter the for loop. To take the current value that errorlevel have in each for iteration you must use Delayed Variable Expansion, that is, enclose the value in exclamation points instead percents: !errorlevel! AND insert this line at begining of your program:
setlocal EnableDelayedExpansion
To make this problem clearer, try this:
set name=Value before FOR
for %%f in (*.pdf) do (
set name=%%f
echo %name%
)
and then try again changing echo %name% by echo !name!.
A few ideas to consider:
I'm not sure that the errorlevel after your FINDSTR command is going to be non-zero just because the string wasn't found.
Even if errorlevel is non-zero, I think that the moment you execute the next command, the new errorlevel from that command gets set.
In your IF statement, you might need to wrap the two sides of your equality check in delimiters, e.g. IF NOT "%errorlevel%" == "0"
You might consider just making your list distinct after you echo all file names into it. It'll save you some logic on the way in. Some code for making a list distinct in DOS is described here: http://www.dullsharpness.com/2010/10/01/create-a-distinct-ordered-list-from-an-unordered-duplicate-list-using-ms-dos/
If you use techniques from #4, you can then just do a directory listing into your file (as follows) and then make unique using techniques in #4.
dir/b C:\Users\print_test2\print_%mm%_%yy%\*.pdf >> C:\Users\print_test2\print_%mm%_%yy%\printed_%mm%_%dd%_%yy%.txt

Resources