0

I'm trying to get a script going that looks for the last in a sequence of directories and creates a new directory one more than the last. And then uses xcopy to copy to the new directory. Finally the script is supposed to delete a trailing directory, but it does not work. There seems to be a problem concatenating the directory string name with the loop counter.

@echo off
set target=d:
set dirname=%target%snapshot
set /a term=3
set /a numsnaps=3
set /a last=0

for /l %%i in (1, 1, %term%) do (
  set d=%dirname%%i%
  if exist "%d%\" (
    echo "found dir %d%"
    set /a last=%i%
  ) else echo "not found dir %d%"
)

if %last% lss %term% (
  set /a d=%dirname%%last% + 1
  mkdir "%d%"
  echo "made dir %d%"
  xcopy source "%d%" /e
  set /a j=%i% - %numsnaps%
  set d=%dirname%%j%
  if exist "%d%\" rmdir "%d%"
) else echo Error last snapshot number reached

It outputs:

"found dir "
Missing operand.
...
gornvix
  • 3,154
  • 6
  • 35
  • 74
  • 1
    Why did you chose to use delayed expansion for the `!n!` variable but not the `%d%` variable? Regardless of that, why do you feel the need to create two extra environmental variables when you do not need to? – Squashman Nov 17 '20 at 00:35
  • @Squashman I don't know, I updated my question with my original approach. I want the environment variables as parameters that I can set once at the top of the batch file. – gornvix Nov 17 '20 at 00:42
  • When you say "string variable that will be used later", when later? I ask because outside of the `for` loop the value of `%d%` will only be `foo3`! It would make much more sense to us, if you were to [edit your question](https://stackoverflow.com/posts/64867631/edit), to include, an overview of the task. – Compo Nov 17 '20 at 00:51
  • @Compo Ok, I rewrote my quesiton. – gornvix Nov 17 '20 at 01:05
  • 1
    You are trying to read a variable `%i%` but such is never set; do you mean `%%i` instead? if so, this loop variable (which is something completely different than a normal environment variable, like `%i%`) does no longer exist ouside of the `for /L %%i` loop, but you are using it also in the second code section… – aschipfl Nov 17 '20 at 01:51
  • 2
    Your methodology seems ill advised to me. Why not just create snapshot directory names using the date, in date sortable format. Sort them newest to oldest, skippping the first three, and remove any remaining! – Compo Nov 17 '20 at 01:56
  • @Compo yes, that would be better. However I'm very new to Windows batch programming, so I'm more concerned about learning the syntax and basics to start with. I've got it working now after reading about delayed expansion, but the code in my question is of low quality. So I'm deleting my question. – gornvix Nov 17 '20 at 09:32

1 Answers1

1
@echo off
setlocal enabledelayedexpansion
set dirname=foo
set /a term=3

for /l %%i in (1, 1, %term%) do (
    set "d=%dirname%%%i"
    echo !d!
)
user93353
  • 13,733
  • 8
  • 60
  • 122
  • 2
    Please add some explanations to your answer. Just like we expect complete examples of questions, we also need to provide complete answers. – Gerhard Nov 17 '20 at 06:09