China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-30 17:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to batch change the file extensions of txt in multiple subdirectories. View 2,276 Replies 22
Floor 16 Posted 2007-03-31 09:11 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh. Just took a look at the generated bpx file. Found the reason for the problem.

Hehe. Sorry about that..

This code was only tested with echo...
Didn't notice the syntax issue with ren..

@echo off & SETLOCAL EnableDelayedExpansion
dir /b /s *.txt>1.pxp
set FileEx=
for /f "tokens=* " %%i in (1.pxp) do (
set FileEx=%%i
set FileEx=!FileEx:~0,-4!
ren "!FileEx!.txt" "*.pxp"
)
pause

Now it's good.
Floor 17 Posted 2007-03-31 11:06 ·  中国 黑龙江 牡丹江 电信
中级用户
★★
Credits 216
Posts 129
Joined 2007-02-14 07:26
19-year member
UID 79469
Gender Male
Status Offline
In batch scripting (commonly used in DOS environments), the `!` here is used for enabling delayed expansion. When delayed expansion is enabled, `!variable!` is used to access the current value of a variable. The `~0,-4` part is a substring extraction operation. In batch variable substring extraction, `%variable:~start,length%` is the basic syntax. Here, `~0,-4` means to take a substring starting from index 0 and taking all characters except the last 4 characters. So it's extracting a substring starting at position 0 and excluding the last 4 characters of the `FileEx` variable.
Floor 18 Posted 2007-03-31 12:29 ·  中国 浙江 杭州 拱墅区 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c
pause
Floor 19 Posted 2007-03-31 12:38 ·  中国 浙江 杭州 拱墅区 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Originally posted by xswdong at 2007-3-31 11:06 AM:
set FileEx=!FileEx:~0,-4!
What does the ! mean here, and is -4 the 4th character from the left starting from 0? Please give an explanation.


Variable expansion with delayed expansion is enabled, so %FileEx must be written as !FileEx. ~0 means an offset of 0, extracting all characters of variable FileEx from the first one, excluding the last four.
Floor 20 Posted 2007-03-31 16:24 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by bjsh at 2007-3-30 08:18 AM:
Why does flyinspace have a reversed schedule, staying up late and getting up late?

No way.

When busy, I'm really busy. Only at this time do I have time to take a look..

If I can help, I'll just write it down.
知,不觉多。不知,乃求知
Floor 21 Posted 2007-03-31 23:34 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
My requirement is met. Considering generality, conversely, convert.C to.TXT.

Then it needs to be changed to the following:

@echo off & SETLOCAL EnableDelayedExpansion
dir /b /s *.c>temp.pxp
set FileEx=
for /f "tokens=* " %%i in (temp.pxp) do (
set FileEx=%%i
set FileEx=!FileEx:~0,-2!
ren "!FileEx!.c" "*.txt"
)
del temp.pxp
pause


But from the perspective of simplicity, I prefer the idea of wudixin96.
When it is necessary to convert conversely, only need to reverse the extension parameters.

[ Last edited by zzhh612 on 2007-4-1 at 12:29 AM ]
Floor 22 Posted 2007-04-01 00:09 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
I'll simplify the code



  1. @echo off
  2. for /f "tokens=1 delims=." %%i in ('dir /b /s *.txt') do ren "%%i.txt" "*.c"
  3. pause
BJSH posted on: 2007-03-31 11:08


[ Last edited by bjsh on 2007-3-31 at 11:18 AM ]
Floor 23 Posted 2007-04-01 00:22 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c
pause

The reason why this code cannot find files in subdirectories is as follows:
%%a is the file name including the full path.
%%~nxa can only represent a file name + extension. When executing ren, it will prompt that the path to the file is not found.
ren %%~nxa %%~na.c
is equivalent to executing: ren *.txt *.c, which is only for the current directory.

So the second sentence should be: for /r path %%a in (*.txt) do ren %%a %%~na.c
which is equivalent to executing: ren path\*.txt *.c
Forum Jump: