BAT File Command Help

EzGoingKev

Registered User.
Local time
Today, 05:42
Joined
Nov 8, 2019
Messages
197
Good afternoon.

I have BAT file that creates a text file with directory of the folder that it is in and puts in on the desktop. Here is the code:

dir "%~dp0" /b > "%USERPROFILE%\Desktop\list.txt"

This works 100% for me but I would like to append the name of the folder the BAT file is in to the file name.

For example if the BAT file was in a folder called Dog the BAT file would create a file called "Dog_list.txt".

Is there a simple way to get the folder name added to the filename?

ETA - the folders are located on a server.
 
Last edited:
Doubtless a BAT person here will help you out, but if you want to switch to something infinitely easier to read, document, make conditional, and maintain, I'll be happy to provide a VBScript (.vbs) file that will do what you're wanting. Will take a moment to do so if you decide you wish.
 
Thanks for the reply.

I do not know what a VBScript file is.

A couple of things first.

Anyone can just click on the BAT file and it will create a file on their desktop name list.txt that has the folder's directory in it. I am looking for something that all the user has to do is click on it.

This is on a corporate server. IT has the permissions locked down pretty tight. For example, I cannot open a PowerShell window for those folders. Will this prevent the VBS from running?
 
90% chance the vbs would still run, i've had mostly corporate experience including powershell restrictions but generally they dont' prevent vbs from running on your local machine. and yes, double clicking on would instantly run it. i'll whip it up just for fun and if you dont use it that's OK.
have to go to traffic court wiht my daughter (she has a lead foot apparently), but will be back later this afternoon.
 
Good afternoon.

I have BAT file that creates a text file with directory of the folder that it is in and puts in on the desktop. Here is the code:

dir "%~dp0" /b > "%USERPROFILE%\Desktop\list.txt"

This works 100% for me but I would like to append the name of the folder the BAT file is in to the file name.

For example if the BAT file was in a folder called Dog the BAT file would create a file called "Dog_list.txt".

Is there a simple way to get the folder name added to the filename?

See if this helps at all
 
I haven't used batch files in a while, but here's what I came up with.
Code:
for %%i in (%cd%) do set fldr=%%~nxi
dir "%~dp0" /b > "%userprofile%\desktop\%fldr%_list.txt"
Hope that helps...
 
Download the attachment, change the extension from .txt to .vbs
Click to run
Yes...it's more code, but easy to read and "add stuff" to / enhance/change.
 

Attachments

I haven't used batch files in a while, but here's what I came up with.
Code:
for %%i in (%cd%) do set fldr=%%~nxi
dir "%~dp0" /b > "%userprofile%\desktop\%fldr%_list.txt"
Hope that helps...

That created a file named "Windows_list.txt"
 
Download the attachment, change the extension from .txt to .vbs
Click to run
Yes...it's more code, but easy to read and "add stuff" to / enhance/change.

That got me this:

1745496094696.png


ETA - I just noticed it created the file in the folder not on my desktop so I changed this line of code:

set ts= fso.createtextfile(strDesktopPath & "\" & strFolderCurrentName & "_" & "list.txt")

It ran and I did not get the error message.
 
Last edited:
That created a file named "Windows_list.txt"
Not for me, it created a txt file with the name of the folder, in my case Temp.
1745496440594.png

1745496484445.png

108. CD SHOP 2025 Plus Page 2.xlsx
Address trial.xlsx
Database1.accdb
DB
Screenshot 2025-04-21 161404.png
Screenshot 2025-04-21 165105.png
Test.bat
VM Modem.docx
 
Not for me, it created a txt file with the name of the folder, in my case Temp.
View attachment 119558
View attachment 119559
108. CD SHOP 2025 Plus Page 2.xlsx
Address trial.xlsx
Database1.accdb
DB
Screenshot 2025-04-21 161404.png
Screenshot 2025-04-21 165105.png
Test.bat
VM Modem.docx

I tried it on a folder on my local machine and it appends the folder name like it is supposed.

When I use it on the server it appends "Windows" to the file name.
 
I tried it on a folder on my local machine and it appends the folder name like it is supposed.

When I use it on the server it appends "Windows" to the file name.
Sorry about that. I don't have a server to do any testing.
 
That got me this:

View attachment 119557

ETA - I just noticed it created the file in the folder not on my desktop so I changed this line of code:

set ts= fso.createtextfile(strDesktopPath & "\" & strFolderCurrentName & "_" & "list.txt")

It ran and I did not get the error message.

Thanks, I missed that in your req's, I was thinking to create in same as current folder. Good catch/edit
 
See if this can help in any way.
 

Users who are viewing this thread

Back
Top Bottom