![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-24 22:54 |
47,813 topics / 349,918 posts / today 1 new / 48,274 members |
| DOS批处理 & 脚本技术(批处理室) » How to batch change the file extensions of txt in multiple subdirectories. |
| Printable Version 2,666 / 22 |
| Floor16 flyinspace | Posted 2007-03-31 09:11 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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. |
|
| Floor17 xswdong | Posted 2007-03-31 11:06 |
| 中级用户 Posts 129 Credits 216 | |
|
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.
|
|
| Floor18 wudixin96 | Posted 2007-03-31 12:29 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c pause |
|
| Floor19 wudixin96 | Posted 2007-03-31 12:38 |
| 银牌会员 Posts 931 Credits 1,928 | |
Originally posted by xswdong at 2007-3-31 11:06 AM: 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. |
|
| Floor20 flyinspace | Posted 2007-03-31 16:24 |
| 银牌会员 Posts 517 Credits 1,206 | |
Originally posted by bjsh at 2007-3-30 08:18 AM: 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. |
|
| Floor21 zzhh612 | Posted 2007-03-31 23:34 |
| 中级用户 Posts 103 Credits 278 | |
|
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 ] |
|
| Floor22 bjsh | Posted 2007-04-01 00:09 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
I'll simplify the code
[ Last edited by bjsh on 2007-3-31 at 11:18 AM ] |
|
| Floor23 zzhh612 | Posted 2007-04-01 00:22 |
| 中级用户 Posts 103 Credits 278 | |
|
@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 |
|
| Prev 1 2 |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |