I'd like to rename a file with the previous month + current year using a batch script. Please help!
For example: rename c:\Example.txt Example_MMYY.txt
Where MM = previous month and YY = current year or Example_0718.txt
try this:
:prevmonthren
setlocal
::argument value
::set "file=%~f1"
::hardcoded value
set "file=testfile.txt"
for %%# in ("%file%") do (
set "ext=%%~x#"
set "nam=%%~n#"
)
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
set /a prev_month=month-1
if %prev_month% lss 10 set "prev_month=0%prev_month%"
if %month%==1 (
set "prev_month=12"
set /a year=year-1
)
set year=%year:~2%
ren %file% %nam%_%prev_month%%year%%ext%
endlocal
you can use hardcoded value for the file location or to uncomment the first line where the file location is set (and comment the second) in order to use command line arguments.