Scheduled batch file error (0xff) - Executing normal it work fine

4.4k Views Asked by At

I've this batch file to execute.

@echo off 
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto1.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto1.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto1.txt
echo %var% >> urlfoto1.txt
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto2.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto2.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto2.txt
echo %var% >> urlfoto2.txt
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "foto3.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "foto3.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.%var%
break > urlfoto3.txt
echo %var% >> urlfoto3.txt

This script is generating random jpg url from a list on a file and create a txt new file with 1 random jpg url.

When I exectute it manually, it work fine. But when I go to scheduling it with Windows Task Scheduler I retrieve a (0xFF) ERROR. What does (0xff) mean? And why the scheduling is not working?

2

There are 2 best solutions below

0
On BEST ANSWER

And similarly to LotPings answer:

@Echo Off
CD /D "%~dp0"
For %%A In (1 2 3) Do Call :Sub "%%A"
Pause
GoTo :EOF

:Sub
For /F "Delims==" %%A In ('Set line[ 2^>Nul') Do Set "%%A=" 
Set "total="&Set "randlinenum="
For /F "Tokens=1*Delims=[]" %%A In ('Find /V /N ""^<"foto%~1.txt"'
)Do Set "line[%%A]=%%B"&Set "total=%%A"
Set /A randlinenum=1+(%RANDOM% %% total)
SetLocal EnableDelayedExpansion
Echo(!line[%randlinenum%]!
(Echo(!line[%randlinenum%]!)>"urlfoto%~1.txt"
EndLocal
Exit /B

Please note that the Find /V /N "", (or FindStr /N "^"), construct will also include any empty lines, so if those exist in any of your foto* files this could feasibly randomly select and output an empty line.

0
On
  • I don't like the redundancy in your batch, use a counting for /l and a call :sub
  • using a find /c is simpler to get the line count of a file.
  • using more +x avoids the need to differentiate the skip count.
  • when outputting only one line, directly use > to overwrite previous content.

Here using %~dp0 (batch folder) for input/output.

:: Q:\Test\2019\05\02\SO_55951454.cmd
@echo off 
cd /d "%~dp0"
for /L %%L in (1,1,3) do Call :Sub %%L
Goto :Eof

:Sub
for /f %%i in ('find /C /V "" ^<"foto%1.txt"') do set count=%%i
set /a "rd=%random% %% count"
for /f "delims=" %%i in ('more +%rd% "foto%1.txt"') do set "var=%%i"&goto :cont
:cont
echo.%var%
>"urlfoto%1.txt" echo.%var%
Goto :Eof