2015年1月27日 星期二

Batch file name string concatenation




Inside your for loop you show the value of %attachment%, so that value is NOT updated in each iteration; you must use delayed expansion to do so.
The set of values in for command is: (%directory% *.xml) that is, the value C:\temp and all files with .xml extension, that I assumed is none in current directory. After that, you use this value in %a% %directory%\%%n expression, so the result is -a C:\temp\C:\temp. I think there is not a point here.
If you want not the folder value in the list, just don't insert it and use %~NXn modifier in the forreplaceable parameter.
Below is the correct code:
copy *.xml C:\FTP
setLocal Enabledelayedexpansion
set "directory=C:\temp"
set "attachment= "
set "a= -a "
for %%n in ("%directory%\*.xml") DO ( 
set "attachment=!attachment! %a% %%~NXn "
echo.!attachment!
)
setlocal disabledelayedexpansion
echo.%attachment%

My Version
setLocal Enabledelayedexpansion
set "directory=E:\data_temp\~Ricky\testscript"
set "attachment= "
set "a= "
for %%n in ("%directory%\*.pcap") DO ( 
set "attachment=!attachment!%a%%%~NXn "
echo.!attachment!
)
setlocal disabledelayedexpansion
echo.%attachment%

PAUSE

沒有留言:

張貼留言

202501 Debian USB LAN Card Bridge

 202501 Debian USB LAN Card Bridge ChatGPT Question I have a machine running debian 12 with a LAN port using a cable to connect to my office...