Je voudrais un script batch à tous les documents texte dans un dossier. C’est ce que j’ai réussi jusqu’ici:
@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
Ce que je voudrais en sortie c’est:
running loop test C:\Users\Family\Desktop\Example\doc 1.txt C:\Users\Family\Desktop\Example\doc 2.txt Done
Mais je reçois ceci:
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
Le principal problème semble être l’espace entre (% dir1% * .txt)
Il pourrait être
@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
Les guillemets évitent les problèmes d’espaces ou d’autres caractères spéciaux dans le chemin.
MODIFIER:
Le %%~dpnX
sert à étendre le nom de fichier de %%X
à
d
= lecteur (C 🙂
p
= chemin (\ Users \ Family \ Desktop \ Example)
n
= nom de fichier (test1) (sans extension)
f
= nom de fichier complet (C: \ Users \ Family \ Desktop \ Example \ test1.txt).
Les modificateurs possibles sont expliqués ici FOR /?