I have multiple files each with the same file name
Example:
But the content within each file is different.
On the 1st and 2nd line of the file within token spots 2 and 4 I am able to identify the file. I would like to rename the files with these two data points.
Example
Within Test.csv file, line record 1 and 2
Within Test (1).csv line record 1 and 2
Within Test (2).csv line record 1 and 2
Within Test (3).csv line record 1 and 2
I would like to rename the following files as
Right now I can identify and rename the files with the data on line 2 and the 4th token. I want to be able to concatenate the two the data on line 2 (token 4) and line 1 (token 2) . Below is my current code.
@echo off
for %%i in (*.csv) do (
for /f "skip=1 token=4 delims=," %%j in ('findstr /B /I "^" "%%i"') do (
ren "%%i" "%%j.temp_txt"
)
)
ren *.temp_txt *.csv
The following (untested) batch iterates the csv's and counts the lines of the contents, stores line1 token2 in the var L1T2 amd line2 token4 to L2T4.
The resulting rename command is stored in a (pseudo) array and executed at the end.
To really execute the renames remove the echo in the last line.
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set Cnt=0
for %%i in (*.csv) do (
Set /A "Line=0,Cnt+=1"
for /f "tokens=2,4 delims=," %%A in ('Type "%%i"') do (
Set /A Line+=1
If !Line! equ 1 Set "L1T2=%%A"
If !Line! equ 2 Set "L2T4=%%B"
)
Set Ren[!Cnt!]=ren "%%i" "!L2T4!_!L1T2!.csv"
)
For /L %%C in (1,1,%Cnt%) Do Echo !Ren[%%C]!