Link to home
Create AccountLog in
Avatar of Rebel_no_1
Rebel_no_1Flag for China

asked on

How do I set an environment variable equal to the first folder inside another folder which file name does not contain the word "Done"

I have a data folder containing 1000 unique folders. I would like to determine the first folder name inside this directory and set this as an environment variable. Several actions will then be performed on this folder and once complete this folder while be renamed with a suffix "-Done".

The idea is to loop this and have the dos batch script run through each of the folders one at a time.

I only need to know: how to set the first folder name not containing "-Done" to an environment variable.

Any help would be greatly appreciated.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I think this should be a good starter to list "all" folders :
dir /AD | find /V"." | find /V"-Done"

I think the rest is easy to do?
Avatar of oBdA
oBdA

Try this:
@echo off
setlocal enabledelayedexpansion
set DataFolder=D:\Temp
for /d %%a in ("%DataFolder%\*.*") do (
	set FolderName=%%~nxa
	if /i not "!FolderName:~-4!"=="Done" (
		set ProcessFolder=%%~a
		goto Found
	)
)
echo All folders have been processed.
goto :eof
:Found
echo Next folder to process: %ProcessFolder%

Open in new window

And for the sake of completeness, here's a complete loop frame where you can add your code where indicated:
@echo off
setlocal enabledelayedexpansion
set DataFolder=D:\Temp
for /d %%a in ("%DataFolder%\*.*") do (
	echo Processing %%~a ...
	set FolderName=%%~nxa
	if /i "!FolderName:~-4!"=="Done" (
		echo ... already done.
	) else (
		call :Process "%%~a"
	)
)
echo All folders have been processed.
goto :eof
:Process
set Folder=%~1
set FolderName=%~nx1

REM Add your processing code here ...
ECHO ren "%Folder%" "%FolderName%-Done"

goto :eof

Open in new window

Avatar of Rebel_no_1

ASKER

oBdA, The first solution works. I just forgot to mention that there is also folders with a suffix of "-Busy" that must be excluded. (Another program is used to build these folders and my program must not work with the folder if it is still named with a suffix *-Busy

If the above works, we're all done. Thanks for a very quick and effective solution.
@echo off
setlocal enabledelayedexpansion
set DataFolder=D:\Temp
for /d %%a in ("%DataFolder%\*.*") do (
	set FolderName=%%~nxa
	if /i not "!FolderName:~-4!"=="Done" (
		if /i not "!FolderName:~-4!"=="Busy" (
			set ProcessFolder=%%~a
			goto Found
		)
	)
)
echo All folders have been processed.
goto :eof
:Found
echo Next folder to process: %ProcessFolder%
goto :eof

Open in new window

Basically it must only work with folders with a suffix of "-Ready"

Example:
001-Done
002-Done
003-Ready
004-Ready
005-Ready
006-Busy



The 001-Done, 002-Done folders have been processed with the script and the 006-Busy folder is still being populated with data. The script should process the 003-Ready folder next.

Hope it makes sense.
The last script opens and closes immediately. I cannot see the error? Almost there...  :-)
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Done! Excellent. Works like a charm!
Thank you for your time and effort. It is always very much appreciated when a complete stranger offers to help another complete stranger. To return the good "heartedness" I will donate something on your behalf to the next needy person I see.

You've done your good deed for today...   :-)