简体   繁体   中英

Rename the pdf file inside sub-directories to the name of the sub directory

I am using win 7 platform. Need your help to create a bat file which simplifies my job. My folders and files are arranged in this structure.

File1

  • folder1 ->abcd.pdf
  • folder2 ->shhd.pdf.............................. folderN ->gfdfgd.pdf

File2

  • folder1 ->gbg.pdf
  • folder2 ->kjc67z.pdf.............................. folderN ->iuxz4i.pdf

--

--

--

FileN

  • folder1 ->ah455.pdf
  • folder2 ->jfhd45.pdf.............................. folderN ->juvxzr.pdf

I want to generate a batch file which renames the pdf file to name of the folder it contains. For example, in the above structure "abcd.pdf" is renamed to folder1.pdf. Respectively this is done for all pdf files.

Also a folder named "test" is created inside each directory(file1,file2..etc) which contains all the renamed pdf files of its respective directory.

If i copy all the folders inside File1 to Myfolder & run the bat file, a folder named test will be created and all the pdf file will be renamed and copied to test folder.

But what I want is, I want to run the bat file in H:\\ drive. Lets assume H:\\ drive contains all the directories File1, File2....FileN with each directory containing a sub-directory as I had shown in the above structure. If i run the bat file in H:\\ drive, then a folder named test has to be created inside each directory which contains the renamed pdf files of its respective sub-directories.

@ECHO OFF &SETLOCAL
for /F "delims=" %%a in ('dir /b /s /a-d *.pdf') do (
     set "fname=%%~fa"
     for %%b in ("%%~dpa.") do set "nname=%%~nxb"
     setlocal enabledelayedexpansion
     ECHO ren "!fname!" "!nname!.pdf"
     endlocal
)

Note: you can rename only one pdf/folder.


Some other code for the OP:

@ECHO OFF &SETLOCAL
MKDIR "H:\Myfolder\test"
for /f "delims=" %%i in ('DIR /b /a-d "H:\Myfolder"') do (
    ECHO %%i
    CD "H:\Myfolder\%%~i"
    ren *.pdf "%%~nxi.pdf"
    copy *.pdf "H:\Myfolder\test"
    cd H:\Myfolder
)
cd H:\Myfolder\test
del H:\Myfolder\test\test.pdf 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM