The batch application renames according to the file content

Time:2021-11-23

I want to achieve the following objectives:
First, I have a. Dat file with a fixed name. I need to change the file name
. dat line 1 reads:   10107xxxxx means January 1, 2007. The following x is not fixed. If it is from January to September, the first character is empty
Now change the file name according to this line. For example, 10107xxxxxx is changed to c01-0101.cvs
110107xxxxx, changed to c01-1101.cvs
Please tell me how to write this bat file

analysis:
Read the first line to a variable
The fifth character of the area variable, which can determine whether it is 0  
1) If it is 0, the first 2 digits of the variable are taken as the month; otherwise, the first 1 digits are taken and prefixed with 0 as the month
2) If it is 0, take 3-4 digits of the variable as the month, otherwise take 2-3 digits as the day

answer:
@echo off

for %%i in (*.dat) do call :goren %%i

goto end

:goren

set /p firstln=<%1

set year=%firstln:~4,1%

if /i %year% GTR 0 (set month=0%firstln:~0,1%& set day=%firstln:~1,2%) else (

    set month=%firstln:~0,2%& set day=%firstln:~2,2%

)

ren %1 c01-%month%%day%.cvs

goto end

:end

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]