V15720002000
V15720002000

Reputation: 23

Rename multiple pdf files after reading corresponding text files present in different folder using bat script

PROBLEM

I am working on a bat script which should be able to read 1000 of text files from a location say Z:/Demo/Text and after reading the stored values the script is able to rename the corresponding pdf files present at say Z:/Demo/PDF. The text files are named and read in the following manner: SomeDate_Part1.txt, SomeDate_Part2.txt....

And the PDF files possess the same attribute just the file extension is different.

Renaming logic:

If the value stored in the SomeDate_Part1.txt file is AAA then the corresponding PDF file ie SomeDate_Part1.pdf should be renamed as CGI1_filename.pdf and if the stored value is BBB then it should be renamed as CGI2_filename.pdf.

I wrote a few lines and was able to successfully read all the text files from the folder. The code I wrote is:

@echo off
for %%x in (\Demo\Text\*.txt) do (
    for /f "usebackq delims=" %%a in ("%%~fx") do (
        if "%%a"=="AAA" echo %%a
        if "%%a"=="BBB" echo %%a
    )
)
pause

How can I solve the remaining issue?

Upvotes: 1

Views: 420

Answers (2)

lit
lit

Reputation: 16236

If you are willing to push ahead into PowerShell, this code might do what you want. I have not tested it. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.

Save this script as Rename-PdfFiles.ps1.

Get-ChildItem -File -Path '\Demo\Text' -Filter '*.txt' |
    ForEach-Object {
        $fn = $_.BaseName
        $pdfname = "\Demo\Text\$($fn).pdf"

        if (Test-Path -Path $pdfname) {
            $mark = Get-Content -Path $_
            if ($mark -eq 'AAA') { $prefix = 'CGI1' } else { $prefix = 'CGI2' }
            Rename-Item -Path $pdfname -NewName "$($prefix)_$(fn).pdf" -WhatIf
        }
    }

Run it from a cmd shell using:

powershell -NoProfile -File .\Rename-PdfFiles.ps1

If you cannot get past making a .bat file, this might work. I have not tested this either. If it outputs the correct RENAME commands, remove the echo at the beginning of the line.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%x IN ('\Demo\Text\*.txt') DO (
    SET "FN=%%~nx"
    SET "PDFNAME=\Demo\Text\!FN!.pdf"

    IF EXIST "!PDFNAME! (
        SET /P "MARK=" <"%%~x"
        IF "!MARK!" == "AAA" ( SET "PREFIX=CGI1" ) else ( SET "PREFIX=CGI2" )
        echo RENAME "!PDFNAME!" "!PREFIX!_!FN!.pdf"
    )

Upvotes: 0

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

set "AAA=1"
set "BBB=2"

for %%x in (\Demo\Text\*.txt) do (
    for /f "usebackq delims=" %%a in ("%%~Fx") do (
        ren "\Demo\PDF\%%~Nx.pdf" "CGI!%%a!_%%~Nx.pdf"
    )
)
pause

This code is practically a copy of the original with just two differences:

  • The values of AAA and BBB are set to 1 and 2, respectively. These values are stated in the question decription.
  • The two if commands that just display the values in the text files are changed by the desired ren command, as requested in the question description. In the ren command, the location of the .pdf file is set to \Demo\PDF as stated in the question. The new part of the name is comprised by CGI!%%a!_. The %%a contain the value stored in the text file that is AAA or BBB accordingly to the description. In this way, the !%%a! part becomes !AAA! or !BBB! that is replaced by the value of AAA or BBB via delayed expansion, so the new file name is CGI1_SomeDate_Part1.pdf or CGI2_SomeDate_Part2.pdf as requested in the question.

Upvotes: 1

Related Questions