Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change variable value inside loop in batch

I use the below code in order to create a directory from the value of the first 4 chars of collector text file

@echo off & setlocal enabledelayedexpansion

:loop

set /P txt_file=<Collector.txt
Set collector_id=!txt_file:~0,4!

:: check for existence of [OutputFolder]
:: if [OutputFolder] doesn't exist, create it
if not exist %collector_id% (
  echo folder %collector_id% not found
  echo creating folder %collector_id%
  md %collector_id% 
  )

xcopy *.txt %collector_id% /v 
Del *.txt

goto loop

I want to execute the above loop continuously in order to check if current directory is empty or not. If not I want to make a dir, if does not exist, with name the first 4 chars of collector.txt.

If the directory is not empty everything is ok. When the above is looping and I add collector.txt to the current directory the collector_id does not change.

Where am I wrong?

Is there any other way, expect infinite, loop to do this?

like image 562
kosbou Avatar asked Dec 27 '22 11:12

kosbou


1 Answers

Put setlocal EnableDelayedExpansion at the start, and use !var! instead of %var%. Then it is evaluated every time.

like image 169
barfuin Avatar answered Jan 26 '23 00:01

barfuin