Batch script to remove # from a file name (only in a specific directory)
Solution 1
@echo off &setlocal
cd /d c:\users\data
for /f "delims=" %%a in ('dir /b /a-d *#*.pdf') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:#=!"
ren "!fname!" "!nname!"
endlocal
)
Solution 2
It's 2013, you should use PowerShell:
Get-ChildItem | Where-Object {$_.Name -match '#'} | ForEach-Object { Rename-Item -Path $_.Name -NewName $($_.Name -Replace "#", "") -WhatIf }
Navigate to the directory in question and execute the line. It finds all files with a # and then renames each of them. Remove the '-Whatif' to actually perform the operation.
A shorter version using aliases and defaults would be:
ls | ? {$_.Name -match "#"} | ForEach { rni $_.Name $($_.Name -Replace "#", "")}
As least the long version seems more readable to me than the 1980s batch syntax. The only cryptic part here is the '$.Name', '$' in PowerShell refers to the current object in the loop. Here in all cases to the current file. Because we need an expression for the -NewName parameter, we have to wrap it into $()
David
Updated on September 18, 2022Comments
-
David over 1 year
I'm looking for a script to remove the # character out of any file names in a specific directory (PDF documents).
I've been looking at some similar examples, but it's all a bit confusing!
-
tumchaaditya over 10 yearsHey, I am thinking of starting with powershell...used batch files for too long now....never bothered to switch to powershell....can you recommend a good place to get started with powershell
-
Peter Hahndorf over 10 years@tumchaaditya - Not off top of my head. I learned it by using it over the years. But there are tons of books and online resources out there.
-
David over 10 yearsThats great, thanks very much. Is it possible to get this to point to a specfic directory? i.e c:\users\data?
-
Endoro over 10 years@David look at my edit, pls.
-
David over 10 yearsPerfect, it works exactly how i need. Thank you very much for the help - very kind of you
-
Arthor about 4 yearsAny chance to include multiple characters such as ( ) and - as well
for /f "delims=" %%a in ('dir /b /a-d *#*') do (
-
Endoro about 4 years@Arthor you can replace '#' with '(', but there is no OR operator in Batch. So you have to write (c/p) the 'for /f' loop for each character. Maybe there is a better solution, but I haven't dealt with Batch for a long time.
-
Endoro about 4 yearsP.S. you may try
for /f "delims=" %%a in ('dir /b /a-d *#*.pdf *(*.pdf *)*.pdf') do (
, but I'm not sure if it will work if more than one character is to be replaced. -
Endoro about 4 yearsP.P.S.
*!*.pdf
will not work, because!
is a special character in Batch. -
Arthor about 4 years@Endoro I tired
for /f "delims=" %%a in ('dir /b /a-d '#' '-' '(' ')' ') do (
. No go.. I would like to do this command across all files, not only PDFs. -
Endoro about 4 years@Arthor so remove the extension:
for /f "delims=" %%a in ('dir /b /a-d *#* *(* *)*') do (
or replace the extension with an asterisks:for /f "delims=" %%a in ('dir /b /a-d *#*.* *(*.* *)*.*') do (
.