使用 Bat 文件复制和重命名

时间:2021-04-28 13:48:48

标签: csv batch-file

我也在 (https://www.mrexcel.com/board/threads/copy-and-rename-with-bat-file.1169385/) 上发布了这个问题 我已经阅读了很多关于此的帖子,并解决了我使用以下代码复制和重命名文件的问题之一,但是在尝试理解本网站上有关此主题的一些帖子时,我仍然不知所措。

xcopy "M:\abc.csv" "O:\File"
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename "O:\File\abc.csv" "x_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%.csv"

我的输出是文件名 = x_YYYY-MM-DD_TTTT.csv,我不明白所有变量是如何工作的,但我需要让这个 bat 文件将 abc.csv 文件重命名为“MM- DD-YYYY-#XXX.csv”。 “#”将是一个增量值,具体取决于一天中有多少文件被重命名(需要在第二天重新开始),“XXX”只是每个文件的设置文本值。 abc.csv 每天都会有相同的名称。我开始玩这个脚本,但我被卡住了。我知道我错过了增量部分。

xcopy "M:\abc.csv" "P:\File"
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename "P:\File\abc.csv" "%date:~4,2%-%date:~7,2%-%date:~10,4%XXX.csv"

最后一个问题,在我复制和重命名的文件夹中,他们将有 3 个文件(abc.csv、def.csv、hij.csv),所有 3 个文件都将被复制和重命名.相同的日期和增量格式,但具有不同的“XXX”文本。您是否将所有这些都放在一个 bat 文件中(我假设您无法在同一个脚本中容纳所有这些,您必须单独执行几行)还是将其分解为 3 个不同的 bat 文件?

我已经用实际的文件名和目录编辑了代码,也取得了一些进展。当您运行 echo ren 并输出多行并且它们都是相同的输出“04-28-2021-1Custinfo.txt”时,这是否意味着代码的增量部分不起作用?

@echo off
 setlocal enabledelayedexpansion
  for /d /r %%a in (*) do (
    set n=0
    set HR=%time:~0,2%
    set HR=%Hr: =0%
    set HR=%HR: =%   
    pushd "P:\Jarett\SM_TEST\IN"
      for /f "delims=" %%b in (' dir /b /a-d 2^>nul ') do (
        set /a n=n+1
           set num=0!n!
           echo ren "P:\Jarett\SM_TEST\IN\Header3.txt" "%date:~4,2%-%date:~7,2%-%date:~10,4%-!num:~-1!%Custinfo.txt"
    )
   popd
  )
pause

我觉得很好,尝试了我找到的其他代码,但是当我第二次运行它时,它会将 Header3.txt 文件保存为最终目标文件夹中的 Header3.txt。当我第二次运行 bat 文件时,我试图让它将其保存为 04-29-2021-2Custinfo.txt。我总是拉一个文件 Header3.txt 有关系吗,这就是为什么它不计算在内?

xcopy "M:\Sage 100 Advanced\2018\MAS90\John\MAS_Mirror\SM_TEST\IN\Header3.txt" "P:\Jarett\SM_TEST\IN" 
rem Presumes %date% returns Ddd DD/MM/YYYY so may need adjusting if American format
set today=%date:~4,2%-%date:~7,2%-%date:~10,4%
echo %today%
echo "Moving today's files to temp"
mkdir C:\Users\jarett.AS\temp
move %today%* temp
move Header3.txt temp
cd temp
set count=0
for %%x in (Header3.txt) do (set /a count+=1)
set count=00%count%
set count=%count:~-1%
rename "P:\Jarett\SM_TEST\IN\Header3.txt" "%today%-%count%Custinfo.txt"
move *.txt ..

这是我最终开始工作的代码。一直在玩它以完全理解一切并尝试不同的事情。我尝试将 Header3.txt rename Header3.txt %today%-%count%Custinfo%count%.txt 重命名,但每次都将其重命名为 04-30-2021-1Custinfo1.txt。有人可以指出我需要进一步研究的领域吗?

set today=%date:~4,2%-%date:~7,2%-%date:~10,4%
if not exist .\temp mkdir temp
move %today%* temp
xcopy "M:\Sage 100 Advanced\2018\MAS90\John\MAS_Mirror\SM_TEST\IN\Header3.txt" "P:\Jarett\SM_TEST\IN"
move P:\Jarett\SM_TEST\IN\Header3.txt temp
cd temp
set count=0
for %%x in (*.txt) do (set /a count+=1)
set count=00%count%
set count=%count:~-1%
rename Header3.txt %today%-%count%Custinfo.txt
move *.txt P:\Jarett\SM_TEST\IN

1 个答案:

答案 0 :(得分:0)

有些数字不清楚,但如果您想要更改,可以进行更改。

例如,不清楚您想要如何编号,是否可以,或者您是否想要每个 XXX

试试这个代码:

@echo off & setlocal EnableDelayedExpansion

cls

set sizemax=130
mode con: COLS=%sizemax% LINES=35

rem create "box" macro "Box"
set "Box=call :box"

%Box% "Prepare the test environment ..."
pause

cd %tmp%
mkdir TEST 2>nul
mkdir Jarett 2>nul

set "source_folder=%tmp%\TEST"
set "destination_folder=%tmp%\Jarett"

echo >%source_folder%\abc.csv
echo >%source_folder%\def.csv
echo >%source_folder%\hij.csv

%Box% "Source Folder"
dir %source_folder% & pause

%Box% "Destination Folder"
dir %destination_folder% & pause 

cls
%Box% "Test the script." & pause

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal EnableDelayedExpansion

rem set source and destination directory/folder
rem remove rem before using the script in production environment in the following two lines
rem set "source_folder=M:\TEST"
rem set "destination_folder=P:\Jarett"

rem assume all file of type and extension "CSV"

rem create 3 variable for the file name. You can change the name of the file abc,def,hij
set "File1=abc"
set "File2=def"
set "File3=hij"

rem create a map and lookup that associate abc with XXX01, def with XXX02, hij with XXX03. You can change the string XXX01, XXX02, XXX03
SET map=!File1!-XXX01;!File2!-XXX02;!File3!-XXX03;
%Box% "map=!map!" & pause

for %%N in (!File1!, !File2!, !File3!) do (

  rem Set the "today" variable. Choice the correct for you language setting and delete/comment others.
  set "today=%date:~7,2%-%date:~4,2%-%date:~10,4%"
  set "today=%date:~4,2%-%date:~7,2%-%date:~10,4%"
  set "today=%date:~3,2%-%date:~0,2%-%date:~6,4%" & rem italian default to MM-DD-YYYY
  %Box% "file:%%N today=!today!" & pause

  rem Count today csv, add 1 and prepare to pad.
  for /f %%f in (' dir !destination_folder!\!today!*.csv /b /a-d 2^>nul ^| find /c /v "" ') do set /a "count=%%f+10001"
  %Box% "file:%%N count=!count!" & pause

  rem prepare time part
  set "now=!time:~0,-3!" & set "now=!now: =0!" & set "now=!now::=.!"
  %Box% "file:%%N now=!now!" & pause

  call :lookup_XXX_map %%N
  %Box% "file:%%N XXX=!XXX!" & pause

  %Box% "move !source_folder!\%%N.csv !destination_folder!\!today!_!now!_!count:~-3!_!XXX!.csv" & pause
  move !source_folder!\%%N.csv !destination_folder!\!today!_!now!_!count:~-3!_!XXX!.csv

  %Box% "Destination Folder"
  dir !destination_folder! & pause
)

echo Exit/End ... & pause

goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:box
  set "s=%~1"
  set /A s1=%sizemax%-1, s2=s1-1
  set "l="
  for /L %%B in (1,2,%sizemax%) do set "l=!l!--" & if "!s:~%s1%,1!" == "" set "s= !s! "
  set "s=!s:~1,%s2%!"
  set "l=!l:~0,%s2%!"
  for %%B in ("" "+%l%+" "|%s%|" "+%l%+" "") do echo(%%~B
goto :eof

:lookup_XXX_map
  CALL SET XXX=%%map:*%1-=%%
  SET XXX=%XXX:;=&:%"
goto :eof

您必须为“DATE”变量选择正确的布局。然后就可以跑了。

当测试没问题时,删除多余的行:

@echo off
setlocal EnableDelayedExpansion

rem set source and destination directory/folder
set "source_folder=M:\TEST"
set "destination_folder=P:\Jarett"

rem assume all file of type and extension "CSV"

rem create 3 variable for the file name. You can change the name of the file abc,def,hij
set "File1=abc"
set "File2=def"
set "File3=hij"

rem create a map and lookup that associate abc with XXX01, def with XXX02, hij with XXX03. You can change the string XXX01, XXX02, XXX03
SET map=!File1!-XXX01;!File2!-XXX02;!File3!-XXX03;

for %%N in (!File1!, !File2!, !File3!) do (

  rem Set the "today" variable. Choice the correct for you language setting and delete/comment others.
  set "today=%date:~7,2%-%date:~4,2%-%date:~10,4%"

  rem Count today csv, add 1 and prepare to pad.
  for /f %%f in (' dir !destination_folder!\!today!*.csv /b /a-d 2^>nul ^| find /c /v "" ') do set /a "count=%%f+10001"

  rem prepare time part
  set "now=!time:~0,-3!" & set "now=!now: =0!" & set "now=!now::=.!"
 
  call :lookup_XXX_map %%N
  
  move !source_folder!\%%N.csv !destination_folder!\!today!_!now!_!count:~-3!_!XXX!.csv

)

echo Exit/End ... & pause

goto :eof

:lookup_XXX_map
  CALL SET XXX=%%map:*%1-=%%
  SET XXX=%XXX:;=&:%"
goto :eof
相关问题