4

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?

kosbou
  • 3,919
  • 8
  • 31
  • 36
  • Also see [this question](http://stackoverflow.com/questions/1199931/how-expand-a-cmd-shell-variable-twice-recursively). – barfuin Jan 17 '13 at 18:38

1 Answers1

10

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

barfuin
  • 16,865
  • 10
  • 85
  • 132
  • 2
    You already have the `EnableDelayedExpansion`, I just noticed, but this way the answer is more complete for other readers I hope. – barfuin Jan 17 '13 at 18:33