Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtBut 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
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
Report Offensive Follow Up For Removal
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,
Report Offensive Follow Up For Removal
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).
Report Offensive Follow Up For Removal
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,
Report Offensive Follow Up For Removal
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.txtThere 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
Report Offensive Follow Up For Removal
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.
Report Offensive Follow Up For Removal
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.txtI'm not sure on what the maximum length of a variable is, so this could push it a little....
Report Offensive Follow Up For Removal
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.
Report Offensive Follow Up For Removal
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.
Report Offensive Follow Up For Removal
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
Report Offensive Follow Up For Removal
Neat trick!! Using set /p to output data without the linebreak then command grouping to get it into a file.
Great script klint!
Report Offensive Follow Up For Removal
Yes indeed, it's a good trick!
Thanks to both of you for your help!
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

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