• scheduled task: batch file doesn't run

    From Ammammata@ammammata@tiscali.it to alt.msdos.batch on Fri Jun 14 09:48:24 2024
    From Newsgroup: alt.msdos.batch

    OS: Windows Server 2008 R2

    According to this web sites, https://stackoverflow.com/questions/5085116/windows-2008-server-task-scheduler-does-not-run-bat-batch-job,
    there's a bug that prevent a batch file *with quotes inside* to be
    executed

    the link provides three possible solutions:

    1.
    create a launcher.bat (without quotes inside) that calls the proper
    batch file

    2. set the "Starting folder" in the parameters and remove the full path
    in the "command"

    3.
    apply a patch http://support.microsoft.com/kb/951246 but is not
    available any more


    the task is executed as administrator, both manually (it works) and
    scheduled (it doesn't)


    this is the batch file, that runs correctly when manually executed, as
    I wrote above

    note the quotes "" in the command for

    rem ------------------------ start
    D:
    cd \TempBackup

    @echo off
    for /f "tokens=2 delims==" %%i in ('wmic path win32_localtime get
    dayofweek /value') do set dow=%%i

    xcopy \\192.168.42.54\as400\SV0%dow%\*.* d:\TempBackup\SV /c /f /h /y
    /j

    rem CURL

    rem hidden parameters for privacy
    SET FTP_URL=xxx.yyy.zzz.www
    SET USERNAME=ftpuser
    SET PASSWORD=********
    SET REMOTE_DIR=/disk1/CompanyName/%dow%/
    SET FOLDER_PATH=d:\TempBackup\SV

    REM Iterate over each file in the folder and upload it using curl
    FOR %%F IN ( %FOLDER_PATH%\*.* ) DO curl -T %%F ftp://%USERNAME%:%PASSWORD%@%FTP_URL%%REMOTE_DIR% --ftp-create-dirs

    echo All files processed.

    del /q %FOLDER_PATH%\*.*
    rem ------------------------ end

    any further help is appreciated

    have a nice weekend
    --
    /-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
    -=- -=- -=- -=- -=- -=- -=- -=- - -=-
    ........... [ al lavoro ] ...........
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From JJ@jj4public@outlook.com to alt.msdos.batch on Sat Jun 15 16:12:35 2024
    From Newsgroup: alt.msdos.batch

    On Fri, 14 Jun 2024 09:48:24 +0200, Ammammata wrote:
    OS: Windows Server 2008 R2

    According to this web sites, https://stackoverflow.com/questions/5085116/windows-2008-server-task-scheduler-does-not-run-bat-batch-job,
    there's a bug that prevent a batch file *with quotes inside* to be
    executed

    the link provides three possible solutions:

    1.
    create a launcher.bat (without quotes inside) that calls the proper
    batch file

    2. set the "Starting folder" in the parameters and remove the full path
    in the "command"

    3.
    apply a patch http://support.microsoft.com/kb/951246 but is not
    available any more

    the task is executed as administrator, both manually (it works) and scheduled (it doesn't)

    this is the batch file, that runs correctly when manually executed, as
    I wrote above

    note the quotes "" in the command for

    rem ------------------------ start
    D:
    cd \TempBackup

    @echo off
    for /f "tokens=2 delims==" %%i in ('wmic path win32_localtime get
    dayofweek /value') do set dow=%%i

    xcopy \\192.168.42.54\as400\SV0%dow%\*.* d:\TempBackup\SV /c /f /h /y
    /j

    rem CURL

    rem hidden parameters for privacy
    SET FTP_URL=xxx.yyy.zzz.www
    SET USERNAME=ftpuser
    SET PASSWORD=********
    SET REMOTE_DIR=/disk1/CompanyName/%dow%/
    SET FOLDER_PATH=d:\TempBackup\SV

    REM Iterate over each file in the folder and upload it using curl
    FOR %%F IN ( %FOLDER_PATH%\*.* ) DO curl -T %%F ftp://%USERNAME%:%PASSWORD%@%FTP_URL%%REMOTE_DIR% --ftp-create-dirs

    echo All files processed.

    del /q %FOLDER_PATH%\*.*
    rem ------------------------ end

    any further help is appreciated

    have a nice weekend

    You misunderstood the problem. That problem has nothing to do with the
    problem in your batch file.

    The KB951246 problem lies at the `schtasks.exe` program. Not at the code
    within the scheduled batch file itself. And it doesn't matter whether the scheduled program is a batch file or not.

    https://mskb.pkisolutions.com/kb/951246

    e.g. if the program command line which need to be scheduled is supposed to
    be run like below.

    "d:\my scripts\test.bat" /z "quoted arg"

    The command line for creating the scheduled task using schtasks is supposed
    to be like below.

    schtasks /create /sc daily /tn test /tr "\"d:\my scripts\test.bat\" /z
    \"quoted arg\""

    However due to the bug in schtasks, the command line of the scheduled
    program of the created scheduled task, ends up like below.

    d:\my scripts\test.bat /z "quoted arg

    Your problem is an entirely different problem. But since you didn't mention
    how exactly, your batch file "doesn't work", I could only point out the code which have the potential of causing the problem - which is the curl's
    command line for the `FOR` command. i.e.: (long text warning)

    curl -T %%F ftp://%USERNAME%:%PASSWORD%@%FTP_URL%%REMOTE_DIR% --ftp-create-dirs

    Here... if the file/folder enumerated by the `FOR` command contains a space
    or special character such as `&`, curl won't be executed as inteaded. To fix it, the `%%F` must be double-quoted. i.e.:

    curl -T "%%F" ftp://%USERNAME%:%PASSWORD%@%FTP_URL%%REMOTE_DIR% --ftp-create-dirs

    In Windows, when specifying a file system path, it should always be double-quoted if it's not already double-quoted. Especially if the path is unknown, or may vary, or can be anything.

    Another example of the need of double-quoting is when your `FOLDER_PATH` variable contains a space or special character. e.g. if it's:

    SET FOLDER_PATH=d:\Temp Backup\SV

    Then below code line will not work as expected:

    del /q %FOLDER_PATH%\*.*

    Because it's expanded as:

    del /q d:\Temp Backup\SV\*.*

    The folder path must be double-quoted like below.

    del /q "d:\Temp Backup\SV\*.*"

    Thus the code line in the batch file must be like below.

    del /q "%FOLDER_PATH%\*.*"
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Ammammata@ammammata@tiscali.it to alt.msdos.batch on Mon Jun 17 09:31:34 2024
    From Newsgroup: alt.msdos.batch

    JJ brought next idea :
    You misunderstood the problem. That problem has nothing to do with the problem in your batch file.

    Your problem is an entirely different problem. But since you didn't mention how exactly, your batch file "doesn't work"

    my fault :(

    checking the event log, I see 7 entries:
    (translated into English)

    - activity activated by scheduler
    - message received (credentials)
    - activity activated
    - action activated (the batch file)
    - activity process created (cmd.exe)
    - action completed (cmd.exe)
    - activity completed

    The scheduled task completes in 1 second and of course nothing happens:
    no copy from NAS to local folder, no upload to remote disk with curl,
    the LAN/WAN are NOT so fast


    Here... if the file/folder enumerated by the `FOR` command contains a space or special character such as `&`, curl won't be executed as inteaded. To fix it, the `%%F` must be double-quoted. i.e.:

    curl -T "%%F" ftp://%USERNAME%:%PASSWORD%@%FTP_URL%%REMOTE_DIR% --ftp-create-dirs


    Hi

    I would try this one first, but note that among the 37 files I must
    transfer, there are 25 whose name begins with $$; the rest are only
    letters and digits, no spaces, no hyphen, no underscore, nothing else
    that's not [A-Z0-9]

    In Windows, when specifying a file system path, it should always be double-quoted if it's not already double-quoted. Especially if the path is unknown, or may vary, or can be anything.


    for what concerns the path, I strongly avoided all spaces and non
    alphanumeric characters, why should I complicate my life when I can
    keep things simple?

    as I stated, the batch file runs without errors when manually executed

    I also made a single-day version, removing the for /f "tokens=2
    delims==" %%i in... command and manually setting dow=1 (day of week,
    1=Monday)

    well, it works (manual) and doesn't (schedule)


    at this point I'll search a third party tool to bypass the scheduler
    --
    /-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
    -=- -=- -=- -=- -=- -=- -=- -=- - -=-
    ........... [ al lavoro ] ...........
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Ammammata@ammammata@tiscali.it to alt.msdos.batch on Mon Jun 17 10:35:56 2024
    From Newsgroup: alt.msdos.batch

    Ammammata brought next idea :
    at this point I'll search a third party tool to bypass the scheduler

    testing "Advanced Task Scheduler" version 9.0 Build 2307

    it works fine, I made a shorter test right now; tomorrow I'll check
    how's gone the full backup
    --
    /-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
    -=- -=- -=- -=- -=- -=- -=- -=- - -=-
    ........... [ al lavoro ] ...........
    --- Synchronet 3.21d-Linux NewsLink 1.2