Hi,

Im trying to make a batch script which can go through a directory and pick all the .txt files and copy the name in a file. Ive been able to make a script which can do this in a particular folder but if there is a folder in the folder it doesnt check in that, this is what I have so far,

@echo off

rem get files into text file
 
set PD=%CD%
set WW=%1
set WD=%WW%

CD %WD%
dir /b | gawk "{print $1}" | grep \.c | sed "s/\.txt//g" > %PWD%\testing2.txt

I was thinking of using recursion to to go deeper into the folders but im not sure how to identify a folder, If I can I can use it as a kind of a stop case.

Thanks,
Rahul

Dani AI

Generated

As noted, a recursive listing fixes the "not checking subfolders" problem. To get just the final filename (basename) from a full path you can avoid fragile field-splitting and use built‑in features. Below are three practical ways that work on Windows environments — pick the one that fits your toolchain.

Batch: recurse and produce basenames (no extension)

@echo off
for /r "C:\path\to\root" %%F in (*.txt) do echo %%~nF >> basenames.txt

Batch: if you already have a file of full paths (one per line)

@echo off
rem paths.txt contains full paths; usebackq + delims= preserves spaces
>basenames.txt (
  for /f "usebackq delims=" %%F in ("paths.txt") do @echo %%~nF
)

PowerShell: modern and robust (keeps international chars nicely)

Get-ChildItem -Path 'C:\path\to\root' -Recurse -Filter '*.txt' |
  Select-Object -ExpandProperty BaseName |
  Out-File -Encoding utf8 basenames.txt

If you prefer GNU tools (gawk), set the field separator to backslash and print the last field:

gawk -F'\\\\' '{print $NF}' paths.txt > basenames.txt

Quick tips: in batch files use double percent markers (%%) and at the interactive prompt use single (%). Use delims= with for /f to preserve spaces in filenames. %%~nF gives the name without extension; %%~xF is the extension; %%~nxF is name+ext; %%~dpF is drive+path. These approaches avoid brittle text-parsing and handle nested folders reliably for 's use case.

use dir /b /s

/s Displays files in specified directory and all subdirectories.

Thanks, that solves my immeadiate problem. Im using gawk to get the last column data from the file.
My file looks like this,

C:\Documents and Settings\name\test\byebye.c
C:\Documents and Settings\name\test\hello.c
C:\Documents and Settings\name\test\hey.h
C:\Documents and Settings\name\test\New Folder
C:\Documents and Settings\name\test\New Folder (2)
C:\Documents and Settings\name\test\New Folder\tesdfsftsdfds.c

using the command,

gawk "{print $NF|"

the data is split into,


Settings\name\test\byebye.c
Settings\name\test\hello.c
Settings\name\test\hey.h
Settings\name\test\New Folder
Settings\name\test\New Folder (2)
Folder\tesdfsftsdfds

What I need is to get the last file name, is it possible to get these by using gawk again or is there a way to space the data after a \, that way i can use gawk better?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.