Steve Howe
Steve Howe

Reputation: 13

Windows 7 batchfile to iterate through subdirectories selecting first file

I am running windows 7 64 bit

I have a directory, full of sub directories, each subdirectory has many files.

I want to write a batchfile which will iterate through the subdirectories in the current directory, copy the first file in each subdirectory to the parent directory, renaming the file .txt

so far I have this:

`for /D %%s in (./*) do
(
 cd %%s
 i=1
 for /f %%a in (./*) do
 (
  if i==1
   cp %%a >> ./../%%s.txt
   i=i+1
  else
 ) 
 cd ..
)`

All I am getting is that the syntax of the command is incorrect.

I have tried using echos to see how far I get and I am not getting past the first line.

Upvotes: 0

Views: 92

Answers (2)

Aacini
Aacini

Reputation: 67216

I think this way is simpler and easier to understand:

@echo off
setlocal EnableDelayedExpansion

for /D %%s in (*) do (
   set "firstFile="
   for %%a in ("%%s\*.*") do if not defined firstFile set "firstFile=%%a"
   copy "!firstFile!" "%%s.txt"
)

An observation on previous code:

  • The .\anyName although correct, is usually never used. A dot indicate "current folder" and .\anyName indicate the anyName below current folder, that is exactly the same than just anyName.

EDIT: New method to copy the Nth file

@echo off
setlocal EnableDelayedExpansion

for /D %%s in (*) do (
   set i=0
   for %%a in ("%%s\*.*") do (
      set /A i+=1
      if !i! equ N copy "%%a" "%%s.txt"
   )
)

You must write a number in place of N above.

Upvotes: 1

Magoo
Magoo

Reputation: 80033

Batch syntax is very specific.

for... do (
 whatever

if ... (
 whatever

) else (
 whatever

The open-parentheses must be in the position indicated. whatever may be on the following line or the same line as the (

If you have a sequence of statements (for an if-true condition, for example) then these must be parenthesised

The close-parenthesis before an else must be on the samp physical line as the else.

cp is invalid - unless you're using cygwin, the command is copy

i=i+1 is invalid - use set /a i=i+1 (/a for arithmetic expressions, else it's assumed to be s string expression)

%var% for the value of a variable. i will never be equal to 1 except within a block statement (parenthesised series of statements) then you need to invoke delayedexpansion (an option of setlocal) and use !var! as %var% is resolved to be the value of var at the time the expression is parsed, not executed.

/ is a switch indicator, not a directory separator. \ is a directory-separator. Windows can often make the substitution, but not when there is a syntax ambuity. Best to use the correct character.

"quote" arguments that may contain separators (like spaces.) It's not strictly necessary in a cd statement, but it's harmless there and is required almost everywhere else.

setlocal enabledelayedexpansion
for /D %%s in (.\*) do (
 cd "%%s"
 set /a i=1
 for /f %%a in (.\*) do (
  if !i!==1 (
   cOpY %%a >> ".\..\%%s.txt"
   set /a i=i+1
  ) else (     ) 
 cd ..
)

Although... I'd be tempted to use PUSHD "%%s" in place of the first cd and popd for the second.

Not sure what your copy is aimed at achieving. To append to an existing file, I'd use type. There is a copy syntax that implements append this file to that but I use it so rarely I'd have to look it up.

commandname /?

from the prompt will provide (sometimes obscure) documentation.

Upvotes: 2

Related Questions