3

As part of a regular file upload process we run a .bat file via Windows Task Scheduler. It opens WinSCPand runs it using a config file.

Then it cds to the file where the upload is stored, renames it, then moves it to the archive file.

If I run the program manually with a pause before the exit, it works fine. Currently is just dumping the file from upload to the archive without renaming it with time and date appended.

@echo off

"C:\Program Files (x86)\WinSCP\winscp.com" /script=CONFIG.txt

cd C:\SCHEDULEDQUERIES\PressGaney\Upload

ren *.csv CL6019_%time:~0,2%%time:~3,2%%date:~-10,2%%date:~3,2%%date:~-4,4%.csv

move *.csv C:\SCHEDULEDQUERIES\PressGaney\archive

exit

Thanks. Happy to give any further details that may be needed.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ya Guy Godzilla
  • 107
  • 1
  • 12
  • I would suggest you change, `%date:~3,2%` to `%date:~-7,2%` to maintain the same format. – Compo Feb 20 '18 at 22:29

2 Answers2

3

For lack of further information, I'd suggest

ren *.csv "CL6019_%time:~0,2%%time:~3,2%%date:~-10,2%%date:~3,2%%date:~-4,4%.csv"

should cure the problem. If not, echo this line and then pause the script.

Perhaps your time format - or the time format used by the by the user under which the job is being run by the task scheduler - is set to single-digit hours, which replaces the leading 0 in the time with a space, so the original ren function sees three arguments, not two.

Of course, f you try to debug this during normal working hours, after morning coffee at 10:00 or later, the time won't contain the space, so it seems to work with your tests.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

Wrap the batch file to another one and redirect its complete output to a log file:

winscp_script.bat > c:\writable\path\winscp_script.log

Next day, inspect the log file for any errors.


In general, you should not rely on %TIME% and %DATE% variables, as their format is locale specific. The local account that runs your Windows Scheduler task can have a different locale than the one you use to test the batch file. Not only you get a wrong name, but if the resulting format includes spaces, it would completely break the ren command (as already suggested by @Magoo).

WinSCP itself has a built-in feature for time formatting, so you can do something like:

set TIMESTAMP_FORMAT=hhnnddmmyyyy

pushd "C:\Program Files (x86)\WinSCP"
for /F "tokens=* USEBACKQ" %%F in (
    `winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"`
) do set TIMESTAMP=%%F
popd

echo %TIMESTAMP%
ren *.csv CL6019_%TIMESTAMP%.csv
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • thank you very very much. i had not considered the impact of Local time on my programs. this one should never be an issue since it is just an archive running on a single server in the next room. but in the future I'll definitely go by UTC. I'll try out the timestamp feature on my next project [link](http://www.creativedeletion.com/2015/08/07/why-not-to-use-server-local-time.html) < – Ya Guy Godzilla Feb 21 '18 at 19:40
  • By "locale" I do not mean timezone (though that's indeed an issue too). I mean locale time format. The account you are using may have for example UK time format `dd/mm/yyyy`, while the Scheduler account may have US time format `mm/dd/yyyy` or yet another. Time format would differ too, between 12- and 24- hour systems. – Martin Prikryl Feb 21 '18 at 19:51
  • ah i understand! – Ya Guy Godzilla Feb 21 '18 at 20:03