0

I have some files in nested folders with some dots in their name which I want to convert them into spaces. For example convert

802.11.Wireless.LAN.Fundamentals.Cisco.Press.eBook-kB.pdf

to

802 11 Wireless LAN Fundamentals Cisco Press eBook-kB.pdf

Notice that the final dot for extension (here before PDF) should not be removed, so I cannot use this script to do this task.

Community
  • 1
  • 1
living being
  • 213
  • 4
  • 10

3 Answers3

6
@echo off
    setlocal enableextensions disabledelayedexpansion

    set "root=c:\some\where"

    for /r "%root%" %%a in ("*.?*.pdf") do (
        set "filename=%%~na"
        setlocal enabledelayedexpansion 
        for %%f in ("!filename:.= !") do (
            endlocal  
            echo ren "%%~fa" "%%~f%%~xa"
        )
    )

This will recursively search under the indicated folder for files with .pdf extensions, that include at least two dots. For each file found, aditional dots in the file name (extension excluded) are replaced with a space.

To avoid problems with exclamation points in file names or paths, delayed expansion (needed to modify a variable inside a block and access the changed value) is enabled to remove the dots and disabled before executing the rename operation.

Rename operations are only echoed to console. If the output is correct, remove the echo command that precedes the ren

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • @npocmaka, no, this is not a competition. My code handles some exceptions your code doesn't handle. But this makes my code slower without even knowing if the aditional security is needed. Not better nor worse. Two different ways to solve the problem. – MC ND Feb 17 '15 at 10:58
2

My JREN.BAT hybrid JScript/batch utility can handle this very simply, directly from the command line. It is pure script that runs natively on any Windows machine from XP onward. It uses regular expression replacement to rename files or folders.

jren "\.(?=.*\.)" " " /s /p "c:\yourRootPathHere"

The command scans all the file names, replacing all periods that have at least one additional period later in the name.

Because JREN is a batch script, you must use call jren if you put the command within another batch script.

Use jren /? to get help on all available options - it is a powerful utility :-)

dbenham
  • 127,446
  • 28
  • 251
  • 390
1

not tested:

@echo off

set "root_dir=c:\pdfs"

setlocal enableDelayedExpansion
for /r "%root_dir%" %%a in (*.pdf) do (
 set "fnm=%%~na"
 set "nfnm=!fnm:.= !"
 rem remove echo if everything looks ok
 echo ren "%%a" "%%~dpa!nfnm!%%~xz"

)

You need to change the root_dir and to remove the echo in the last line if the echoed command is OK. The mask is put to *.pdf

npocmaka
  • 55,367
  • 18
  • 148
  • 187