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?

1
  • Also see this question. Commented Jan 17, 2013 at 18:38

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

You already have the EnableDelayedExpansion, I just noticed, but this way the answer is more complete for other readers I hope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.