合并两个批处理文件

时间:2018-08-09 15:06:52

标签: batch-file

我有两个批处理文件。这是其中之一:

:: Create a list of all the jpg's in the folder
dir /b > List.txt

:: Create a new folder for each jpg with the same name as the jpg
for %%i in (*.jpg) do mkdir "%%~ni"

:: Move all the jpg's into the newly created folders
for %%i in (*.jpg) do move "%%i" "%%~ni"

:: Duplicate the jpg's in the folders and add "-original" onto the end
for /R %%f in (*.jpg) do copy "%%~f" "%%~dpnf-original%%~xf"

:: Copy csv into new folders with same file name as the jpg's
for /D %%a in ("*") do copy /y /d C:\Projects\test.csv "%%a\%%a.csv"

这是另一个:

:: Replace string in csv with csv file name and overwite csv file
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        set "line=!line:REPLACE=%%~ni!"
        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)

如果我运行第一个,则第二个运行正常。但是然后我尝试将它们合并到一个批处理文件中:

:: Create a list of all the jpg's in the folder
dir /b > List.txt

:: Create a new folder for each jpg with the same name as the jpg
for %%a in (*.jpg) do mkdir "%%~na"

:: Move all the jpg's into the newly created folders
for %%a in (*.jpg) do move "%%a" "%%~na"

:: Duplicate the jpg's in the folders and add "-original" onto the end
for /R %%b in (*.jpg) do copy "%%~b" "%%~dpnb-original%%~xb"

:: Copy csv into new folders with same file name as the jpg's
for /D %%c in ("*") do copy /Y /D C:\Projects\test.csv "%%c\%%c.csv"

:: Replace string in csv with csv file name and overwite csv file
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        set "line=!line:REPLACE=%%~ni!"
        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)

我在csv文件中得到了回音:

C:\Projects\Test>(
set "line=REPLACE"  
 set "line=!line:REPLACE=NEWNAME!"  
 echo(!line! 
) 
!line!

NEWNAME意味着要替换REPLACE。我确保没有两次使用过变量,所以我有点不确定为什么会这样。

2 个答案:

答案 0 :(得分:0)

通过添加回显,setlocal线和endlocal使其工作。

:: Create a list of all the jpg's in the folder
dir /b > List.txt

:: Create a new folder for each jpg with the same name as the jpg
for %%a in (*.jpg) do mkdir "%%~na"

:: Move all the jpg's into the newly created folders
for %%a in (*.jpg) do move "%%a" "%%~na"

:: Duplicate the jpg's in the folders and add "-original" onto the end
for /R %%b in (*.jpg) do copy "%%~b" "%%~dpnb-original%%~xb"

:: Copy csv into new folders with same file name as the jpg's
for /D %%c in ("*") do copy /Y /D C:\Projects\test.csv "%%c\%%c.csv"

:: Replace string in csv with csv file name and overwite csv file
@echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        set "line=!line:REPLACE=%%~ni!"
        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
endlocal

答案 1 :(得分:0)

我不确定我是否理解,但是我认为这可能是您发布的两个脚本一起作为一个脚本:

@Echo Off
>"List.txt" (For /F "Delims=" %%A In ('Where .:*.jpg') Do (Echo %%~nxA
        XCopy "%%A" "%%~dpnA\"&&Move /Y "%%A" "%%~dpnA\%%~nA-original%~xA">Nul
        (For /F "UseBackQ Delims=" %%B In ("C:\Projects\test.csv") Do (
                Set "_=%%B"&SetLocal EnableDelayedExpansion
                Echo=!_:/REPLACE-=/%%~nA-!&EndLocal))>"%%~dpnA\%%~nA.csv"))

未经测试,(并且我无意将其扩展到我在回答此问题时对您的两个脚本所做的理解之外的范围)。如果我的理解是错误的,请提供反馈。