N.B.
One can remove the preceding space individually like "REN ' IBM.txt'
'IBM.txt'" resulting in exactly IBM.txt. But "REN ' *.txt' '*.txt'"
causes absolutely no action--no error message. (I do not know why.) (If
I replace the space by a character (such as 1) beforehand then try "REN
'1*.txt' '*.txt", the result is the same--no action. Evidently I misused
the wildcard. I only need to know how to use wildcards so that thousand
files can be processed automatically.
My observation:
If one attempts to create ' abc.xyz' by inserting a space before a
filename using RENAME command in Windows Explorer, the space will
disappear as soon as Enter is hit. Is the space inserted by keyboard a
temporary empty space (NUL) whereas the space before a filename is a
space character (<<255>>)? I am confused.
Your simple ' xyz.txt' example does not lend a clue if any filenames have
other spaces in them. If none of the filenames have additional spaces in
them, the following one liner can be pasted and run at the command prompt to
remove the leading spaces.
for %a in (" *.txt") do ren "%a" %a
However, if any filenames have additional spaces in them (' x yz.txt'), then
use the following batch instead.
@echo off
setlocal enabledelayedexpansion
for %%a in (" *.txt") do (
set "fn=%%a"
ren "%%a" "!fn:~1!"
)
How about
for /f "tokens=*" %%i in ( ' dir /b /a-d " *.txt" ' ) do
ECHO ren " %%i" "%%i"
(all as one line - line will be wrapped.
* Reduce %% to % if executing directly from the prompt.
* remove ECHO keyword to execute the rename. It is there to verify what is
intended to be done
* the spaces surrounding the single-quotes within the parentheses are
superfluous and used for clarity.
)
OP: >> ..."the space will disappear"...
Isn't Windows helpful? Done "for your convenience."
an interesting effect billious.
@echo off
>a.txt echo abc
>>a.txt echo def
>>a.txt echo ghi
for /f "tokens=*" %%i in (a.txt) do ECHO "%%i"
echo ==========
for /f "delims=" %%i in (a.txt) do ECHO "%%i"
del a.txt
pause
--
Regards,
Mic
Hmm. I'd think in terms of delimiters.
with tokens=*, the lines are [delimiters]*text, all tokens delivered to
metavariable.
With delims=, there are obviously no delimiters, so there's only one 'token'
OTOH, if that theory's correct then theoretically, if the filename in
question had a name like
" ,lead.txt" then it should be renamed "lead.txt" since both space and comma
are default delimiters. It doesn't - the comma is retained.
" ;semi.txt" is completely ignored - I'd blame the FOR/F skips
leading-semicolon-lines characteristic (which raises the question of FOR/F
processing ANY line where the first non-delimiter (or possibly non-space) is
a semicolon - not just first-character-is-a-semicolon.)
" semi;colon.txt" seemed happy though.
I tested the proposed scripts to the best I understand, but none
appeared succsessful in real situation.
It seems that as long as
ren " *.txt" "*.txt"
is the core part of a script, it doesn't work. As I remarked in my
original post, it causes no action at all, not even error messages.
(You might be curious why I ended up with files with filenames preceded
by space (or blank or a white space or space/null character). As I
stated in the original post, the files downloaded from a data vendor
have filenames like "_xyzD.txt". In order to remove "_" and "D", I used
the following batch file:
Ren \temp\????.txt " ??.txt"
Ren \temp\?????.txt " ???.txt"
:
:
Ren \temp\????????.txt " ??????.txt"
This results in filenames without "_" and "D" but with a blank on the
top. (Ren \temp1\"_*.txt" "*.txt" causes no action.) Therefore the
problem seems to be how to use the * wildcard.)
MS offers "How to Remove Spaces from an MS-DOS Filename" (kb/65163)
but it is concerned only with a space in the middle of a filename.
As I remarked in the original post, you cannot insert a space before a
filename using RENAME command in Win Explorer. I think this fact has
something to do with why Ren " *.txt" "*.txt" does not work.
I think that the only succsessful way might be to treat a filename as a
string followed by clipping off the first character (nul character) or
by reducing the number of characters by one from the top. This idea was
suggested by someone but I am unable to write an appropriate script. Can
someone help?
I think you may have missed the point of the posts. While it is true
that wild card renames will not work, using the literal file name
will. The common way to expand a file name into its literal string is
using a FOR statement, as most of the previous posts did. The
simplest approach being a simple FOR from the command prompt,
something like this ...
t:\Testing>for %a in (" *.txt") do ren "%a" %a
This successfully renamed a file named [space]test.txt for me (the
[space] part being where the literal space is found). As other's have
said, this simple approach does not work correctly if the target
file's name has more than a leading space delimiter anywhere in it.
For example, a file named [space]test[space]case.txt results in a
syntax error, as the resulting command after the DO part of the FOR
has more that the two arguments that REN expects to see. That is, it
tries to execute this command ...
ren " test case.txt" test case.txt
where the part that is supposed to be the second argument actually
looks like three as far as the command processor is concerned. That's
the part that complicated all of the other suggested solutions.
If your files only have leading spaces and no impeded spaces, commas,
semicolons or equal signs, the simple approach should work fine.
Otherwise, I'd suggest you reexamine the earlier solutions and be more
specific about what did or did not happen when you tested them. (Note
that in some cases, the provided solutions might have an extra ECHO in
the solution before REN to illustrate what the rename statement looks
like in you environment. In such cases, the ECHO part must be removed
to actually rename files.)
_________________________________
Tom Lavedas