Home > other >  Windows bat file script to format source date
Windows bat file script to format source date

Time:09-30

I have file in below format in my Windows server folder:

C:\ABC\abcdef - ggggg hhhhhh iiii jj xxxx kkk - pp qq Sep 21, 2021.txt

I need to check if the file exists in the folder and get the date from the file name. Then the file should be moved to another folder with a new file name with source file date append in MMddyyyy format.

The file should be moved to directory:

C:\ABC\DEF

with the name:

abcdef_xxxx_09212021.txt

I need help to write the Windows batch script to get the date from source file name and move the file with using that date reformatted in new file name.

I figure out file move and rename, but need help to hold date value and then change the format from MMM dd, yyyy to MMddyyyy and append with new file name as in below command line:

IF EXIST "C:\ABC\abcdef - ggggg hhhhhh iiii jj xxxx kkk -*.txt" move /Y "C:\ABC\abcdef - ggggg hhhhhh iiii jj xxxx kkk -*.txt" C:\ABC\DEF\abcdef_xxxx_.txt

CodePudding user response:

The file moving task with renaming the file with date in file name reformatted can be done with following batch file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BaseFolder=C:\ABC"
set "!Jan=01"
set "!Feb=02"
set "!Mar=03"
set "!Apr=04"
set "!May=05"
set "!Jun=06"
set "!Jul=07"
set "!Aug=08"
set "!Sep=09"
set "!Oct=10"
set "!Nov=11"
set "!Dec=12"
for /F "eol=| delims=" %%G in ('dir "           
  • Related