Another unzip and renaming question with batch files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bcvd
Posts: 4
Joined: 24 Feb 2015 13:35

Another unzip and renaming question with batch files

#1 Post by bcvd » 24 Feb 2015 14:00

Hi all,

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!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Another unzip and renaming question with batch files

#2 Post by foxidrive » 25 Feb 2015 00:13

Test this with unimportant dummy files:

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 /d "%dirA%"

echo All files in "%dirA%" to be uncompressed

FOR %%a IN (*.zip) do (
   "C:\Program Files\WinRAR\winrar.exe" e "%%a" "%dirE%"
)

cd /d "%dirE%"

echo Removing old PDF files

del *.pdf

echo Renaming PDF files

for /f "tokens=1,* delims=." %%a in ('dir "*.*.pdf" /b /a-d ') do ren "%%a.%%b" "%%a.pdf"

pause
goto :eof

bcvd
Posts: 4
Joined: 24 Feb 2015 13:35

Re: Another unzip and renaming question with batch files

#3 Post by bcvd » 25 Feb 2015 07:03

Thanks for the prompt reply!

It works. I had to move the delete command above the unzip command because otherwise I am deleting both the old and new files. When changing directory, why is it better to use /d?

I see that you were a little more creative in renaming than I was, using the filename to our advantage. Suppose I just wanted to take the left 9 characters, in case the file name structures were to change? Was the code above on the right path?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Another unzip and renaming question with batch files

#4 Post by Squashman » 25 Feb 2015 07:30

bcvd wrote:When changing directory, why is it better to use /d?

From the help file.

Code: Select all

Use the /D switch to change current drive in addition to changing current directory for a drive.

Post Reply