Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through directory names using a batch file?

Assume I have this structure:

d:/
  -- /alpha/
  -- /beta/
  -- /gamma/
  -- /delta/

I'm trying to execute a batch file that goes through those folders (but not through sub-folders within them).

How do I get this result using a FOR LOOP (assuming I don't know the name and quantity of the folders):

ren alpha alpha1
ren beta beta1
ren gamma gamma1
ren delta delta1
like image 379
Abuda Dumiaty Avatar asked Oct 15 '13 11:10

Abuda Dumiaty


People also ask

What is %% f in batch file?

To use for in a batch file, use the following syntax: for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>] To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type: for %f in (*.doc *.txt) do type %f.

How do I get a list of files in a folder and subfolders into text file?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory.

How does for loop work in batch?

For loop (default) of Batch language is used to iterate over a list of files. Example: copy some files into a directory (Note: the files to be copied into the target directory need to be in the same disk drive).


1 Answers

This is all you should need:

for /D %i in (*) do rename "%i" "%i1"

The /D performs the command against the directory names, as per the help, which can be obtained using the command for /?

If Command Extensions are enabled, the following additional forms of the FOR command are supported:

FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory
names instead of file names.
like image 70
David Martin Avatar answered Oct 16 '22 12:10

David Martin