与enabledelayedexpansion问题的文件重命名批处理脚本 [英] issues with enabledelayedexpansion for file renaming batch script

查看:120
本文介绍了与enabledelayedexpansion问题的文件重命名批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个批处理脚本单调的文件更名。基本上,它使所有文件1 2 3 4 ....等称号。因为我已经扩大了它能够处理不同类型(TXT,DOC,FLV等)的文件,而不是一切工作了。

i am writing a batch script monotonic file renamer. basically, it makes the titles of all the files 1 2 3 4 .... and so on. i have since expanded it to be able to handle files of different types (txt, doc, flv, etc) but not everything is working out.

我主要关注的是我已经打破了延迟扩展呼叫我是拍前。现在用的!VAR1!是永远不会扩大,或者从来不承认作为一个变量。

my main concern is i have broken the delayed expansion calls i was making before. now using !var1! is never expanded, or never recognized as a variable.

这是我的脚本的冗长注释版本

here is a verbosely commented version of my script

::a monotonic file renamer
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

SET tempfile=temp.txt
SET exttemp=exttemp.txt
if [%1] == [] goto usage

::make sure your dont overwrite something useful
if EXIST %tempfile% (
ECHO Temp file already exists, are you sure you want to delete?
del /P %tempfile%
)
if EXIST %exttemp% (
ECHO EXT Temp file already exists, are you sure you want to delete?
del /P %exttemp%
)

::initialize 
SET /a counter=0
SET type=
SET /a ender=%1

::write filenames to tempfile
DIR /B /ON > %tempfile%

::read lines one by one
for /f "usebackq delims=" %%a in (%tempfile%) do (
REM make sure we do not rename any of the working files
if NOT "%%a"=="renamer.bat" (
if NOT "%%a"=="temp.txt" (
if NOT "%%a"=="exttostr.bat" (
SET /a counter+=1
REM get file extension
exttostr %%a > %exttemp%
SET /P type= < %exttemp%
REM housekeeping
del /F %exttemp%
REM rename
ren %%a !counter!.!type!
ECHO Renamed "%%a" to "!counter!.!type!"
)))
REM exit when we have run enough
if "!counter!"=="!ender!" goto exit
)

goto exit

:usage
echo Usage: renamer NUMFILES

:exit
::final housekeeping
DEL temp.txt

这个想法是我将我的两个文件,​​renamer.bat(本文件)和exttostr.bat(辅助来获取文件扩展名)进入该文件夹,然后运行它,它会从1按字母顺序排列的文件重命名为怎么过多少文件我指定的。

the idea is i drop my two files, renamer.bat(this file) and exttostr.bat(helper to get the file extension) into the folder and run it, it will rename files sorted alphabetically from 1 to how ever many files i specify.

当我运行code,它绝不会使用标记为延迟扩展得体,总是让他们为!VARNAME!的变量,所以它重命名的第一个文件!柜台!!型!并引发错误的休息,因为已经有与该名称目录中的文件。

when i run the code, it never uses the variables marked for delayed expansion appropriately, always leaving them as "!varname!", so it renames the first file "!counter!.!type!" and throws errors for the rest because there is already a file in the directory with that name.

这使我想起一个次要问题。排序目录列表​​按字母顺序导致差的处理编号的文件。例如名单:
1 7 15 75 120
排序:
1 120 15 7 75
我一直没能找到解决的办法呢,只知道它的确是DIR排序的预期结果。唯一的解决办法我已经是在前面足够的零填充数字。

this brings me to a secondary issue. sorting the dir list alphabetically results in a poor handling of numbered files. for example the list: "1 7 15 75 120" is sorted: "1 120 15 7 75" i have not been able to find a way around this yet, only that it is indeed the intended result of the dir sort. the only workaround i have is padding numbers with enough zeroes in the front.

在此先感谢任何见解!

一切进行排序,但第二个问题。我想我还没有说得好。我有这个问题,当我把目录中的文件名,不写出来的时候。因此他们已经需要被填充。我一直希望有一些其他的方式来读取目录,并将它进行适当的排序。

everything is sorted but the second problem. i think i have not spoken well. i have this issue when i take IN the directory file names, not when writing out. so they already need to be padded. i has hoping there was some other way to read the directory and have it be sorted appropriately.

最有前途的事情,我发现在这里:的http:// www.dostips.com/Dt$c$cBatchFiles.php#Batch.SortTextWithNumbers

the most promising thing i have found is here: http://www.dostips.com/DtCodeBatchFiles.php#Batch.SortTextWithNumbers

    @ECHO OFF
if "%~1"=="/?" (
    echo.Sorts text by handling first number in line as number not text
    echo.
    echo.%~n0 [n]
    echo.
    echo.  n     Specifies the character number, n, to
    echo.        begin each comparison.  3 indicates that
    echo.        each comparison should begin at the 3rd
    echo.        character in each line.  Lines with fewer
    echo.        than n characters collate before other lines.
    echo.        By default comparisons start at the first
    echo.        character in each line.
    echo.
    echo.Description:
    echo.        'abc10def3' is bigger than 'abc9def4' because
    echo.        first number in first string is 10
    echo.        first number in second string is 9
    echo.        whereas normal text compare returns 
    echo.        'abc10def3' smaller than 'abc9def4'
    echo.
    echo.Example:
    echo.        To sort a directory pipe the output of the dir
    echo.        command into %~n0 like this:
    echo.           dir /b^|%~n0
    echo.
    echo.Source: http://www.dostips.com
    goto:EOF
)

if "%~1" NEQ "~" (
    for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
    goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
    set f=,%%B
    (
        set f0=!f:~0,%n%!
        set f0=!f0:~1!
        rem call call set f=,%%%%f:*%%f0%%=%%%%    
        set f=,!f:~%n%!
    )
    for /f "delims=1234567890" %%b in ("!f!") do (
        set f1=%%b
        set f1=!f1:~1!
        call set f=0%%f:*%%b=%%
    )
    for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`@#$*_-+=:;',.?/\ " %%b in ("!f!") do (
        set f2=00000000000000000000%%b
        set f2=!f2:~-20!
        call set f=%%f:*%%b=%%
    )
    echo.!f1!!f2!!f!,%%B
    rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)

这code可以在其中一个数字文件名(即video100.mov是好的,video100video10.mov会打破它)

this code can sort the filenames with one number in them (i.e. video100.mov is fine, video100video10.mov would break it)

这个问题我已经是我想加入到这个帮手FN通话将再次突破了,所以我将试图在我的修改renamer.bat现在包括这个。任何帮助是AP preciated。

the issue i have is i think adding a call to this helper fn will break it again, so i will be trying to include this in my modified renamer.bat now. any help is appreciated.

推荐答案

大概批量提取扩展复位当地的环境。

Probably the batch for extracting the extension reset the local environment.

但是,你不需要它。您可以提取与〜X 选项扩展。与此类似....

But, you don't need it. You may extract the extension with the ~x option. Something similar to this ....

:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
  if exist %%~fa (
    set /a counter += 1
    echo ren %%~fa !counter!%%~xa 
  )
)
goto :eof

要包括领导在柜台零,因此该目录排序正确,更换previous三行重命名命令

to include leading zeroes in the counter, so that the directory sorts correctly, replace the previous rename command with three lines

set zcounter=0000!counter!
set zcounter=!zcounter:~-4!
echo ren %%~fa !counter!%%~xa 

所以把所有拼在一起,添加刚刚在批处理文件中创建的monotonicrename功能,可以作为简单,因为...

So putting all pieces together, add the monotonicrename function you just created in the batch file that can be as simpler as...

@echo off
setlocal enabledelayedexpansion
call :monotonicrename %1
goto :eof
:monotonicrename
set /a counter = 0
for %%a in (%1\*.*) do (
  if exist %%~fa (
    set /a counter += 1
    set zcounter=0000!counter!
    set zcounter=!zcounter:~-4!
    echo ren %%~fa !zcounter!%%~xa 
  )
)
goto :eof

这篇关于与enabledelayedexpansion问题的文件重命名批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆