1

i want to make a batch file, This Batch file must look out, into a folder with the name "Draft" and for every sub-folder will be make a search for a .txt file "list.txt" and when finds this .txt file, Then will be execute a copy from the folder "Draft" to the folder "Ready". I have written a small script but i have some issues.

@echo off
:loop

  for /d %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do ( 
  SET a=%%i
  echo %a%
  )

echo Folder is empty or does not exist
timeout /t 15
goto loop

The problem in this small part of the script is that, the variable "a" cannot save the name of the folder, if i change the echo %a% to echo Hello World then the script prints only one time the message and not as long as the loop runs. Any ideas?

ntosis
  • 11
  • 1
  • 2
  • Google "enableDelayedExpansion". But I don't see why you need an environment variable. It seems to me you could simply use `%%i` directly. – dbenham Jul 13 '15 at 12:24
  • Possible duplicate of [use variables inside if and for loop](https://superuser.com/questions/426841/use-variables-inside-if-and-for-loop) – phuclv Feb 10 '19 at 12:16
  • [Variables in batch file not being set when inside IF?](https://superuser.com/q/78496/241386), [Batch File: Loop Not Outputting Inputted Variable Value to External File](https://superuser.com/q/1323681/241386), [batch script for loop and if statement not interacting properly](https://superuser.com/q/1357062/241386)... – phuclv Feb 10 '19 at 12:17
  • Do you want to copy all Subfolders from Draft to Ready that have a list.txt file in tem or do you want to copy the text inside list.txt to ready? – Ricardo Bohner Nov 22 '21 at 10:13

1 Answers1

0

You can probably do this in a one liner:

for /R "C:\Users\ntosis\Desktop\Draft" %G in (list.txt) do ( type "%G" >> "C:\Users\ntosis\Desktop\Ready\list.txt"

This will concatenate all your list.txt files into a single list.txt in the Ready folder.

Make sure your Ready folder is not a subfolder of your Draft folder otherwise you'll get duplicated lines.

If you want to run from a batch file dont forget to double your %:

for /R "C:\Users\ntosis\Desktop\Draft" %%G in (list.txt) do ( type "%%G" >> "C:\Users\ntosis\Desktop\Ready\list.txt"

If you want to copy the whole source directory structure with your list.txt files then thats a bit different.

grover
  • 1
  • 1