2

I am very new to batch scripting.

There are solutions regarding this for linux and macos, but I need it for windows.

I have the following, which only works on special characters !@#$%^&*~ etc. basically the ones you can see on your keyboard. But I need to remove special characters like  and °. The following scripts do not work on those two specific characters. , a script I can run via cmd. There is this but it is not winows7 cmd compatible.

@echo off &setlocal
cd /d c:\users\data
for /f "delims=" %%a in ('dir /b /a-d *#*.txt') do (
    set "fname=%%~a"
    setlocal enabledelayedexpansion
    set "nname=!fname:#=!"
    ren "!fname!" "!nname!"
    endlocal
)

And the following which can remove spaces in file names only

@echo off
setlocal disableDelayedExpansion
if /i "%~1"=="/R" (
  set "forOption=%~1 %2"
  set "inPath="
) else (
  set "forOption="
  if "%~1" neq "" (set "inPath=%~1\") else set "inPath="
)
for %forOption% %%F in ("%inPath%* *") do (
  if /i "%~f0" neq "%%~fF" (
    set "folder=%%~dpF"
    set "file=%%~nxF"
    setlocal enableDelayedExpansion
    echo ren "!folder!!file!" "!file: =!"
    ren "!folder!!file!" "!file: =!"
    endlocal
  )
)

2 Answers 2

1

Your first script worked for me if you deal with the fact that code page 437 must be used for your script.

Paste the below script into Notepad. Save as test.bat with ANSI encoding.

@echo off &setlocal
cd /d c:\users\data
for /f "delims=" %%a in ('dir /b /a-d *ø*.txt') do (
    set "fname=%%~a"
    setlocal enabledelayedexpansion
    set "nname=!fname:ø=!"
    ren "!fname!" "!nname!"
    endlocal
)

The above script will rename degree°.txt to degree.txt.

ø is used because it is Unicode U+00F8. The degree symbol is 0xF8 in code page 437.

 does not appear in code page 437, so I'm not aware of a parallel solution. It's probably required to change the code page as directed in this answer to do it properly. Here's an attempt at a batch file that includes changing the code page.

echo Ignore the error displayed below
@echo off &setlocal
chcp 65001>nul
cd /d c:\users\data
for /f "delims=" %%a in ('dir /b /a-d *Â*.txt') do (
    set "fname=%%~a"
    setlocal enabledelayedexpansion
    set "nname=!fname:Â=!"
    ren "!fname!" "!nname!"
    endlocal
)

Paste the above in a Notepad file and save with encoding UTF-8. It's necessary to include the first line of the script because the batch file will give an error initially processing the UTF-8 file.

1
  • That is totally sweet @davidmneedham! I learned a new trick today and that doesn't happen very often. :) May 30, 2019 at 20:18
0

Wish I could take credit for this, but it was something I stumbled across. I can't find the post I found it in, so this is from memory.

find . -depth -name "*" -print0 | $var -0 sed -i 's/[:cntrl:]/_/g' *

You may have to play with it a bit, I'm just taking a couple of minutes from a project of mine to post this. Share your results please.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .