简体   繁体   中英

Reading list of files in a directory and copying the contents using batch command file

I have a list of csv files in a directory which have name with format XX_YYYFile.csv, where XX is a name that can have any characters (including space), and YYY is random 3 digits. For example: "book_123File.csv", "best movie_234File.csv", etc. I want to read this list of files then create new CSV files by removing "_YYYFile". The content of the new files are the same with the original ones, except the first line needs to be added with value "number,name,date".

set inputFileFolder=C:\Input
set outputFileFolder=C:\Output
FOR /F "delims=" %%F IN ('DIR %inputFileFolder%\*File.csv /B /O:D') DO (
    set reportInputFile=%inputFileFolder%\%%F
    set reportInputFileName=%%F
    set result=!reportInputFileName:~0,-12!
    set reportOutputFileName=!result!.csv
    set reportOutputFile=%outputFileFolder%\!result!.csv
    echo number,name,date > !reportOutputFile!
    for /f "tokens=* delims=" %%a in (!reportInputFile!) do (
        echo %%a >> !reportOutputFile!
    )
)

If I run this batch file, file "book.csv" is successfully created with the correct contents (first line: "number,name,date", the next lines are from file "book_123.csv"). But file "best movie_234.csv" and other files contain space in the filename are not created successfully. File "best movie.csv" is created with only 1 line "number,name,date". The contents of file "best movie_234.csv" are not copied to file "best movie.csv".

Please help.

You need to Escape Characters, Delimiters and Quotes properly. Note the usebackq parameter in inner for /F loop as well:

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "inputFileFolder=C:\Input"
set "outputFileFolder=C:\Output"
FOR /F "delims=" %%F IN ('DIR "%inputFileFolder%\*File.csv" /B /O:D') DO (
    set "reportInputFile=%inputFileFolder%\%%F"
    set "reportInputFileName=%%F"
    set "result=!reportInputFileName:~0,-12!"
    set "reportOutputFileName=!result!.csv"
    set "reportOutputFile=%outputFileFolder%\!result!.csv"

    >"!reportOutputFile!" echo number,name,date

    for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
       >>"!reportOutputFile!" echo %%a
    )

        rem above `for /f ... %%a ...` loop might be replaced by FINDSTR
    rem >>"!reportOutputFile!" findstr "^" "!reportInputFile!"

        rem                                                or by TYPE
    rem >>"!reportOutputFile!" type "!reportInputFile!"  
)

Hint : each > and >> redirector works as follows:

  • opens specified oputput file, then
  • writes something to oputput file, and finally
  • closes oputput file.

This procedure might be extremely slow if repeated in next for /f ... %%a ... loop for larger files:

    >"!reportOutputFile!" echo number,name,date
    for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
       >>"!reportOutputFile!" echo %%a
    )

Use block syntax rather:

    >"!reportOutputFile!" (
    echo number,name,date
        for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
           echo %%a
        )
    )

above for /f ... %%a ... loop might be replaced by FINDSTR command (it eliminates empty lines like for does) as follows:

    >"!reportOutputFile!" (
         echo number,name,date
         findstr "^." "!reportInputFile!"
    )

or by TYPE command (it will retain empty lines unlike for ) as follows:

    >"!reportOutputFile!" (
         echo number,name,date
         type "!reportInputFile!"  
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM