How to rename a bunch of files to eliminate quote marks
simple single line bash code:
for f in *; do mv -i "$f" "${f//[\"[:space:]]}"; done
$f
is your current file name and ${f//[\"[:space:]]}
is your bash substring replacer which stands for:
in this f
(file name), //
(replace) these [\"[:space:]]
(characters) with nothing[1].
NOTE 1: string replacement statement: ${string//substring/replacement}
; because you don't need to replace your substring to nothing, leave /replacement
to blank.
NOTE 2: [\"[:space:]]
is expr
regular expression.
how to delete a file with quote in file name
If you only need to do this once in a while interactively, use
rm -i -- *
and answer y
or n
as appropriate. This can be used to get rid of many files having funny characters in their name.
It has the advantage of not needing to type/escape funny characters, blanks, etc, since the shell globbing with *
does that for you. It is also as short as it gets, so easy to memorize.
How to get rid of enclosing double-quotes in the expanded `forfiles` variables?
One approach is to nest a for %I
loop within the forfiles
and use the %~I
expansion -- use this code in a Command Prompt window:
forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %I in (@relpath) do echo %~I"
To use that code within a batch file you must double the %
-signs:
forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %%I in (@relpath) do echo %%~I"
The returned list of files will be (relying on the sample files from the original question):
.\file1.txt
.\file2.txt
Another variant is to nest another forfiles
in the body of the initial one, because forfiles
removes (non-escaped) double-quotes within given strings like the command line after /C
:
forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""
Or alternatively (the doubled inner forfiles
is intentional, this works around a bug -- see this post):
forfiles /P "C:\root" /M "*.txt" /C "forfiles forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\""
The inner forfiles
will enumerate exactly one item, which is the one passed over by the outer loop. Since @relpath
is already expanded when the inner loop is executed, the quotes are removed as they are not escaped.
So the returned list of files looks like (again taking the sample files from the original question):
.\file1.txt
.\file2.txt
The additional line-break between the lines is generated by forfiles
. You can avoid that using redirection (dismiss forfiles
output, but display only the echo
output in the console window):
> nul forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C 0x22cmd /C > con echo @relpath0x22"
How to use sed to remove all double quotes within a file
You just need to escape the quote in your first example:
$ sed 's/\"//g' file.txt
writing double quotes to file name text with powershell
In Windows you cannot use certain chars in a file or folder name;
a double quote is one of those.
Read more at https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file.
Excerpt from that page:
The following reserved characters [are not allowed]:
< (less than)
(greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
Removing double quotes from variables in batch file creates problems with CMD environment
You have an extra double quote at the end, which is adding it back to the end of the string (after removing both quotes from the string).
Input:
set widget="a very useful item"
set widget
set widget=%widget:"=%
set widget
Output:
widget="a very useful item"
widget=a very useful item
Note: To replace Double Quotes " with Single Quotes ' do the following:
set widget=%widget:"='%
Note: To replace the word "World" (not case sensitive) with BobB do the following:
set widget="Hello World!"
set widget=%widget:world=BobB%
set widget
Output:
widget="Hello BobB!"
As far as your initial question goes (save the following code to a batch file .cmd or .bat and run):
@ECHO OFF
ECHO %0
SET BathFileAndPath=%~0
ECHO %BathFileAndPath%
ECHO "%BathFileAndPath%"
ECHO %~0
ECHO %0
PAUSE
Output:
"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
Press any key to continue . . .
%0
is the Script Name and Path.%1
is the first command line argument, and so on.
Related Topics
Profiling Arbitrary Cuda Applications
Disable CPU Caches (L1/L2) on Armv8-A Linux
Built-In Kernel Driver Still Need Device Tree
Git - Crlf Issue in Windows + Linux Dual Boot
Find' (Command) Finds Nothing with -Wholename
How to Rename a Shared Library to Avoid Same-Name Conflict
Need Some Advise to Begin Programming on Arm (With Linux) Platform
Controlling The Boot Screen on Linux Embedded App
File/Directory Permissions Trailing + ( Drwxr-Xr-X+ )
Changing File Permissions Linux
How to Enable Mixed Mode Debugging in Visual Studio Code
How to Make Libusb Library Visible to Another Program
What Exactly Is Program Stack's Growth Direction
Linux Kernel Code in Memory Check with Sha256 Sum
Difference Between Archiving and Compression
List Files That Are in Directory1 But Not in Directory2 and Vice Versa
How to Restrict Access to Certain Files for a Certain Process