Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a directory with a bunch of folders in them. I want each folder to create a text file that lists all the files in that folder. The format to list files should look like this:

"001.jpg" "002.jpg" "003.jpg" "004.jpg"

basically it should list files with no linebreaks. How can I do this with using cmd in windows 7?

share|improve this question

migrated from serverfault.com Feb 9 '13 at 16:35

This question came from our site for professional system and network administrators.

    
Heh, is there an xargs command for Windows? You're not using Powershell, are you? –  ewwhite Feb 9 '13 at 9:24
2  
This looks more like powershell programming which with your use of windows 7 (which isn't a server OS) means it's probably better asked on Stack Overflow or Super User. –  Iain Feb 9 '13 at 9:31
    
Install CygWin and then ask this question again without mentioning Windows. –  ruief Feb 9 '13 at 14:55
add comment

2 Answers

up vote 0 down vote accepted

You can achieve the desired result using only the CMD shell by combining forfiles, for and delayed variable expansion (see also: cmd).

Steps

  1. Create a batch file (e.g., C:\jpglist.bat) and save the following instructions to it:

    @echo off
    if %2 == FALSE exit
    cd /D %1
    for %%a in (*.jpg) do set list=!list! "%%a"
    echo %list:~1%> jpglist.txt
    
  2. Open a command prompt.

  3. Execute

    forfiles /P PATH /S /C "cmd /V:on /C C:\jpglist.bat @path @isdir"
    

    where PATH is the full path of the main directory.

  4. The batch file is no longer needed. Delete it if you want.

How it works

The command:

  • forfiles /P PATH /S /C "..." goes recursively (/S) through PATH and its subdirectories and executes ... for every directory entry.

  • cmd /V:on /C ... opens a new shell with delayed variable expansion enabled (/V:on) and executes ....

  • C:\jpglist.bat @path @isdir calls the batch file and passes the entry's full path as first parameter.

    @isdir will be TRUE if @path is a directory; otherwise, it will be FALSE.

The batch file:

  • if %2 == FALSE exit exits immediately if the first argument is not a directory.

  • cd /D %1 changes to the directory specified in the first argument (/D enables switching drives in case it's needed).

  • for %%a in (*.jpg) do set list=!list! "%%a" goes through all files with a .jpg extension and adds their names to the variable list.

    Using !list! instead of %list% delays the variable's expansion, i.e., list will get expanded in every iteration, rather then only once at the beginning.

  • echo %list:~1%> jpglist.txt writes the contents of the variable list (except the first character, which will be a leading space) in a file named jpglist.txt in the current directory.

share|improve this answer
add comment
ForEach($_ In Get-ChildItem "C:\Folder\" -Recurse -Directory)
{
    ForEach($file In Get-ChildItem -File $_.FullName)
    {
        [System.IO.File]::AppendAllText("$($_.FullName)\crazyList.txt", "`"$($file.Name)`" ", [System.Text.Encoding]::Unicode)
    }
}

That outputs a file in each folder with a format exactly as you specified it, with quotation marks.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.