Computing.Net > Forums > Programming > Batch - list files only (no path)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - list files only (no path)

Reply to Message Icon

Name: AK12
Date: October 14, 2008 at 01:06:17 Pacific
OS: Windows XP Pro SP3
CPU/Ram: 7200 @ 2.0GHz , 2.5G
Product: DELL Precision M90
Comment:

Hi,

I have created a batch files to search for files from an external list.txt. But now I want to generate a list.txt from all the files that are in a directory and subdirectories but without the path.

I have figured out something like this:
DIR (MYPATH) /B/S/A:-D>list.txt

But I get this:
C:\Blabla\gaga\chacha\file.sql
C:\Blabla\gaga\folder\code.sql

And I would want just:
file.sql
code.sql

or even better:
file.sql,code.slq,…

The purpose of this batch is to search if files from a specified directory exist in another one.


Thanks for any help,
Julien



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 14, 2008 at 01:47:51 Pacific
Reply:

Doing it with dir could be a bit of a pain. Using the for /r command and for variable substitution makes it easy.


for /r mydrive:\mypath\mydir %%g in (*) do echo %%~nxg>>list.txt


0

Response Number 2
Name: AK12
Date: October 14, 2008 at 02:17:23 Pacific
Reply:

Hi,

Thanks for such a fast response!

It works, I think I was a little too focused on the dir...

But what does the "g" from "%~nxg" do?
Is it from the %%g?


Thanks again,


0

Response Number 3
Name: Judago
Date: October 14, 2008 at 02:42:58 Pacific
Reply:

Yes the g is just from the variable, every for loop needs a one letter variable. Upper and lower case letters count as different variables, so %%g will be different to %%G (something to know to avoid hair getting torn out :).

The for variable substitution always starts with "~" after the percentage signs. and ends in the variable. The letters in the middle are inbuilt modifiers, n is file name only(without extension) and x is the file extension. The are a few more, but instead of explaining them all I recommend checking out the help page for the for command (for /? at the commandline).


0

Response Number 4
Name: AK12
Date: October 14, 2008 at 04:40:39 Pacific
Reply:

Hi,

Thank you very much for the help and explanations.

Julien



0

Response Number 5
Name: AK12
Date: October 14, 2008 at 07:13:19 Pacific
Reply:

Hi

I have another question.

How could I update a variable without changing it's actual value. Kind of like:

for /f "delims=" %%a in (list.txt) do(
set search=%search%,%%a
)

I have a file like this:
code.sql
web.aspx
...

And I want a string or variable like this:
code.sql,web.aspx,...

Thanks again,


0

Related Posts

See More



Response Number 6
Name: pball
Date: October 14, 2008 at 08:04:57 Pacific
Reply:

I would say you need to put this before the for loop.

setlocal enabledelayedexpansion

That allows variables to be updated inside of a for loop. The variable then needs exclamation points (!) instead of percent signs (%) around it when inside of the for loop.

Quick lesson, variables do no get updated when inside of a for loop unless the enable delayed expansion is turned on.

type set /? into a cmd window to learn more.
----
EDIT
Just put the two different parts together.

This following script will get you a comma separated list of file names in a folder you specify

@echo off
setlocal enabledelayedexpansion
for /r H:\fortran %%g in (*) do (
set search=!search!%%~nxg,
)
echo %search% > list.txt

There will be a comma at the end of the list, like in the example below.

Primecalc_read_v3.exe,Primecalc_read_v3.f,

If you can't have that comma at the end of the list let me know, it can be removed


0

Response Number 7
Name: AK12
Date: October 14, 2008 at 23:23:16 Pacific
Reply:

Hi,

The "!" and "setlocal enabledelayedexpansion" were exactly what I was looking for.

Actually it would be better without the comma at the end.

And again, thanks for you time.


0

Response Number 8
Name: Judago
Date: October 15, 2008 at 02:03:48 Pacific
Reply:

No comma at the end.


@ECHO OFF
set search=
setlocal enabledelayedexpansion
for /r Drive:\path\dir %%g in (*) do (
if not defined search (set search=%%~nxg) else set search=!search!,%%~nxg
)
echo !search!>>list.txt

I'm not sure on what the maximum length of a variable is, so this could push it a little....


0

Response Number 9
Name: Judago
Date: October 15, 2008 at 02:46:16 Pacific
Reply:

From testing I've just done I think the limit is 8184 characters, this could also be echo's limit. There is a way to get around the variables limit, but I'm not so sure about echo's limit.

I'm pretty sure there's a way to do what your trying to do with type but I can't seem to get it working. Maybe someone else can fill us in....

It would be easy to use a third party utility to do it, but that's another story.


0

Response Number 10
Name: AK12
Date: October 15, 2008 at 02:47:07 Pacific
Reply:

Hi,

It works.

And I checked here
http://technet.microsoft.com/en-us/...

that:
# The maximum individual environment variable size is 8192bytes.
# The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB.

And that is enough for what I'm doing.


0

Response Number 11
Name: klint
Date: October 15, 2008 at 06:26:22 Pacific
Reply:

This is now academic, but if the 8192-byte limit wasn't enough for you, here's one that has no limits:

@echo off
setlocal
set first=y
(
for /r Drive:\path\dir %%g in (*) do (
if defined first (
set first=
set/p=%%~nxg<nul
) else (
set/p=,%%~nxg<nul
)
)
)>list.txt


0

Response Number 12
Name: Judago
Date: October 15, 2008 at 15:10:26 Pacific
Reply:

Neat trick!! Using set /p to output data without the linebreak then command grouping to get it into a file.

Great script klint!


0

Response Number 13
Name: AK12
Date: October 16, 2008 at 00:22:59 Pacific
Reply:

Yes indeed, it's a good trick!

Thanks to both of you for your help!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - list files only (no path)

Batch copy files on txt file incl. subdir. www.computing.net/answers/programming/batch-copy-files-on-txt-file-incl-subdir/20053.html

Batch files: Get absolute path www.computing.net/answers/programming/batch-files-get-absolute-path/18251.html

batch list files of dir+user choice www.computing.net/answers/programming/batch-list-files-of-diruser-choice/16683.html