|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
哦。刚才看了一下生成的bpx 文件。发现了出问题的原因
呵呵。不好意思。。
这个代码只经过了echo 测试。。。
没有注意到 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
现在好了。
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.
|
|
2007-3-31 09:11 |
|
|
xswdong
中级用户
  
积分 216
发帖 129
注册 2007-2-14
状态 离线
|
『第 17 楼』:
使用 LLM 解释/回答一下
set FileEx=!FileEx:~0,-4!
这里的! 什么意思 ~0,-4 是从0开始忘左第4个字符吗? 请给解答
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.
|
|
2007-3-31 11:06 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c
pause
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c
pause
|
|
2007-3-31 12:29 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
Originally posted by xswdong at 2007-3-31 11:06 AM:
set FileEx=!FileEx:~0,-4!
这里的! 什么意思 ,-4 是从0开始忘左第4个字符吗? 请给解答
启用了变量延迟,%FileEx 必须写成 !FileEx
~0是指偏移量为0,从第一个开始提取变量FileEx的所有字符,除了最后四位。
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.
|
|
2007-3-31 12:38 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
Originally posted by bjsh at 2007-3-30 08:18 AM:
flyinspace的作息时间怎么黑白颠倒啊????
没办法。
忙的时候忙。。只有这个时间才有空来瞧瞧。。
能够帮忙的就顺手写咯。
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.
|

知,不觉多。不知,乃求知 |
|
2007-3-31 16:24 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 21 楼』:
flyinspace真是牛!!!
使用 LLM 解释/回答一下
我的要求达到了,考虑到能通用性,反过来将.C转为.TXT.
则需要改为如下:
@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
不过从简洁的角度出发,我偏向于wudixin96的思路.
当需要反过来转换时,只需要把扩展名参数颠倒一下就行了.
Last edited by zzhh612 on 2007-4-1 at 12:29 AM ]
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 ]
|
|
2007-3-31 23:34 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 22 楼』:
使用 LLM 解释/回答一下
我来简化下代码
- @echo off
- for /f "tokens=1 delims=." %%i in ('dir /b /s *.txt') do ren "%%i.txt" "*.c"
- pause
BJSH发表于: 2007-03-31 11:08
Last edited by bjsh on 2007-3-31 at 11:18 AM ]
I'll simplify the code
- @echo off
- for /f "tokens=1 delims=." %%i in ('dir /b /s *.txt') do ren "%%i.txt" "*.c"
- pause
BJSH posted on: 2007-03-31 11:08
Last edited by bjsh on 2007-3-31 at 11:18 AM ]
|
|
2007-4-1 00:09 |
|
|
zzhh612
中级用户
  
积分 278
发帖 103
注册 2006-10-21
状态 离线
|
『第 23 楼』:
wudixin96的代码的改进.
使用 LLM 解释/回答一下
@echo off
for /r %%a in (*.txt) do ren %%~nxa %%~na.c
pause
这个代码不能找到子目录中的文件.原因如下:
%%a是包含了完整路径的文件名.
%%~nxa 只能表示一个文件名+护展名,执行ren时,会提示找不到文件的路径
ren %%~nxa %%~na.c
相当于执行的是:ren *.txt *.c 这只针对当前目录.
所以第二句应为:for /r 路径 %%a in (*.txt) do ren %%a %%~na.c
相当于执行:ren 路径\*.txt *.c
@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
|
|
2007-4-1 00:22 |
|