0

友人のために古いデータをクリーンアップしようとしています。データは次のような複数のフォルダに配置されます。

  1. C:\ Tally \ Data \ 0000
  2. D:\ Tally \ Data \ 1092
  3. C:\ New folder \ Tally7.2 \ Data \ 0001

ここで、Everythingという検索エンジンで正規表現^ [0-9] [0-9] [0-9] [0-9] $を使用して、0000のような4桁の名前を持つすべての可能なフォルダーを検索しました。両側を二重引用符で囲んだ、すべての行に1つのディレクトリパスを含むテキストファイルにエクスポートしました。

次のことを行うためにバッチスクリプトを作成する必要があります。

  1. テキストファイルを1行ずつ読みます。
  2. その名前のフォルダが宛先ディレクトリにすでに存在するかどうかを確認してください。
  3. はいの場合は、フォルダの名前を変更してコピーします。
  4. そうでない場合は、フォルダの名前を変更せずにコピーします。
  5. 最後に、宛先のすべてのフォルダーの名前を0000から順番に変更します。

どうすればこれを達成できますか?

4

1 に答える 1

0
@echo off
setlocal EnableDelayedExpansion
set destination=C:\put the destination folder here
set newName=10000
rem Read the text file line by line
for /F "delims=" %%a in (thefile.txt) do (
   rem Check if a folder with that name already exists in the destination directory.
   set "folder=%%~Na"
   if exist "%destination%\!folder!" (
      rem If yes, then rename the folder and copy it.
      call :getNewName folder
   )
   rem If not, copy without renaming the folder.
   md "%destination%\!folder!"
   copy "%%~a" "%destination%\!folder!"
)
rem Finally, I want to sequentially rename all the folders in the destination starting with 0000. 
cd /D "%destination%"
set n=9999
for /D %%a in (*) do (
   set /A n+=1
   if "%%a" neq "!n:~-4!" (
      ren "%%a" "!n:~-4!"
   )
)
goto :EOF

:getNewName folder
   set /A newName-=1
   set folder=%newName%
   if exist "%destination%\%folder%" goto getNewName
exit /B
于 2012-11-01T03:11:19.207 に答える