batch-filescheduled-taskswindows-sbs

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


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?


Solution

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