Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to remove a space on top of filenames.

1,559 views
Skip to first unread message

Beyond X

unread,
Oct 13, 2011, 10:32:34 PM10/13/11
to
I need to remove a space preceding filenames of many (ca. 1000) text files.
Those files are originally obtained as "_xyzD.txt" from a market data
vendor.(xyz represents a symbol and thus must be preserved.) When I
remove '_' and 'D' using a series of batch scripts, the end products are
' xyz.txt'.
How to remove this preceding space using an automated batch process is
my problem. I am aware that more than a few filename changers are
available, but they are all, as long as I know, Windows/Linux programs
and thereby difficult, if not impossible, to integrate into an automated
batch process. I have posted a similar message to the WinXP group, but,
despite several people trying to help me, I have not solved the problem.
I cannot use those filename changer utility programs that operate on
Windows environments. Someone suggested to construct a script where
filenames are reduced to a string and then apply a procedure to shorten
the string by one character. If this is possible with a batch script, it
will be a wow but I have no idea how to do it.
Can someone help me to remove the preceding space by any batch way possible?

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.

Todd Vargo

unread,
Oct 13, 2011, 11:50:58 PM10/13/11
to

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!"
)

billious

unread,
Oct 14, 2011, 2:52:18 AM10/14/11
to

"Todd Vargo" <tlv...@sbcglobal.netz> wrote in message
news:j78bk4$mmd$1...@news.albasani.net...
> Beyond X wrote:
[snip]

>
> 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."


foxidrive

unread,
Oct 14, 2011, 4:27:42 AM10/14/11
to
On 14/10/2011 17:52, billious wrote:
> "Todd Vargo"<tlv...@sbcglobal.netz> wrote in message
> news:j78bk4$mmd$1...@news.albasani.net...
>> Beyond X wrote:
> [snip]
>>
>> 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"


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

billious

unread,
Oct 14, 2011, 8:06:36 AM10/14/11
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:8oSlq.1111$Ml3...@newsfe17.iad...

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.


Beyond X

unread,
Oct 14, 2011, 3:31:54 PM10/14/11
to
Thanx to all for kind replies.

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?

Tom Lavedas

unread,
Oct 14, 2011, 3:05:59 PM10/14/11
to

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

Beyond X

unread,
Oct 14, 2011, 10:41:42 PM10/14/11
to
Thank you very much for your time kindly trying to help me.

My platform is Windows XP Pro SP2. (I also have access to Windows 2000
Pro SP4, Windows 98 and Windows 7 if needed. I have not studied possible
diferences among corresponding commands if that makes differences in my
present project.)

That said, I tried to reproduce your method that you say successfully
removed the space from " test.txt". But I failed.
Would you please show me letter to letter the script you tested?

Beyond X

unread,
Oct 14, 2011, 10:50:51 PM10/14/11
to
There is only one space in all files, i.e., before filenames.

When I carry out
for %a in (" *.txt") do ren "%a" %a
it returns a rather peculiar error message:
a" a was unexpected at this time.

Beyond X

unread,
Oct 14, 2011, 11:17:25 PM10/14/11
to
I do not understand what dir /b /a-d means. In my case all files are
in one directry (\temp1).
When I skip this part and make the part (\temp1\" *.txt"), I get an
error message: The system cannot find the file \temp1\" *.txt".
Actually everytime when " *.txt" is used in association with ren
command, the system seems to fail to find files to process. I don't know
why.

foxidrive

unread,
Oct 14, 2011, 10:28:17 PM10/14/11
to
check your command very carefully, it should work.


I pasted the commnd into my command prompt and you can see that it worked below.


d:\AAA>for %a in (" *.txt") do ren "%a" %a

d:\AAA>ren " a.txt" a.txt

d:\AAA>dir *.txt /b
a.txt


--
Regards,
Mic

Todd Vargo

unread,
Oct 14, 2011, 11:11:56 PM10/14/11
to
We have no idea of which post you are responding to when you exclude the
entire previous post. For best results, retain a relevant portion of the
posts when you respond so others will know which post your comment relates
to.

As far as the error above goes. This is expected when you paste the line
above in a batch file instead of running at the command prompt as I had
indicated in my post. To use this command in a batch file instead, you must
double the percent symbols as follows.

for %%a in (" *.txt") do ren "%%a" %%a

Todd Vargo

unread,
Oct 14, 2011, 11:15:48 PM10/14/11
to

"Beyond X" <no-...@mail.com> wrote in message
news:4E988E2A...@mail.com...
> Thanx to all for kind replies.
>
> I tested the proposed scripts to the best I understand, but none appeared
> succsessful in real situation.

The code that I posted (as posted) works.


>
> 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.

It appears you thirst for an explanation of why your command does not work.
The problem is in your destination of "*.txt". The * in the destination
specifies to retain the entire basename (including the leading space).

HTH

billious

unread,
Oct 14, 2011, 11:26:11 PM10/14/11
to

"Beyond X" <no-...@mail.com> wrote in message
news:4E98F50B...@mail.com...
You get this message if you attempt to execute that FOR command as a line
within a batch file.

If you execute the line directly from the prompt, it should work.

If it's executed within a batch file, it's interpreted as

for
%a in (" *.txt") do ren "%
a" %a

and since the environment variable [%a in (" *.txt") do ren "%] doesn't
exist, that is replaced by [nothing]

hence batch attempts to execute

for a" %a

yielding the message you report.

If you have the line within a batch file (which makes it a lot easier to
edit) then you need to double the "%" on each reference to the metavariable
(the FOR loop-control variable - "a" in this instance)

for %%a in (" *.txt") do ren "%%a" %%a

should work WITHIN a batch file





billious

unread,
Oct 15, 2011, 12:00:06 AM10/15/11
to

"Beyond X" <no-...@mail.com> wrote in message
news:4E98FB45...@mail.com...
Ah - the "I don't understand, so I'll ignore it" school of thinking. Please
get yourself expelled from that institution - any way you can manage it,
else you're doomed to wander through life bemused and befuddled.

Generally, from the prompt,

commandname /?

will yield help (sometimes cryptic help) for commandname.

with a FOR /F command, each line of input is processed in turn by the DO
part.

If you have filenames in the parentheses, the lines of input are drawn from
the files mentioned.

If the parentheses enclose a command between single quotes, then the command
is executed and the response which would normally be transmitted to the
console will be processed instead by the FOR/F.

hence

FOR/F ... in ( ' dir /b /a-d "\temp\ *.txt" ' ) do ...

will execute a DIR /B /A-D command on the directory \temp\, looking to match
filenames of the form
" *.txt" (it doesn't appear to matter whether the double-quotes enclose the
entire string or simply the filemask.)

DIR /B /A-D will produce a BASIC report (ie filenames only) and suppress
directorynames - see DIR/? from the prompt for documentation.

In this case, the output of the DIR /B /A-D command would be

[space]filename1.txt
[space]filename2.txt

The 'tokens=*' option to FOR/F conveniently removes the leading space(s)
before applying each remainder-of-the-line in turn to %%i hence

... ren " %%i" "%%i"

renames the filename WITH the space prepended to WITHOUT.

Because the syntax of the REName command is REN [directoryname\]filename
newfilename then in your case, since you are executing the DIR on a
(potentially) foreign directory, you'd need to include the directoryname in
the FROM-name (otherwise the rename is attempted in the current directory)

hence,

... ren "\temp\ %%i" "%%i"

The double-quotes on the destination filename are for completeness. In your
case, you claim not to have spaces within the filename. If you HAD spaces
within the filename, the quotes would be required. Since you don't they're
optional. The FROM (full-)filename requires the quotes since it contains
spaces.


Dr J R Stockton

unread,
Oct 16, 2011, 2:02:45 PM10/16/11
to
In alt.msdos.batch.nt message <4E979F42...@mail.com>, Thu, 13 Oct
2011 21:32:34, Beyond X <no-...@mail.com> posted:

>I need to remove a space preceding filenames of many (ca. 1000) text files.
>Those files are originally obtained as "_xyzD.txt" from a market data
>vendor.(xyz represents a symbol and thus must be preserved.) When I
>remove '_' and 'D' using a series of batch scripts, the end products
>are ' xyz.txt'.


You should have retained the original files and done your experimenting
on copies, of course.

You should also have given a few examples, to help us understand which
parts of "_xyxD.txt" are constant, and which can vary, and how they can
vary.

The best way to work is to get a list of filenames using dir /b,
redirect that into a file, then use some editor that you understand, and
that is better than Notepad, Wordpad, etc. to create a new file which is
a batch file containing the necessary renaming files. Then, when you
are certain of that batch file being correct, try it on a COPY of your
real files.

For the editing, I might use COLS, from my Web programs directory, or
else MiniTrue (see <www.merlyn.demon.co.uk/pc-links.htm>); or I might
use a programmers' editor that can handle RegExps. Others might
recommend something like SED, AWK, or their siblings.

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Never fully trust an article from a poster who gives no full real name.

Beyond X

unread,
Oct 17, 2011, 1:27:29 AM10/17/11
to
Thanks a lot to everyone who tried to help me solving my problem.

I at last found where I had erred. Namely:
In the syntax of the For command [ For %%parameter In (fileset) Do
command ]
I had mistakenly believed that the (fileset) could/should include
direction to where files are (something like (\directory\files)). In
other words I did not know that the fileset should be just a name of
target file and that the For command must be executed within the
directory where the target files were. Looking back, your suggestions
were hinting it, but this poor guy just didn't pick it up. What I needed
was inclusion of a SET option to specify where files are.
At any rate, my final and working script now contains two additional
sub-batch files:

E.BAT [CD\TEMP]
F.BAT [For %%H in (" *.txt") do ren "%%H" %%H]

(These are actually the last two of the total seven sub-batch files
added into the (fileset) of the mother For batch file. In the latter,
those sub-batch files constitute the (fileset) as shown below:
For %%H In (\A.bat \B.bat C.bat D.bat E.bat F.bat) Do Call %%H

Again please accept my appreciation for your mind and time.

N.B.
I am still curious about why
[ren " *.txt" "*.txt"] or [ren a*.txt *.txt] or [ren aa*.txt a*.txt]
causes no action and no error messages.
Also I wonder why
[ren 1a*.txt a*.txt]
results in aa*.txt.
0 new messages