Using FOR command to look into specific foldr

October 29, 2010 at 12:29:21
Specs: Windows XP
Hi All,

I have a problem that I have been trying to solve.
I am trying to use the FOR dos command to look for all text documents that exist in a specific file named "spool" and copy them into a specific file called "history" that exists in the same folder as spool does.

This spool file exists in many different places inside the following directory: C:\test
for example like this:

So far here is what I have, and I know that this is nowhere close to what I am trying to, but I am trying to get a start but I keep getting stuck.

for /r C:\test\ %%A in (*.txt) do If %%~nA equ spool copy *.* %%~nA equ history

This clearly does not work, and only returns gibbrish.

What am I doing wrong?

If anyone can help, I would be very very thankful.


See More: Using FOR command to look into specific foldr

Report •

October 30, 2010 at 04:20:32
Try the following batch where Folder_Name is your root folder:

:: MYCOPY.BAT Usage: mycopy Folder_Name
@echo off & setlocal EnableDelayedExpansion

set folder=%*
if not defined folder (echo.  Usage: mycopy Folder_Name & goto :EOF)
set folder=%folder:"=%
if not exist "%folder%" (echo.  Folder "%folder%" not found & goto :EOF)

for /F "delims=" %%i in ('dir /AD /B /S "%folder%"') do (
  set layer=%%i
  if not "!layer:spool=!"=="!layer!" (
    if exist "%%i\*.txt" copy "%%i\*.txt" "!layer:spool=history!"
:: End_Of_Batch

Report •

November 1, 2010 at 08:27:38
Hi Ivo,

This worked like a charm! this script is very impressive and concise. I just have to do some testing to see that it works on a big folder tree.

Thanks so much!!!

Report •
Related Solutions

Ask Question