0

I have the following batch file to download backups from our server:

@echo off
d:
cd \DBBackups
curl -u user:password https://example.com/Backups/list.php?p=anotherpassword >list.txt
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ list.txt"`) do (
    echo =========================================
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    set "tmp=%%a"

    set "_tempvar=YesYes"
    if !tmp:~-3!==tar set "_tempvar=1"
    if !tmp:~-3!==bak set "_tempvar=1"
    if !_tempvar! EQU 1 (
        if exist !var! (
            echo !var! has already been downloaded
          ) else (
            echo Downloading !var!
        )
        if not exist !var! wget --no-check-certificate --user=user --password=yap https://www.example.com/Backups/!var!)
    )
    ENDLOCAL
)
c:
echo "Drive changed!"

The file receives a list of files (one file per line) and saves them to list.txt. Then it reads each line and downloads the file. At the end, changing the drive back to c: fails.

I thought it maybe that the ERRORLEVEL is being set to greater than 0. So I've tried the solutions presented here including "cmd /c "exit /b 0"", "cd .", and "ver > nul" but they don't work. The echo at the end "Drive changed!" 0 does print.

Any suggestions welcome.

Chiwda
  • 1,233
  • 7
  • 30
  • 52
  • 1
    Note that `cd` has the switch `/D` which also changes drives. So you could have done `cd /d d:\DBBackups`. Even better, you can use `pushd d:\DBBackups` and then `popd` at the end to return to the previous directory. As for the issue of the script seemingly terminating early, that's odd. I do notice an extra close-parenthesis at the end of the line beginning `if not exist`. Is that intentional? It may be messing up the `setlocal` and `endlocal` calls. – paddy Jul 19 '23 at 05:13
  • Also note that you must escape the exclamation mark in your last `echo` call: `echo Drive changed^!` – paddy Jul 19 '23 at 05:17
  • Certainly seems strange. Please add `echo ON` and `echo %CD%` on the 2 lines before `c:` to see whether the `c:` is actually executed as expected. I'd check that the file has been saved as ANSI. – Magoo Jul 19 '23 at 05:28
  • @paddy thanx for pointing out the misplaced ")" - don't know how it got in there. I removed it and there is no change in behavior. As for the popd and pushd sequence, I had tried it long ago - forgot to mention it. It's even there in the comments of my real batch file (not the one I posted here). – Chiwda Jul 19 '23 at 05:35
  • When all else fails, try wrapping your batch file in another batch file and `call` it. The outer batch file can do a `pushd` and `popd` surrounding that. – paddy Jul 19 '23 at 05:37
  • @Magoo %CD% with echo on echoes as follows: "d:\DBBackups>echo d:\DBBackups d:\DBBackups" (That's echoing the command and then printing the current directory) – Chiwda Jul 19 '23 at 06:44
  • If I put echo %CD% after the line with c:, it shows the correct directory i.e. "C\Users\My Name". But the prompt is stubbornly on d:\DBBackups! – Chiwda Jul 19 '23 at 07:04
  • 3
    Since you execute `D:` then a `cd`, the current directory when the `setlocal` is executed is `d:\DBBackups`. This directory will be restored when the batch ends (an implicit `endlocal`). You need to add an `endlocal` command before the `c:` – Magoo Jul 19 '23 at 08:01
  • Beautiful! That worked. If you want to put that as an answer, I will accept it. – Chiwda Jul 19 '23 at 11:55

1 Answers1

1

Since you execute D: then a cd, the current directory when the setlocal is executed is d:\DBBackups.

This directory will be restored when the batch ends (an implicit endlocal). You need to add an endlocal command before the c:

Magoo
  • 77,302
  • 8
  • 62
  • 84