views:

294

answers:

2

I have created a batch file to output a folder content into a list of names.

@echo off
cd /d %1
Title %~f1
dir %1 /b /l > %1\..\file_list.txt

How can I make the file_list.txt to be "dir name".txt?

For example I am at folder ABC and I want to output the dir list so that the final text file will be named ABC.txt instead of file_list.txt

Edit: Thanks Alconja, it works PerfectlY. Since this is an information and learning site, would you mind expanding your answer to explain the parameters you add?

+1  A: 

I don't think there's an easy way to get just the last part from the current path in just batch scripting.

But, if you have sed on your machine, it becomes relatively easy:

@echo off
cd /d %1
Title %~f1
for /F "usebackq delims=" %%i in (`"pwd | sed -e ""{s/.*\\//g}"""`) do dir %1 /b /l > %1\..\%%i.txt


Correction: Alconja's answer is correct. Apparently with the extensions to batch scripting, this is possible. (I now understand the ~f as well). So, you don't need sed anymore, and this reduces to:

@echo off
cd /d %1
Title %~f1
for /F "usebackq delims=" %%i in (`pwd`) do dir %1 /b /l > %1\..\%%~ni.txt

Arguably, since you change directory, you don't need the %1 in the output path or the dir command, and you should just be able to substitute %1 for the path, so you can try (untested):

@echo off
cd /d %1
Title %~f1
dir /b /l > ..\%~n1.txt
lc
It works perfectly. I have save it to the "SendTo" context menu.
qwertyuu
+2  A: 

Based on the discussion here, I think this is what you're after:

@echo off&setlocal enableextensions
Title %~f1
for %%* in (%1) do set MyDir=%%~n*
dir %1 /b /l > %1\..\"%MyDir%.txt"
endlocal&goto :eof

Gave it a quick test & seems to do what you want

...provided as is/no warranties/etc :)

Alconja
Cool, I'm not used to those extensions.
lc