- 1 year ago
Windows batch replace empty cells with above content
To replace empty cells with the content of the above cell in a Windows batch file, you can use a for
loop with the skip
and tokens
options to read and process each line of a CSV file.
Here's an example batch script that demonstrates the process:
batch@echo off
setlocal enabledelayedexpansion
set input_file=input.csv
set output_file=output.csv
set previous_line=
for /f "usebackq skip=1 tokens=*" %%a in ("%input_file%") do (
set "line=%%a"
if "!line!"=="" (
echo(!previous_line!>>"%output_file%"
) else (
echo(!line!>>"%output_file%"
set "previous_line=!line!"
)
)
endlocal
In the above script, replace input.csv
with the name of your input file, and output.csv
with the desired name for the output file.
The script reads each line of the input file, skips the first line (assumed to be the header), and checks if the line is empty. If the line is empty, it echoes the content of the previous line (stored in the previous_line
variable) to the output file. If the line is not empty, it echoes the line itself to the output file and updates the previous_line
variable.
After running the script, the output file (output.csv
) will have the empty cells replaced with the content of the above cell.