Useful scripts

Field note: a nightly export broke after a file share move, and error 1202 was why

A scheduled script that exported accounting data to a mapped drive stopped working after the office's file shares moved. The cause was a stale remembered drive mapping, and the first fix made it worse.

A small office had a nightly batch script that ran a vendor application’s command-line export, dropped the output on a mapped drive, and renamed the .tsv file to .iif so QuickBooks could import it. It had run quietly for years. Then the office’s file shares moved to a new server, and the export files just stopped showing up.

What it looked like

No error anyone would see. The scheduled task reported that it ran, and the morning import folder was empty. The only clue was in the script’s own log file, on the step that mapped the drive:

System error 1202 has occurred.
The local device name has a remembered connection to another network resource.

What was actually wrong

The vendor app writes to a hardcoded drive-letter path, so the script has to map that letter before it runs. It did that the simple way:

if not exist Z:\ net use Z: \\server\share

The account the task runs under still had a remembered (persistent) mapping for that letter, pointing at the old share. The old share was gone, so Z:\ didn’t exist and the script tried to map it. But Windows refuses to map a letter that already has a remembered connection to a different resource. That’s error 1202.

It’s an easy one to miss, because a scheduled task has its own drive mappings. It runs under its own account, usually without an interactive logon. Test the script while you’re logged in and it works fine, because your session has a good mapping. The task’s account doesn’t.

The detour: the first fix made it worse

The first pass at a fix guessed the new share’s path from another drive’s label, then deleted and remapped using that guess. The label was misleading. The “fix” removed a mapping that had been working and replaced it with a wrong one.

Lesson learned: ask Windows where a letter actually points before you hardcode anything.

net use Z:

That prints the real remote path next to “Remote name.”

There was a second detour: a fancier PowerShell step for the rename kept hanging. The plain ren command it replaced had worked for years, so it went back in.

The fix

The script now checks what the letter points to, and fixes it only if it’s missing or wrong:

@echo off
set "SHARE=\\newserver\exports"
set "LOG=C:\Scripts\nightly-export.log"

echo %date% %time% Starting >> "%LOG%"

rem Is Z: mapped to the right share? If not, clear it and remap.
net use Z: 2>nul | find /i "%SHARE%" >nul
if errorlevel 1 (
    net use Z: /delete /y >> "%LOG%" 2>&1
    net use Z: "%SHARE%" /persistent:no >> "%LOG%" 2>&1
)
net use Z: >> "%LOG%" 2>&1

Two more improvements came along with it:

  • It exports the previous day by default, with an optional date argument, so a missed night can be re-run by hand:
set "RUNDATE=%~1"
if "%RUNDATE%"=="" for /f %%d in ('powershell -NoProfile -Command "(Get-Date).AddDays(-1).ToString('dd-MMM-yyyy')"') do set "RUNDATE=%%d"
echo Exporting %RUNDATE% >> "%LOG%"
  • Every step writes its output to the log, so the next failure explains itself.

The share path, log path and date format above are examples. Match them to your environment and to the date format your application expects.

/persistent:no is worth using for script-created mappings: it tells Windows not to remember the connection, so a future share move can’t leave a stale entry behind.

What I took away from it

  • Error 1202 means a stale remembered mapping. Clear it with net use X: /delete /y, then remap.
  • Scheduled tasks have their own mappings. “It works when I run it” doesn’t prove the task’s account is set up the same way.
  • Verify UNC paths with net use, don’t guess from labels or memory.
  • Log every step. The script’s own log was the only thing that pointed at the cause.
  • Keep working simple steps simple. The plain ren beat the clever replacement.

Sources

  • Net use (Microsoft Learn): net use <drive> shows a single connection, /delete removes it, and /persistent controls whether Windows remembers it
  • WNetAddConnection2 (Microsoft Learn): ERROR_DEVICE_ALREADY_REMEMBERED, “The local device name has a remembered connection to another network resource”
  • Error 1202 explanation (Microsoft TechNet): the request is rejected because the drive letter already has a remembered connection to a different resource; the fix is to delete the remembered connection or connect without remembering it

All articles