I'm new to using batch files and I've been perusing the board for a bit trying to find a solution for my renaming issue. On my network, I am trying to extract .pdf files from a .zip and rename them with the first 9 characters of the filename.
Original filenames look like this:
055CDGEF2.BW5002.2523368.20150220.133242.6304.pdf
0789DGEF2.BW5002.2523368.20150220.133242.6304.pdf
I want revised filenames to be:
055CDGEF2.pdf
0789DGEF2.pdf
The original files are date-stamped, so they will be different every day. The renamed files will be the same names every day, so I will need to overwrite. That also means I'll need to delete all existing .pdf files in my "extract to" folder before extracting and re-naming.
Here's what I've got so far from looking at other examples:
Code: Select all
@echo off
setlocal
REM - Compressed file folder_-
set dirA=S:\xx\reports
REM - Folder to extract -
set dirE=s:\xx\reports
cd %dirA%
set path="C:\Program Files\WinRAR\";%path%
echo All files in %dirA% to be uncompressed
FOR %%i IN (*.zip) do (
winrar e "%%~ni.zip" "%dirE%"
)
goto eof
:eof
endlocal
@pause
:end
EXIT /B 0
That just extracts but doesn't delete any existing .pdf files. Now I want to rename. I've been trying to get the following to work in a separate batch file, to no avail:
Code: Select all
SETLOCAL ENABLEDELAYEDEXPANSION
set dirE=s:\xx\reports
cd %dirE%
FOR %%i IN (*.pdf) do (
set newname=%%i
set newname=%newname:~0,9%
Rename "%%~i" "newname.pdf"
)
@pause
This doesn't work. I can see it is currently looking in the correct directory and is looping through each .pdf file, but it doesn't do anything for each of them (except the first, which it just renames to a file called "newname").
Any help with the re-naming and merging it all together would be much appreciated! Bonus for anything you can explain so I don't have to keep asking newb questions!