批处理脚本 - 串联上的文件序列号,以保持名称唯一 [英] Batch script - concatenate sequential numbers on files to keep the names unique

查看:136
本文介绍了批处理脚本 - 串联上的文件序列号,以保持名称唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个脚本对应于文件名的文件夹中复制文件和我有部分制定了使用for循环,它检查,以确保目标文件夹存在,并复制文件一旦验证以为然。例如:11-01111_ABC_DEF.pdf将进入/ 11/0111 /。的长度可以由一个或两个字符而不同,但该格式是一致的。我抄我的下面基本的脚本。

我的问题是,有时需要处理具有相同名称的文件。我怎么能去串联一个数字文件的末尾,如果该文件的一个或多个副本中的目标文件夹中已经存在?

  SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
在(* .PDF * .TXT)%% X千万(
  设置源=C:\\文件
  集DEST = R:\\
  设置文件名= %% X
  集preFIX =文件名:〜!0,2!
  一套!的文件夹=文件名:3,5〜!  如果不存在!DEST!\\!preFIX!\\!文件夹! MOVE!文件名! !来源!\\失败
  如果存在!源!\\!文件名! MOVE!文件名! !DEST!\\!preFIX!\\!文件夹!


解决方案

  SETLOCAL ENABLEDELAYEDEXPANSION
设置目标=工作
对于%% i的(* .PDF * .txt)中做(
   FOR / F令牌= 1-3 * delims = -_。在%%Ĵ(%% I)做(
      如果存在%DEST%\\ %% J□\\ %% K是否存在%DEST%\\ %% J□\\ %%ķ\\ %%我是否存在%DEST%\\ %% J□\\ %%ķ\\% %的J - %% K-1 _ %%升_ %% M(
     FOR / F令牌= 1-9 * delims = -_。 %%的n('DIR%DEST%\\ %% J□\\ %%ķ\\ %%的J - %% K * - * / B')不设置/ A INC = %% p
     集/ A INC + = 1
     移动%% i的%DEST%\\ %% J□\\ %%ķ\\ %%的J - !%% K-INC _ %%升_ %% M||移动%%〜我,失败
     设置INC =
     )
      如果存在%DEST%\\ %% J□\\ %%ķ\\ %% i的,如果不存在%DEST%\\ %% J□\\ %%ķ\\ %%的J - %% K-1 _ %%升_ %%米(
     移动%% i的%DEST%\\ %% J□\\ %%ķ\\ %%的J - %% K-1 _ %%升_ %% M||移动%%〜我,失败
     )
      如果存在%DEST%\\ %% J□\\ %% K,如果不存在%DEST%\\ %% J□\\ %%ķ\\ %%〜我(
     移动%%〜我%DEST%\\ %% J□\\ %%ķ\\ %%〜我||移动%%〜我,失败
     )
      如果不存在%DEST%\\ %% J□\\ %% K搬家%%〜我,失败
      )

把这个与.txts和.pdfs的文件夹中,如果你要我改变一些事情发表评论。

I'm working on a script for copying files in a folder which corresponds to the file's name and I have that part worked out using a FOR loop which checks to make sure the destination folder exists and copies the files once it has verified that it does. Example: 11-01111_ABC_DEF.pdf would go into /11/0111/. The length may vary by one or two characters but the format is consistent. I've copied my basic script below.

My problem is that sometimes a file with the same name needs to be processed. How could I go about concatenating a number to the end of the file if one or more copies of the file already exists in the destination folder?

setlocal enableextensions enabledelayedexpansion
for %%x in (*.PDF *.TXT) do (
  set "source="C:\files"
  set "dest=R:\"  
  set "filename=%%x"
  set "prefix=!filename:~0,2!"
  set "folder=!filename:~3,5!"

  if not exist !dest!\!prefix!\!folder! MOVE !filename! !source!\failed 
  if exist !source!\!filename! MOVE !filename! !dest!\!prefix!\!folder! 
)

解决方案

setlocal ENABLEDELAYEDEXPANSION
set "dest=worked"
for %%i in (*.pdf *.txt) do (
   for /f "tokens=1-3* delims=-_." %%j in ("%%i") do (
      if exist "%dest%\%%j\%%k" if exist "%dest%\%%j\%%k\%%i" if exist "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" (
     for /f "tokens=1-9* delims=-_." %%n in ('dir %dest%\%%j\%%k\%%j-%%k*-* /b') do set /a inc=%%p
     set /a inc+=1
     move "%%i" "%dest%\%%j\%%k\%%j-%%k-!inc!_%%l_%%m" || move "%%~i" "failed"
     set inc=
     )
      if exist "%dest%\%%j\%%k\%%i" if not exist "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" (
     move "%%i" "%dest%\%%j\%%k\%%j-%%k-1_%%l_%%m" || move "%%~i" "failed"
     )
      if exist "%dest%\%%j\%%k" if not exist "%dest%\%%j\%%k\%%~i" (
     move "%%~i" "%dest%\%%j\%%k\%%~i" || move "%%~i" "failed"
     )
      if not exist "%dest%\%%j\%%k" move "%%~i" "failed"
      )
)

put this in the folder with the .txts and .pdfs comment if you want me to change something.

这篇关于批处理脚本 - 串联上的文件序列号,以保持名称唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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