Number the file names in the Script package

I would like a batch script for all text documents in a folder. This is what I have done so far:

@ECHO off
title Test
set dir1=C:\Users\Family\Desktop\Example

:Start
cls
echo 1. test loop
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto test
if %choice%==2 exit

:test
cls
echo running loop test 
FOR %%n in (%dir1% *.txt) DO echo %dir1%\%%n
echo Done
pause

I would choose the following:

running loop test
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done

But I get the following:

running loop test
C:\Users\Family\Desktop\Example\C:\Users\Family\Desktop\Example
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done
+3
source share
1 answer

The main problem is the space between (% dir1% * .txt)

It could be

@ECHO off
title Test
set "dir1=C:\Users\Family\Desktop\Example"

:Start
cls
echo 1. test loop
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto test
if %choice%==2 exit

:test
cls
echo running loop test 
FOR %%X in ("%dir1%\*.txt") DO echo %%~dpnX
echo Done
pause

Quotation marks are designed to prevent problems with spaces or other special characters in the path.

EDIT:
%%~dpnX designed to expand the file name %%Xto
d= drive (C :)
p= path (\ Users \ Family \ Desktop \ Example)
n= file_name (test1) (without extension)

f= fully qualified file name (C: \ Users \ Family \ Desktop \ Example \ test1.txt).

Possible modifiers are described here FOR /?

+11

Source: https://habr.com/ru/post/1784857/


All Articles