Search code examples
batch-filedos

A batch file that search for a file, rename file and move it to another directory


@echo OFF
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "E2F.csv" E2F0%%g%%f%%e.csv
PAUSE
POPD

I want a batch script that does the following:

search for a file called E2F.csv

The script must rename E2F.csv to E2F0YYMMDD

Eg. E2F0130517.csv

The script must then move E2F0130517.csv to E2F folder.

I highly appreciate your help.


Solution

  • Perhaps you can try this code:

    set CURR_DATE=%DATE%
    set MONTH=%CURR_DATE:~4,2%
    set DAY=%CURR_DATE:~7,2%
    set YEAR=%CURR_DATE:~12,2%
    set VERSION=%YEAR%%MONTH%%DAY%
    set source_folder = "C:\source"
    set dest_folder = "C:\dest"
    cd %source_folder%
    if exist "%source_folder%\E2F.csv" (
    echo "File Exists, Renaming and copying now"
    move %source_folder%\E2F.csv  %dest_folder%\E2F%VERSION%.csv
    ) else (
    echo "File Not Found"
    )
    

    Kindly, let me know if this solves your issue.