#1 07 Jul 2018 08:04

Lauraq
Member
Registered: 31 Jul 2011
Posts: 33

Problem with .bat save list files in a folder :(

Hi:)
I have this .bat to save the list of files in a folder


@echo off
chcp 1252
SET NOMEFILE=%TEMP%\FILES.TXT
IF EXIST %NOMEFILE% DEL %NOMEFILE%
FOR /F "delims=;" %%x in ('dir /b %1') do CALL :NOESTENSIONE "%%x" >> %NOMEFILE%
NOTEPAD %NOMEFILE%
DEL %NOMEFILE%
GOTO :EOF

:NOESTENSIONE
ECHO %~n1
GOTO :EOF



The bat file work fine but does not work with special characters like "&". Can you help me?
Thanks:)

Offline

#2 08 Jul 2018 10:52

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 976
Website

Re: Problem with .bat save list files in a folder :(

The & symbol is a continuation character, so will need to be escaped.. or:

The simplest solution is to use quotes for the assignment, and delayed expansion. This eliminates any need for escaping.
https://stackoverflow.com/questions/141 … -win-batch

Offline

#3 08 Jul 2018 13:22

Lauraq
Member
Registered: 31 Jul 2011
Posts: 33

Re: Problem with .bat save list files in a folder :(

sorry but I'm not expert at all. I found this .bat but I can not change it sad  Could you do it? Thank you

Offline

#4 08 Jul 2018 15:09

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Problem with .bat save list files in a folder :(

You just need to put quotes around the variable expansions, but one will need delayed expansion.

@echo off
chcp 1252
if not exist "%~1" exit /b 1
set "nomefile=%temp%\FILES.TXT"
>nul copy nul "%nomefile%"
for /f "delims=;" %%X in (' dir /b "%~1" ') do call :NOESTENSIONE "%%~X" >> "%nomefile%"
notepad "%nomefile%"
if exist "%nomefile%" del "%nomefile%"
exit /b 0

:NOESTENSIONE
setlocal DisableDelayedExpansion
	set "_n1=%~n1"
	setlocal EnableDelayedExpansion
	echo(!_n1!
goto :eof

Last edited by Pyprohly (08 Jul 2018 15:16)

Offline

#5 08 Jul 2018 18:31

Lauraq
Member
Registered: 31 Jul 2011
Posts: 33

Re: Problem with .bat save list files in a folder :(

Thanks! Is perfect! smile

Last edited by Lauraq (08 Jul 2018 18:34)

Offline

#6 09 Jul 2018 12:29

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,052

Re: Problem with .bat save list files in a folder :(

Should be able to avoid delayed expansion altogether (and simplify to remove the need of the subroutine call).  Untested, but I think this should do it:

@echo off
chcp 1252
if not exist "%~1" exit /b 1
set "nomefile=%temp%\FILES.TXT"
(
  for /f "delims=;" %%X in ('dir /b "%~1" ') do (
    set /p "OUT=%%~nX" <nul
    echo:
  )
) > "%nomefile%"
notepad "%nomefile%"
if exist "%nomefile%" del "%nomefile%"
exit /b 0

cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

#7 09 Jul 2018 12:52

Lauraq
Member
Registered: 31 Jul 2011
Posts: 33

Re: Problem with .bat save list files in a folder :(

Thanks, this works perfectly too smile
Last help...is there a way to have a list of only 2 types of files (mp3 and flac) and exlcude all the other tipe of files?
Thank you

Offline

#8 10 Jul 2018 09:08

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Problem with .bat save list files in a folder :(

Assuming the parameter is a directory name:

@echo off
chcp 1252

set "nomefile=%temp%\FILES.TXT"
set "file_mask=*.mp3 *.flac"

if not exist "%~1"\* exit /b 1

pushd "%~1" || exit /b 1
>"%nomefile%" (
	for /f "delims=;" %%X in (' dir /a:-d /b %file_mask% ') do (
		set/p"=%%~nX"<nul
		echo(
	)
)
popd

notepad "%nomefile%"
if exist "%nomefile%" del "%nomefile%"

Offline

#9 10 Jul 2018 09:15

Lauraq
Member
Registered: 31 Jul 2011
Posts: 33

Re: Problem with .bat save list files in a folder :(

perfect! smile  you are very kind!

Thanks for all

Offline

Board footer

Powered by FluxBB