0x01 如何读取csv 文件内容
- 用 for /f
Salary.csv
name,country,city,salary
John,UK,London,5000
Mary,German,Berlin,5300
Peter,US,Boston,3000
read_salary.bat
@echo off
for /f "skip=1 delims=, tokens=1,2,3" %%i in (%cd%\salary.csv) do (
echo input=%%i %%j %%k
)
Execution
C:\Users\inakamono\wk\coding\batch>dir
Volume in drive C is OS
Volume Serial Number is CE42-F672
Directory of C:\Users\inakamono\wk\coding\batch
09/17/2020 09:41 PM <DIR> .
09/17/2020 09:41 PM <DIR> ..
09/17/2020 09:42 PM 115 read_salary.bat
09/17/2020 09:40 PM 92 salary.csv
2 File(s) 207 bytes
2 Dir(s) 46,528,008,192 bytes free
C:\Users\inakamono\wk\coding\batch>read_salary.bat
input=John UK London
input=Mary German Berlin
input=Peter US Boston
C:\Users\inakamono\wk\coding\batch>
0x02 如何读取文件夹内的文件
- 用 for /r
loop_files.bat
@echo off
set test=%cd%
echo %test%
for /r %test% %%F IN (*) DO (
echo %%F
)
Execution
C:\Users\inakamono\wk\coding\batch>dir
Volume in drive C is OS
Volume Serial Number is CE42-F672
Directory of C:\Users\inakamono\wk\coding\batch
09/17/2020 09:52 PM <DIR> .
09/17/2020 09:52 PM <DIR> ..
09/17/2020 09:56 PM 89 loop_files.bat
09/17/2020 09:42 PM 115 read_salary.bat
09/17/2020 09:40 PM 92 salary.csv
3 File(s) 296 bytes
2 Dir(s) 46,535,454,720 bytes free
C:\Users\inakamono\wk\coding\batch>loop_files.bat
C:\Users\inakamono\wk\coding\batch
C:\Users\inakamono\wk\coding\batch\loop_files.bat
C:\Users\inakamono\wk\coding\batch\read_salary.bat
C:\Users\inakamono\wk\coding\batch\salary.csv
C:\Users\inakamono\wk\coding\batch>
FOR - Loop through a set of files in one folder.
FOR /R - Loop through files (recurse subfolders) .
FOR /D - Loop through several folders.
FOR /L - Loop through a range of numbers.
FOR /F - Loop through items in a text file.
FOR /F - Loop through the output of a command.
参考
For loop