Renomeando datas no windows cmd

0

Eu tenho um monte de diretórios (pastas, se você quiser) que seguem esse padrão

121022 Description of the directory's contents goes here\  

(alguns não têm, apenas a data)

e preciso renomeá-los para seguir o seguinte padrão

12-10-22 Description of the directory's contents ...\

Existe uma maneira de fazer isso usando o Windows cmd e as ferramentas que o acompanham (ou seja, ren)? Se não, qual seria a maneira mais portátil de fazer isso? Eu tenho um conjunto muito restrito de privilégios na máquina em que estou fazendo isso.

    
por Rook 14.11.2012 / 22:04

1 resposta

4

Se você começar com o nome do diretório em uma variável, como fdir:

set "fdir=20121022 Description of this directory"

Em seguida, seu arquivo em lotes pode dividir o nome do diretório:

set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"

neste ponto, as variáveis parecem:

fdir=20121022 Description of this directory
fdiryear=2012
fdirmonth=10
fdirday=22
fdirdesc= Description of this directory

Em seguida, defina o novo nome do diretório e renomeie o diretório:

set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
ren "%fdir%" "%fnew%"

o novo nome do diretório será:

fnew=2012-10-22 Description of this directory

Se você puder me fornecer mais algumas informações sobre como deseja coletar a lista de nomes de diretório, posso fornecer um script mais completo.

Por exemplo, se todos os diretórios estiverem no diretório atual, o arquivo em lote ficaria assim:

@echo off

echo.
for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"
echo.
set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="
goto :EOF


:work
set fdir=%~1
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"

set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
echo Renaming folder "%fdir%" to "%fnew%"
rem    ren "%fdir%" "%fnew%"
goto :EOF

Explicações:

Isso desativará o eco dos comandos conforme eles são executados para evitar muita confusão na tela:

@echo off

Isso exibirá uma linha em branco:

echo.

O comando for:

for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"

/D means to search for matches of Directory names only.
If you omit the /D, it will search for matches of Filenames only.

%%f (%%~f) is a variable name that gets set to the names of the matched directories.

[in ("2010*", "2011*", "2012*")] is a list of patterns to search for. 
In this case, it will only process directories that begin with a year: 
2010, or 2011, or 2012. You can edit this list for your needs. If you 
know that all directories in the current folder are in the same format 
and are candidates to be renamed, the list can be simply: ("*") like:  
for /D %%f in ("*") do call :work "%%~f"

the word "do" simply precedes the command that you want to "do" for each 
match found.

call :work says to execute the commands at the label :work for each match.

"%%~f" says to pass the matched directory name as the first argument to the 
commands at the label you specified (:work)  

The "~" in "%%~f" says to remove "quote" marks from %%f to avoid passing the 
directory name inside of 2 sets of "quote" marks. For example, if %%f contained 
"20121022 Description of this directory" then using "%%f" would pass 
""20121022 Description of this directory"", which would fail. "%%~f" will pass 
exactly 1 set of "quotes".

Depois que todos os nomes de diretórios correspondentes são processados, a execução retorna ao "eco". comando que segue o comando "for ...".

A seguir, há apenas algumas "limpezas" para limpar as variáveis usadas no script, para que elas não se acumulem e confundam o espaço variável (ambiente).

set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="

A próxima instrução sai do script em lote e termina a execução.

goto :EOF

Em seguida, o marcador

:work

Labels begin with a colon at the first column followed by a 
label name made up of letters and numbers (no spaces). Labels are 
the targets of call and goto commands.  

Em seguida, o comando:

set fdir=%~1

Sets the variable named "fdir" to be the directory name that was 
passed from the "for" command.

The "~" in %~1 means to use the name only without any surrounding "quote" marks.

Os próximos comandos:

set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"

Split up the directory name into the desired pieces, and 
define the "new" directory name.

O próximo comando:

echo Renaming folder "%fdir%" to "%fnew%"

Just displays the old and new directory names so you can see the 
progress on the screen.

E os últimos comandos:

rem    ren "%fdir%" "%fnew%"
goto :EOF

Renames the directory and then "exits" to return from the call.

Observação: fiz o comando real para renomear o diretório como um comentário para que você possa executar o arquivo em lote e ver o que acontecerá sem realmente renomear nada. Depois de executá-lo e ter certeza de que os diretórios corretos serão renomeados corretamente, você pode editar a linha para renomear o diretório e executar o script em lote novamente.

Para fazer isso, remova o "rem" da linha:

rem    ren "%fdir%" "%fnew%"

Becomes:

ren "%fdir%" "%fnew%"
    
por 14.11.2012 / 23:32