How to rename file using batch fiel process

January 21, 2012 at 21:22:48
Specs: Windows 2003
I have a network directory that is being populated with pdf file every hour. The pdf filename is almost consistent except the last 7characters of the file name which was intended to serialixation of the pdf file (e.g. gibyroom0000001.pdf)

I want to copy the pdf file to another directory and rename it without the serialization
gibyroom0000001.pdf ->gibyroom.pdf
gibyroom0000002.pdf ->gibyroom.pdf

Also I want to overwrite the file as I do not need to keep an outdated pdf file that is why my filename is just gibyroom.pdf

See More: How to rename file using batch fiel process

Report •

January 22, 2012 at 12:25:04
This should do it...

@echo off
setlocal enabledelayedexpansion
REM setting your variables
SET origin=c:\origin
SET destination=c:\destination
REM loading output of dir (bare and sorted) into tmp.tmp
dir %origin% /b /od > tmp.tmp
REM creating incremental variable to hold line count
set /a incr=0
REM looping through tmp.tmp to count lines
for /f %%a in ('type "tmp.tmp"^|find "" /v /c') do (
set /a incr=%%a
echo %incr%
REM incr variable is now equal to the number of lines from the dir output
REM setting skip variable equal to incr minus 1
set /a skip=%incr% - 1
REM looping through tmp.tmp to find newest file at bottom of list by skipping lines minus 1
for /f "skip=%skip%" %%a in (tmp.tmp) do (
echo %%a > newest_file.tmp
REM loading contents of newest_file.tmp into variable
set /p NewestFile=<newest_file.tmp
REM copying newest file to savepath
copy /Y "%origin%\%NewestFile%" "%destination%\gibyroom.pdf"

Report •
Related Solutions

Ask Question